blob: e0eedc8bfe52bc043a5082bf1c5884575312ce62 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
/*
------------------- 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;
}
}
}
|