summaryrefslogtreecommitdiff
path: root/Runtime/Graphs/UnityEngine.Graphs/GraphBehaviour.cs
blob: 2330c66b897a06deb09abf27d544c9ad06e22308 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using UnityEngine;

#if TESTING
using Object = UnityEngine.Graphs.Testing.Object;
#else
using Object = UnityEngine.Object;
#endif

namespace UnityEngine.Graphs.LogicGraph
{
	public interface IMonoBehaviourEventCaller
	{
		event GraphBehaviour.VoidDelegate OnAwake;
		event GraphBehaviour.VoidDelegate OnStart;
		event GraphBehaviour.VoidUpdateDelegate OnUpdate;
		event GraphBehaviour.VoidUpdateDelegate OnLateUpdate;
		event GraphBehaviour.VoidUpdateDelegate OnFixedUpdate;
	}

	//TODO: we can optimize this, now say Update will get called always for each graph even though nothing is using it
	// we can generate code for those in generated graph instead

	// Component that represents graph in the hierarchy. 
	// When used in editor it stores all LogicGraph state as well, but since those are editor side classes they get stripped out when the player is built.
	public class GraphBehaviour :
	#if TESTING
	Object, IMonoBehaviourEventCaller
	#else
	MonoBehaviour, IMonoBehaviourEventCaller
	#endif
	{
		[NonSerialized]
		private bool m_IsInitialized;

		#region Nodes
		public delegate void VoidDelegate ();
		public delegate void VoidUpdateDelegate (float deltaTime);

		[Logic]
		[Title("Flow Events/On Awake")]
		public event VoidDelegate OnAwake;

		[Logic]
		[Title("Flow Events/On Start")]
		public event VoidDelegate OnStart;

		[Logic]
		[Title("Flow Events/On Update")]
		public event VoidUpdateDelegate OnUpdate;

		[Logic]
		[Title("Flow Events/On Late Update")]
		public event VoidUpdateDelegate OnLateUpdate;

		[Logic]
		[Title("Flow Events/On Fixed Update")]
		public event VoidUpdateDelegate OnFixedUpdate;

		protected virtual void @ΣInit()
		{
		}

		private void Initialize()
		{
			if (m_IsInitialized)
				return;
			m_IsInitialized = true;
			@ΣInit();
		}

		public void OnEnable()
		{
			Initialize ();
		}

		public void Awake ()
		{
			Initialize ();

			if (OnAwake != null)
				OnAwake();
		}

		public void Start ()
		{
			if (OnStart != null)
				OnStart ();
		}

		public void Update ()
		{
			if (OnUpdate != null)
				OnUpdate (Time.deltaTime);
		}

		public void LateUpdate ()
		{
			if (OnLateUpdate != null)
				OnLateUpdate (Time.deltaTime);
		}

		public void FixedUpdate ()
		{
			if (OnFixedUpdate != null)
				OnFixedUpdate (Time.deltaTime);
		}
		#endregion

		protected void PullSceneReferenceVariables(string references)
		{
			foreach (var asm in System.AppDomain.CurrentDomain.GetAssemblies())
				if (asm.GetName().Name == "UnityEditor.Graphs.LogicGraph")
				{
					var method = asm.GetType("UnityEditor.Graphs.LogicGraph.CompilerUtils").GetMethod("SetEditorModeGeneratedGraphSceneReferences");
					method.Invoke(null, new object[] {this, references});
					break;
				}
		}
	}
}