DelayShow.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using UnityEngine;
  2. using System.Collections;
  3. public class DelayShow : MonoBehaviour
  4. {
  5. public float delayTime = 1.0f;
  6. public Transform[] Targets;
  7. float leftTime = 0;
  8. bool bShowed = false;
  9. // Use this for initialization
  10. void Start()
  11. {
  12. if(delayTime > 0)
  13. {
  14. if (Targets == null || Targets.Length == 0) return;
  15. for (int idx = 0; idx < Targets.Length; idx++)
  16. {
  17. if(Targets[idx] != null)
  18. {
  19. Targets[idx].gameObject.SetActive(false);
  20. }
  21. }
  22. }
  23. }
  24. private void OnEnable()
  25. {
  26. bShowed = false;
  27. leftTime = delayTime;
  28. }
  29. private void OnDisable()
  30. {
  31. if (delayTime > 0)
  32. {
  33. if (Targets == null || Targets.Length == 0) return;
  34. for (int idx = 0; idx < Targets.Length; idx++)
  35. {
  36. if (Targets[idx] != null)
  37. {
  38. Targets[idx].gameObject.SetActive(false);
  39. }
  40. }
  41. }
  42. }
  43. private void Update()
  44. {
  45. if(!bShowed)
  46. {
  47. leftTime -= Time.unscaledDeltaTime;
  48. if (leftTime <= 0)
  49. {
  50. DelayFunc();
  51. }
  52. }
  53. }
  54. void DelayFunc()
  55. {
  56. if (Targets == null || Targets.Length == 0) return;
  57. for(int idx =0; idx < Targets.Length;idx++)
  58. {
  59. if (Targets[idx] != null)
  60. {
  61. Targets[idx].gameObject.SetActive(true);
  62. }
  63. }
  64. bShowed = true;
  65. }
  66. }