PowerIcon.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class PowerIcon : MonoBehaviour
  6. {
  7. public bool isCrystalPowerIcon;
  8. public Image darkImage;
  9. public Image lightImage;
  10. public Image activatingImage;
  11. public Text priceTxt;
  12. private BattleController battleController;
  13. private Power power;
  14. private bool activating;
  15. public Sprite defaultIcon;
  16. public void init(BattleController battleController)
  17. {
  18. this.battleController = battleController;
  19. SetPower(power);
  20. }
  21. public void Show()
  22. {
  23. transform.localScale = new Vector3(1f, 1f, 1f);
  24. }
  25. public void Hide()
  26. {
  27. transform.localScale = Vector3.zero;
  28. }
  29. public void SetPower(Power power)
  30. {
  31. this.power = power;
  32. if (power != null)
  33. {
  34. if(power.GetIcon() != null)
  35. {
  36. lightImage.sprite = darkImage.sprite = power.GetIcon();
  37. priceTxt.text = power.crystalNeed > 0 ? power.crystalNeed.ToString() : "";
  38. }
  39. activating = power.isActivating;
  40. }
  41. else
  42. {
  43. if(!isCrystalPowerIcon && defaultIcon != null)
  44. {
  45. lightImage.sprite = darkImage.sprite = defaultIcon;
  46. }
  47. activating = false;
  48. }
  49. activatingImage.enabled = activating;
  50. transform.localScale = power != null ? Vector3.one : Vector3.zero;
  51. }
  52. public void OnClick()
  53. {
  54. if(battleController.GetCtrlCraft() != null && power != null)
  55. {
  56. if(battleController.AttempUsePower(power.GetId()))
  57. {
  58. DGAnalytics.CustomEvent ("Pow"+power.GetId());
  59. }
  60. }
  61. }
  62. // Update is called once per frame
  63. void FixedUpdate ()
  64. {
  65. if(power != null)
  66. {
  67. if(power.isActivating != activating)
  68. {
  69. activating = power.isActivating;
  70. activatingImage.enabled = activating;
  71. }
  72. if(!power.IsCoolDown())
  73. {
  74. lightImage.fillAmount = power.GetCDTime()/power.GetCDDuration();
  75. }
  76. else if(lightImage.fillAmount < 1f)
  77. {
  78. lightImage.fillAmount = 1f;
  79. }
  80. }
  81. }
  82. }