ComponentActions.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. ------------------- Code Monkey -------------------
  3. Thank you for downloading the Code Monkey Utilities
  4. I hope you find them useful in your projects
  5. If you have any questions use the contact form
  6. Cheers!
  7. unitycodemonkey.com
  8. --------------------------------------------------
  9. */
  10. using System;
  11. using UnityEngine;
  12. namespace CodeMonkey.MonoBehaviours {
  13. /*
  14. * Trigger Actions on MonoBehaviour Component events
  15. * */
  16. public class ComponentActions : MonoBehaviour {
  17. public Action OnDestroyFunc;
  18. public Action OnEnableFunc;
  19. public Action OnDisableFunc;
  20. public Action OnUpdate;
  21. private void OnDestroy() {
  22. if (OnDestroyFunc != null) OnDestroyFunc();
  23. }
  24. private void OnEnable() {
  25. if (OnEnableFunc != null) OnEnableFunc();
  26. }
  27. private void OnDisable() {
  28. if (OnDisableFunc != null) OnDisableFunc();
  29. }
  30. private void Update() {
  31. if (OnUpdate != null) OnUpdate();
  32. }
  33. public static void CreateComponent(Action OnDestroyFunc = null, Action OnEnableFunc = null, Action OnDisableFunc = null, Action OnUpdate = null) {
  34. GameObject gameObject = new GameObject("ComponentActions");
  35. AddComponent(gameObject, OnDestroyFunc, OnEnableFunc, OnDisableFunc, OnUpdate);
  36. }
  37. public static void AddComponent(GameObject gameObject, Action OnDestroyFunc = null, Action OnEnableFunc = null, Action OnDisableFunc = null, Action OnUpdate = null) {
  38. ComponentActions componentFuncs = gameObject.AddComponent<ComponentActions>();
  39. componentFuncs.OnDestroyFunc = OnDestroyFunc;
  40. componentFuncs.OnEnableFunc = OnEnableFunc;
  41. componentFuncs.OnDisableFunc = OnDisableFunc;
  42. componentFuncs.OnUpdate = OnUpdate;
  43. }
  44. }
  45. }