RevertComponent.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using UnityEngine;
  2. using System.Collections;
  3. using Engine.Component;
  4. using System;
  5. public class RevertComponent : BaseComponent
  6. {
  7. /// <summary>使用时间</summary>
  8. public float FUseTime = 3f;
  9. /// <summary>激活时间</summary>
  10. private float fEnableTime = 0f;
  11. private bool isOnEnable = false;
  12. private AudioSource m_source;
  13. private Action<RevertComponent> ResetCallBackFun;
  14. /// <summary>组件被激活时</summary>
  15. void OnEnable()
  16. {
  17. fEnableTime = Time.realtimeSinceStartup;
  18. isOnEnable = true;
  19. m_source = this.GetComponent<AudioSource> ();
  20. if(m_source != null)
  21. GameSoundManager.Instance.Register (m_source, this.gameObject);
  22. }
  23. void OnDisable()
  24. {
  25. if(m_source != null)
  26. GameSoundManager.Instance.UnRegister (m_source);
  27. }
  28. public void SetCallBack(Action<RevertComponent> callBack)
  29. {
  30. ResetCallBackFun = callBack;
  31. }
  32. /// <summary>帧函数</summary>
  33. void Update()
  34. {
  35. if (Time.realtimeSinceStartup - fEnableTime >= FUseTime)
  36. {
  37. Revert();
  38. }
  39. }
  40. public void Revert()
  41. {
  42. isOnEnable = false;
  43. if (ResetCallBackFun != null)
  44. {
  45. ResetCallBackFun.Invoke(this);
  46. ResetCallBackFun = null;
  47. }
  48. PrefabManager.Instance.RevertPrefab(this.gameObject, this.gameObject.name);
  49. this.transform.parent = null;
  50. }
  51. }