PowerIcon.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. }
  51. public void OnClick()
  52. {
  53. if(battleController.GetCtrlCraft() != null && power != null)
  54. {
  55. if(battleController.AttempUsePower(power.GetId()))
  56. {
  57. DGAnalytics.CustomEvent ("Pow"+power.GetId());
  58. }
  59. }
  60. }
  61. // Update is called once per frame
  62. void FixedUpdate ()
  63. {
  64. if(power != null)
  65. {
  66. if(power.isActivating != activating)
  67. {
  68. activating = power.isActivating;
  69. activatingImage.enabled = activating;
  70. }
  71. if(!power.IsCoolDown())
  72. {
  73. lightImage.fillAmount = power.GetCDTime()/power.GetCDDuration();
  74. }
  75. else if(lightImage.fillAmount < 1f)
  76. {
  77. lightImage.fillAmount = 1f;
  78. }
  79. }
  80. }
  81. }