summaryrefslogtreecommitdiff
path: root/Runtime/Graphs/UnityEngine.Graphs/LogicNodeLibrary/YieldedNodeBase.cs
blob: 2086d9568c010aca1dd244f05fbd4145a1909601 (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
using System.Collections;
using UnityEngine;

namespace UnityEngine.Graphs.LogicGraph
{
	public abstract class YieldedNodeBase
	{
		protected float m_Time;
		protected float m_Percentage;

		public Action done;
		public Action update;

		public virtual float totalTime { set { m_Time = value; } }
		public virtual float percentage { get { return m_Percentage; } }

		protected YieldedNodeBase () {}

		protected YieldedNodeBase (float time)
		{
			m_Time = time;
		}

		public IEnumerator Start ()
		{
			OnStart ();

			if (m_Time > 0.0f)
			{
				float doneTime = Time.time + m_Time;
				float t = 0;
				do
				{
					t += Time.deltaTime;
					m_Percentage = t / m_Time;

					OnUpdate();
					if (update != null)
						update();

					yield return 0;
				} while (Time.time < doneTime);
			}

			OnDone();
			if (done != null)
				done();
		}

		protected abstract void OnStart ();
		protected abstract void OnUpdate ();
		protected abstract void OnDone ();
	}
}