Tanoda
TransformTweenBehaviour.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
10using System;
11using System.Collections;
12using System.Collections.Generic;
13using UnityEngine;
15
16namespace Leap.Unity.Animation {
17
23 public class TransformTweenBehaviour : MonoBehaviour {
24
25 [Tooltip("The transform to which to apply the tweened properties.")]
26 public Transform targetTransform;
27
28 [Tooltip("The transform whose position/rotation/localScale provide the start state of the tween.")]
29 public Transform startTransform;
30 [Tooltip("The transform whose position/rotation/localScale provide the end state of the tween.")]
31 public Transform endTransform;
32
33 public bool startAtEnd = false;
34
35 [Header("Tween Settings")]
36 public bool tweenLocalPosition = true;
37 public bool tweenLocalRotation = true;
38 public bool tweenLocalScale = true;
39 [MinValue(0.001F)]
40 public float tweenDuration = 0.25F;
42
43 #region Events
44
45 public Action<float> OnProgress = (progress) => { };
46
47 public Action OnLeaveStart = () => { };
48 public Action OnReachEnd = () => { };
49 public Action OnLeaveEnd = () => { };
50 public Action OnReachStart = () => { };
51
52 #endregion
53
54 private Tween _tween;
60 public Tween tween {
61 get { return _tween; }
62 set { _tween = value; }
63 }
64
65 void OnValidate() {
66 if (targetTransform != null) {
68 Debug.LogError("The start transform of the TransformTweenBehaviour should be "
69 + "a different transform than the target transform; the start "
70 + "transform provides starting position/rotation/scale information "
71 + "for the tween.", this.gameObject);
72 }
73 else if (endTransform == targetTransform) {
74 Debug.LogError("The end transform of the TransformTweenBehaviour should be "
75 + "a different transform than the target transform; the end "
76 + "transform provides ending position/rotation/scale information "
77 + "for the tween.", this.gameObject);
78 }
79 }
80 }
81
82 void Awake() {
83 initUnityEvents();
84
85 // Tween setup methods return the Tween object itself, so you can chain your setup
86 // method calls.
87 _tween = Tween.Persistent().OverTime(tweenDuration)
89
90 if (tweenLocalPosition) _tween = _tween.Target(targetTransform)
91 .LocalPosition(startTransform, endTransform);
92
93 if (tweenLocalRotation) _tween = _tween.Target(targetTransform)
94 .LocalRotation(startTransform, endTransform);
95
96 if (tweenLocalScale) _tween = _tween.Target(targetTransform)
97 .LocalScale(startTransform, endTransform);
98
99 // Hook up the UnityEvents to the actual Tween callbacks.
100 _tween.OnProgress(OnProgress);
102 _tween.OnReachEnd(OnReachEnd);
103 _tween.OnLeaveEnd(OnLeaveEnd);
105
106 // TODO: This isn't great but it's the only way I've seen to make sure the tween
107 // updates with its progress at the right state :(
108 if (startAtEnd) {
109 _tween.progress = 0.9999999F;
110 _tween.Play(Direction.Forward);
111 }
112 else {
113 _tween.progress = 0.0000001F;
114 _tween.Play(Direction.Backward);
115 }
116 }
117
118 void OnDestroy() {
119 if (_tween.isValid) {
120 _tween.Release();
121 }
122 }
123
124 private Coroutine _playTweenAfterDelayCoroutine;
125 private Direction _curDelayedDirection = Direction.Backward;
126
133 public void PlayTween() {
134 PlayTween(Direction.Forward);
135 }
136
137 public void PlayTween(Direction tweenDirection = Direction.Forward, float afterDelay = 0F) {
138 if (_playTweenAfterDelayCoroutine != null && tweenDirection != _curDelayedDirection) {
139 StopCoroutine(_playTweenAfterDelayCoroutine);
140 _curDelayedDirection = tweenDirection;
141 }
142
143 _playTweenAfterDelayCoroutine = StartCoroutine(playAfterDelay(tweenDirection, afterDelay));
144 }
145
146 private IEnumerator playAfterDelay(Direction tweenDirection, float delay) {
147 yield return new WaitForSeconds(delay);
148
149 tween.Play(tweenDirection);
150 }
151
152 public void PlayForward() {
153 PlayTween(Direction.Forward);
154 }
155
156 public void PlayBackward() {
157 PlayTween(Direction.Backward);
158 }
159
160 public void PlayForwardAfterDelay(float delay = 0F) {
161 PlayTween(Direction.Forward, delay);
162 }
163
164 public void PlayBackwardAfterDelay(float delay = 0F) {
165 PlayTween(Direction.Backward, delay);
166 }
167
171 public void StopTween() {
172 tween.Stop();
173 }
174
175 public void SetTargetToStart() {
176 setTargetTo(startTransform);
177 }
178
179 public void SetTargetToEnd() {
180 setTargetTo(endTransform);
181 }
182
183 private void setTargetTo(Transform t) {
184 if (targetTransform != null && t != null) {
185 if (tweenLocalPosition) targetTransform.localPosition = t.localPosition;
186 if (tweenLocalRotation) targetTransform.localRotation = t.localRotation;
187 if (tweenLocalScale) targetTransform.localScale = t.localScale;
188 }
189 }
190
191 #region Unity Events (Internal)
192
193 [System.Serializable]
194 public class FloatEvent : UnityEvent<float> { }
195
196 #pragma warning disable 0649
197 [SerializeField]
198 private EnumEventTable _eventTable;
199 #pragma warning restore 0649
200
201 public enum EventType {
202 //OnProgress = 100, // Requires float Event data
203 OnLeaveStart = 110,
204 OnReachEnd = 120,
205 OnLeaveEnd = 130,
206 OnReachStart = 140
207 }
208
209 private void initUnityEvents() {
210 setupCallback(ref OnLeaveStart, EventType.OnLeaveStart);
211 setupCallback(ref OnReachEnd, EventType.OnReachEnd);
212 setupCallback(ref OnLeaveEnd, EventType.OnLeaveEnd);
213 setupCallback(ref OnReachStart, EventType.OnReachStart);
214 }
215
216 private void setupCallback(ref Action action, EventType type) {
217 action += () => _eventTable.Invoke((int)type);
218 }
219
220 private void setupCallback<T>(ref Action<T> action, EventType type) {
221 action += (anchObj) => _eventTable.Invoke((int)type);
222 }
223
224 #endregion
225
226 }
227
228}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
This is a wrapper MonoBehaviour that demonstrates and exposes some of the basic functionality of the ...
void StopTween()
Stops the underlying tween and resets it to the starting state.
void PlayTween(Direction tweenDirection=Direction.Forward, float afterDelay=0F)
void PlayTween()
Tweens play forward by default, but at any time past the starting state they can also be played backw...
Tween tween
Returns the Tween object the TransformTween behaviour produces on Start().
void Invoke(int enumValue)
Tween Play()
Starts playing this Tween. It will continue from the same position it left off on,...
Definition: Tween.cs:272
Tween OverTime(float seconds)
Specifies that this Tween should travel from begining to end over a certain number of seconds.
Definition: Tween.cs:165
Tween OnProgress(Action< float > action)
Specifies an action to be called every step of the Tween. This callback happens after:
Definition: Tween.cs:222
static Tween Persistent()
Creates a persistant Tween that will not ever auto-release itself. You must specifically call Release...
Definition: Tween.cs:36
Tween OnReachEnd(Action action)
Specifies an action to be called whenever this Tween reaches the end.
Definition: Tween.cs:262
Tween OnLeaveStart(Action action)
Specifies an action to be called whenever this Tween is Played forward when at the start.
Definition: Tween.cs:232
Tween Smooth(SmoothType type=SmoothType.Smooth)
Specifies that this Tween should the given smoothing method.
Definition: Tween.cs:184
bool isValid
Returns whether or not this Tween is considered valid. A Tween can become invalid under the following...
Definition: Tween.cs:54
void Release()
Forces this Tween to be recycled right away. Once this method is called, the Tween will be invalid an...
Definition: Tween.cs:362
Tween OnLeaveEnd(Action action)
Specifies an action to be called whenever this Tween is Played backwards when at the end.
Definition: Tween.cs:252
MaterialSelector Target(Material material)
void Stop()
Stops this Tween. If it is not a persistant Tween, it will be recycled right away.
Definition: Tween.cs:345
float progress
Gets or sets how far along completion this Tween is. This value is a percent that ranges from 0 to 1.
Definition: Tween.cs:99
Tween OnReachStart(Action action)
Specifies an action to be called whenever this Tween reaches the start.
Definition: Tween.cs:242