| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- /*
- ------------------- Code Monkey -------------------
- Thank you for downloading the Code Monkey Utilities
- I hope you find them useful in your projects
- If you have any questions use the contact form
- Cheers!
- unitycodemonkey.com
- --------------------------------------------------
- */
- using System;
- using UnityEngine;
- namespace CodeMonkey.MonoBehaviours {
- /*
- * Trigger Actions on MonoBehaviour Component events
- * */
- public class ComponentActions : MonoBehaviour {
- public Action OnDestroyFunc;
- public Action OnEnableFunc;
- public Action OnDisableFunc;
- public Action OnUpdate;
- private void OnDestroy() {
- if (OnDestroyFunc != null) OnDestroyFunc();
- }
- private void OnEnable() {
- if (OnEnableFunc != null) OnEnableFunc();
- }
- private void OnDisable() {
- if (OnDisableFunc != null) OnDisableFunc();
- }
- private void Update() {
- if (OnUpdate != null) OnUpdate();
- }
- public static void CreateComponent(Action OnDestroyFunc = null, Action OnEnableFunc = null, Action OnDisableFunc = null, Action OnUpdate = null) {
- GameObject gameObject = new GameObject("ComponentActions");
- AddComponent(gameObject, OnDestroyFunc, OnEnableFunc, OnDisableFunc, OnUpdate);
- }
- public static void AddComponent(GameObject gameObject, Action OnDestroyFunc = null, Action OnEnableFunc = null, Action OnDisableFunc = null, Action OnUpdate = null) {
- ComponentActions componentFuncs = gameObject.AddComponent<ComponentActions>();
- componentFuncs.OnDestroyFunc = OnDestroyFunc;
- componentFuncs.OnEnableFunc = OnEnableFunc;
- componentFuncs.OnDisableFunc = OnDisableFunc;
- componentFuncs.OnUpdate = OnUpdate;
- }
- }
- }
|