TipBulletNumber.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class TipBulletNumber : MonoBehaviour
  6. {
  7. [SerializeField] Text outText;
  8. [SerializeField] private bool autoDelete = true;
  9. private bool disableAutoDelete = false; // 是否禁用自动删除
  10. private Coroutine deleteCoroutine; // 记录计时协程,方便取消
  11. void Start()
  12. {
  13. if (autoDelete && !disableAutoDelete)
  14. {
  15. deleteCoroutine = StartCoroutine(AutoDelete());
  16. }
  17. }
  18. /// <summary>
  19. /// 3秒后自动删除对象(如果 `disableAutoDelete` 为 `false`)
  20. /// </summary>
  21. private IEnumerator AutoDelete()
  22. {
  23. yield return new WaitForSecondsRealtime(3.0f);
  24. Destroy(gameObject);
  25. }
  26. /// <summary>
  27. /// 手动删除对象
  28. /// </summary>
  29. public void Remove()
  30. {
  31. if (deleteCoroutine != null)
  32. {
  33. StopCoroutine(deleteCoroutine);
  34. }
  35. Destroy(gameObject);
  36. }
  37. /// <summary>
  38. /// 设置字体颜色
  39. /// </summary>
  40. /// <param name="color"></param>
  41. public void SetOutTipColor(Color color) {
  42. outText.color = color;
  43. }
  44. /// <summary>
  45. /// 设置是否禁用自动删除
  46. /// </summary>
  47. public void SetDisableAutoDelete(bool disable)
  48. {
  49. disableAutoDelete = disable;
  50. if (disableAutoDelete && deleteCoroutine != null)
  51. {
  52. StopCoroutine(deleteCoroutine);
  53. }
  54. }
  55. }