HPBarItem.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 Jiang Yin. All rights reserved.
  4. // Homepage: https://gameframework.cn/
  5. // Feedback: mailto:ellan@gameframework.cn
  6. //------------------------------------------------------------
  7. using System.Collections;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using UnityGameFramework.Runtime;
  11. namespace MetaClient
  12. {
  13. public class HPBarItem : MonoBehaviour
  14. {
  15. private const float AnimationSeconds = 0.3f;
  16. private const float KeepSeconds = 0.4f;
  17. private const float FadeOutSeconds = 0.3f;
  18. [SerializeField]
  19. private Slider m_HPBar = null;
  20. private Canvas m_ParentCanvas = null;
  21. private RectTransform m_CachedTransform = null;
  22. private CanvasGroup m_CachedCanvasGroup = null;
  23. private Entity m_Owner = null;
  24. private int m_OwnerId = 0;
  25. public Entity Owner
  26. {
  27. get
  28. {
  29. return m_Owner;
  30. }
  31. }
  32. public void Init(Entity owner, Canvas parentCanvas, float fromHPRatio, float toHPRatio)
  33. {
  34. if (owner == null)
  35. {
  36. Log.Error("Owner is invalid.");
  37. return;
  38. }
  39. m_ParentCanvas = parentCanvas;
  40. gameObject.SetActive(true);
  41. StopAllCoroutines();
  42. m_CachedCanvasGroup.alpha = 1f;
  43. if (m_Owner != owner || m_OwnerId != owner.Id)
  44. {
  45. m_HPBar.value = fromHPRatio;
  46. m_Owner = owner;
  47. m_OwnerId = owner.Id;
  48. }
  49. Refresh();
  50. StartCoroutine(HPBarCo(toHPRatio, AnimationSeconds, KeepSeconds, FadeOutSeconds));
  51. }
  52. public bool Refresh()
  53. {
  54. if (m_CachedCanvasGroup.alpha <= 0f)
  55. {
  56. return false;
  57. }
  58. if (m_Owner != null && Owner.Available && Owner.Id == m_OwnerId)
  59. {
  60. Vector3 worldPosition = m_Owner.CachedTransform.position + Vector3.forward;
  61. Vector3 screenPosition = GameEntry.Scene.MainCamera.WorldToScreenPoint(worldPosition);
  62. Vector2 position;
  63. if (RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)m_ParentCanvas.transform, screenPosition,
  64. m_ParentCanvas.worldCamera, out position))
  65. {
  66. m_CachedTransform.localPosition = position;
  67. }
  68. }
  69. return true;
  70. }
  71. public void Reset()
  72. {
  73. StopAllCoroutines();
  74. m_CachedCanvasGroup.alpha = 1f;
  75. m_HPBar.value = 1f;
  76. m_Owner = null;
  77. gameObject.SetActive(false);
  78. }
  79. private void Awake()
  80. {
  81. m_CachedTransform = GetComponent<RectTransform>();
  82. if (m_CachedTransform == null)
  83. {
  84. Log.Error("RectTransform is invalid.");
  85. return;
  86. }
  87. m_CachedCanvasGroup = GetComponent<CanvasGroup>();
  88. if (m_CachedCanvasGroup == null)
  89. {
  90. Log.Error("CanvasGroup is invalid.");
  91. return;
  92. }
  93. }
  94. private IEnumerator HPBarCo(float value, float animationDuration, float keepDuration, float fadeOutDuration)
  95. {
  96. yield return m_HPBar.SmoothValue(value, animationDuration);
  97. yield return new WaitForSeconds(keepDuration);
  98. yield return m_CachedCanvasGroup.FadeToAlpha(0f, fadeOutDuration);
  99. }
  100. }
  101. }