Tanoda
TweenInstance.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 System;
11using System.Collections;
12
14
15 public class TweenInstance : IPoolable, IDisposable {
16 private static int _nextInstanceId = 1;
17
18 public const int ID_UNUSED = 0;
19 public const int ID_IN_POOL = -1;
20 public const int ID_WAITING_FOR_RECYCLE = -2;
21 public const int ID_INVALID_STATE = -3;
23
24 public const int NOT_RUNNING = -1;
26
28
31
32 public float curPercent;
33 public float dstPercent;
34 public float velPercent;
36
38 public Func<float, float> smoothFunction;
39
40 public Action<float> OnProgress;
41 public Action OnLeaveEnd;
42 public Action OnReachEnd;
43 public Action OnLeaveStart;
44 public Action OnReachStart;
45
47
48 public TweenInstance() {
50 }
51
52 public void OnSpawn() {
53 instanceId = _nextInstanceId++;
55 }
56
57 public void OnRecycle() { }
58
59 public void ResetDefaults() {
61
62 curPercent = 0;
63 dstPercent = 1;
64 velPercent = 1; //By default, tween over the course of 1 second
65 direction = Direction.Forward;
66
67 smoothType = SmoothType.Linear;
68 smoothFunction = null;
69
70 OnProgress = null;
71 OnLeaveEnd = null;
72 OnReachEnd = null;
73 OnLeaveStart = null;
74 OnReachStart = null;
75 }
76
77 public void Dispose() {
79
80 for (int i = 0; i < interpolatorCount; i++) {
81 interpolators[i].Dispose();
82 interpolators[i] = null;
83 }
85
87
88 Pool<TweenInstance>.Recycle(this);
89 }
90
91 public void Step(TweenRunner runner) {
92 curPercent = Mathf.MoveTowards(curPercent, dstPercent, Time.deltaTime * velPercent);
93
95
96 if (curPercent == dstPercent) {
97 runner.RemoveTween(this);
98 }
99 }
100
101 public void interpolatePercent() {
102 float progress;
103 switch (smoothType) {
104 case SmoothType.Linear:
105 progress = curPercent;
106 break;
107 case SmoothType.Smooth:
108 progress = Mathf.SmoothStep(0, 1, curPercent);
109 break;
110 case SmoothType.SmoothEnd:
111 progress = 1.0f - (curPercent - 1.0f) * (curPercent - 1.0f);
112 break;
113 case SmoothType.SmoothStart:
114 progress = curPercent * curPercent;
115 break;
116 default:
117 progress = smoothFunction(curPercent);
118 break;
119 }
120
121 for (int i = interpolatorCount; i-- != 0;) {
122 IInterpolator interpolator = interpolators[i];
123 if (interpolator.isValid) {
124 interpolators[i].Interpolate(progress);
125 } else {
127 }
128 }
129
130 if (OnProgress != null) {
132 }
133 }
134
135 public struct TweenYieldInstruction : IEnumerator {
136 private TweenInstance _instance;
137 private int _instanceId;
138
140 _instance = instance;
141 _instanceId = _instance.instanceId;
142 }
143
144 public object Current {
145 get {
146 return null;
147 }
148 }
149
150 public bool MoveNext() {
151 return (_instanceId == _instance.instanceId) && (_instance.runnerIndex != -1);
152 }
153
154 public void Reset() { }
155 }
156 }
157}
Implement this interface to add your own interpolators to Tween!
bool isValid
Returns whether or not this interpolator is currently considered valid. Any invalid interpolators wil...
void Interpolate(float percent)
Called to trigger the interpolation of this interpolator. Use this callback to do whatever work your ...
Implement this interface to recieve a callback whenever your object is spawned from a pool.
Definition: Pool.cs:19