Tanoda
Plugins/LeapMotion/Core/Scripts/Animation/Tween/Internal/TweenRunner.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using UnityEngine.Assertions;
11using System;
12using System.Collections.Generic;
13
15
16 public class TweenRunner : MonoBehaviour {
17 private TweenInstance[] _runningTweens = new TweenInstance[16];
18 private int _runningCount = 0;
19
20 private Queue<TweenInstance> _toRecycle = new Queue<TweenInstance>();
21
22 private static TweenRunner _cachedInstance = null;
23 public static TweenRunner instance {
24 get {
25 if (_cachedInstance == null) {
26 _cachedInstance = FindObjectOfType<TweenRunner>();
27 if (_cachedInstance == null) {
28 _cachedInstance = new GameObject("__Tween Runner__").AddComponent<TweenRunner>();
29 _cachedInstance.gameObject.hideFlags = HideFlags.HideAndDontSave;
30 }
31 }
32 return _cachedInstance;
33 }
34 }
35
36 void Update() {
37 for (int i = _runningCount; i-- != 0;) {
38 var instance = _runningTweens[i];
39 try {
40 instance.Step(this);
41 } catch (Exception e) {
42 Debug.LogError("Error occured inside of tween! Tween has been terminated");
43 Debug.LogException(e);
44 if (instance.runnerIndex != -1) {
46 }
47 }
48 }
49
50 while (_toRecycle.Count > 0) {
51 var instance = _toRecycle.Dequeue();
52 Assert.IsTrue(instance.instanceId < 0, "Should never try to recycle a Tween with a valid instance id.");
53
54 if (instance.instanceId == TweenInstance.ID_WAITING_FOR_RECYCLE) {
55 Assert.IsTrue(instance.runnerIndex == TweenInstance.NOT_RUNNING, "Should never try to recycle a running Tween.");
56 instance.Dispose();
57 }
58 }
59 }
60
62 Assert.IsTrue(instance.runnerIndex == TweenInstance.NOT_RUNNING, "Should never schedule a running Tween for recycle.");
63
65 _toRecycle.Enqueue(instance);
66 }
67
69 if (_runningCount >= _runningTweens.Length) {
70 Utils.DoubleCapacity(ref _runningTweens);
71 }
72
73 instance.runnerIndex = _runningCount;
74 _runningTweens[_runningCount++] = instance;
75
76 //Dispatch events here, right when the tween has started and state is valid
77 if (instance.curPercent == 0.0f) {
78 if (instance.OnLeaveStart != null) {
79 instance.OnLeaveStart();
80 }
81 } else if (instance.curPercent == 1.0f) {
82 if (instance.OnLeaveEnd != null) {
83 instance.OnLeaveEnd();
84 }
85 }
86 }
87
89 if (instance.runnerIndex == -1) {
90 return;
91 }
92
93 --_runningCount;
94 if (_runningCount < 0) {
95 throw new Exception("Removed more tweens than were started!");
96 }
97
98 int index = instance.runnerIndex;
99
100 _runningTweens[_runningCount].runnerIndex = index;
101 _runningTweens[index] = _runningTweens[_runningCount];
102
103 instance.runnerIndex = -1;
104
105 //Dispatch events here, right when tween is stopped and state is valid
106 if (instance.curPercent == 1.0f) {
107 if (instance.OnReachEnd != null) {
108 instance.OnReachEnd();
109 }
110 } else if (instance.curPercent == 0.0f) {
111 if (instance.OnReachStart != null) {
112 instance.OnReachStart();
113 }
114 }
115
116 //Instance might have been re-added in one of the above callbacks
117 if (instance.runnerIndex == -1 && instance.returnToPoolUponStop) {
119 }
120 }
121 }
122}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19