LTSeq.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /**
  5. * Internal Representation of a Sequence<br>
  6. * <br>
  7. * &nbsp;&nbsp;<h4>Example:</h4>
  8. * var seq = LeanTween.sequence();<br>
  9. * seq.append(1f); <span style="color:gray">// delay everything one second</span><br>
  10. * seq.append( () => { <span style="color:gray">// fire an event before start</span><br>
  11. * &nbsp;Debug.Log("I have started");<br>
  12. * });<br>
  13. * seq.append( LeanTween.move(cube1, Vector3.one * 10f, 1f) ); <span style="color:gray">// do a tween</span><br>
  14. * seq.append( (object obj) => { <span style="color:gray">// fire event after tween</span><br>
  15. * &nbsp;var dict = obj as Dictionary<string,string>;<br>
  16. * &nbsp;Debug.Log("We are done now obj value:"+dict["hi"]);<br>
  17. * }, new Dictionary<string,string>(){ {"hi","sup"} } );<br>
  18. * @class LTSeq
  19. * @constructor
  20. */
  21. public class LTSeq {
  22. public LTSeq previous;
  23. public LTSeq current;
  24. public LTDescr tween;
  25. public float totalDelay;
  26. public float timeScale;
  27. private int debugIter;
  28. public uint counter;
  29. public bool toggle = false;
  30. private uint _id;
  31. public int id{
  32. get{
  33. uint toId = _id | counter << 16;
  34. /*uint backId = toId & 0xFFFF;
  35. uint backCounter = toId >> 16;
  36. if(_id!=backId || backCounter!=counter){
  37. Debug.LogError("BAD CONVERSION toId:"+_id);
  38. }*/
  39. return (int)toId;
  40. }
  41. }
  42. public void reset(){
  43. previous = null;
  44. tween = null;
  45. totalDelay = 0f;
  46. }
  47. public void init(uint id, uint global_counter){
  48. reset();
  49. _id = id;
  50. counter = global_counter;
  51. this.current = this;
  52. }
  53. private LTSeq addOn(){
  54. this.current.toggle = true;
  55. LTSeq lastCurrent = this.current;
  56. this.current = LeanTween.sequence(false);
  57. this.current.previous = lastCurrent;
  58. lastCurrent.toggle = false;
  59. this.current.totalDelay = lastCurrent.totalDelay;
  60. this.current.debugIter = lastCurrent.debugIter + 1;
  61. return current;
  62. }
  63. private float addPreviousDelays(){
  64. // Debug.Log("delay:"+delay+" count:"+this.current.count+" this.current.totalDelay:"+this.current.totalDelay);
  65. LTSeq prev = this.current.previous;
  66. if (prev != null && prev.tween!=null) {
  67. return this.current.totalDelay + prev.tween.time;
  68. }
  69. return this.current.totalDelay;
  70. }
  71. /**
  72. * Add a time delay to the sequence
  73. * @method append (delay)
  74. * @param {float} delay:float amount of time to add to the sequence
  75. * @return {LTSeq} LTDescr an object that distinguishes the tween
  76. * var seq = LeanTween.sequence();<br>
  77. * seq.append(1f); // delay everything one second<br>
  78. * seq.append( LeanTween.move(cube1, Vector3.one * 10f, 1f) ); // do a tween<br>
  79. */
  80. public LTSeq append( float delay ){
  81. this.current.totalDelay += delay;
  82. return this.current;
  83. }
  84. /**
  85. * Add a time delay to the sequence
  86. * @method append (method)
  87. * @param {System.Action} callback:System.Action method you want to be called
  88. * @return {LTSeq} LTSeq an object that you can add tweens, methods and time on to
  89. * @example
  90. * var seq = LeanTween.sequence();<br>
  91. * seq.append( () => { // fire an event before start<br>
  92. * &nbsp;Debug.Log("I have started");<br>
  93. * });<br>
  94. * seq.append( LeanTween.move(cube1, Vector3.one * 10f, 1f) ); // do a tween<br>
  95. * seq.append( () => { // fire event after tween<br>
  96. * &nbsp;Debug.Log("We are done now");<br>
  97. * });;<br>
  98. */
  99. public LTSeq append( System.Action callback ){
  100. LTDescr newTween = LeanTween.delayedCall(0f, callback);
  101. // Debug.Log("newTween:" + newTween);
  102. append(newTween);
  103. return addOn();
  104. }
  105. /**
  106. * Add a time delay to the sequence
  107. * @method add (method(object))
  108. * @param {System.Action} callback:System.Action method you want to be called
  109. * @return {LTSeq} LTSeq an object that you can add tweens, methods and time on to
  110. * @example
  111. * var seq = LeanTween.sequence();<br>
  112. * seq.append( () => { // fire an event before start<br>
  113. * &nbsp;Debug.Log("I have started");<br>
  114. * });<br>
  115. * seq.append( LeanTween.move(cube1, Vector3.one * 10f, 1f) ); // do a tween<br>
  116. * seq.append((object obj) => { // fire event after tween
  117. * &nbsp;var dict = obj as Dictionary<string,string>;
  118. * &nbsp;Debug.Log("We are done now obj value:"+dict["hi"]);
  119. * &nbsp;}, new Dictionary<string,string>(){ {"hi","sup"} } );
  120. */
  121. public LTSeq append( System.Action<object> callback, object obj ){
  122. append(LeanTween.delayedCall(0f, callback).setOnCompleteParam(obj));
  123. return addOn();
  124. }
  125. public LTSeq append( GameObject gameObject, System.Action callback ){
  126. append(LeanTween.delayedCall(gameObject, 0f, callback));
  127. return addOn();
  128. }
  129. public LTSeq append( GameObject gameObject, System.Action<object> callback, object obj ){
  130. append(LeanTween.delayedCall(gameObject, 0f, callback).setOnCompleteParam(obj));
  131. return addOn();
  132. }
  133. /**
  134. * Retrieve a sequencer object where you can easily chain together tweens and methods one after another
  135. *
  136. * @method add (tween)
  137. * @return {LTSeq} LTSeq an object that you can add tweens, methods and time on to
  138. * @example
  139. * var seq = LeanTween.sequence();<br>
  140. * seq.append( LeanTween.move(cube1, Vector3.one * 10f, 1f) ); // do a move tween<br>
  141. * seq.append( LeanTween.rotateAround( avatar1, Vector3.forward, 360f, 1f ) ); // then do a rotate tween<br>
  142. */
  143. public LTSeq append( LTDescr tween ){
  144. this.current.tween = tween;
  145. // Debug.Log("tween:" + tween + " delay:" + this.current.totalDelay);
  146. this.current.totalDelay = addPreviousDelays();
  147. tween.setDelay( this.current.totalDelay );
  148. return addOn();
  149. }
  150. public LTSeq insert( LTDescr tween ){
  151. this.current.tween = tween;
  152. tween.setDelay( addPreviousDelays() );
  153. return addOn();
  154. }
  155. public LTSeq setScale( float timeScale ){
  156. // Debug.Log("this.current:" + this.current.previous.debugIter+" tween:"+this.current.previous.tween);
  157. setScaleRecursive(this.current, timeScale, 500);
  158. return addOn();
  159. }
  160. private void setScaleRecursive( LTSeq seq, float timeScale, int count ){
  161. if (count > 0) {
  162. this.timeScale = timeScale;
  163. // Debug.Log("seq.count:" + count + " seq.tween:" + seq.tween);
  164. seq.totalDelay *= timeScale;
  165. if (seq.tween != null) {
  166. // Debug.Log("seq.tween.time * timeScale:" + seq.tween.time * timeScale + " seq.totalDelay:"+seq.totalDelay +" time:"+seq.tween.time+" seq.tween.delay:"+seq.tween.delay);
  167. if (seq.tween.time != 0f)
  168. seq.tween.setTime(seq.tween.time * timeScale);
  169. seq.tween.setDelay(seq.tween.delay * timeScale);
  170. }
  171. if (seq.previous != null)
  172. setScaleRecursive(seq.previous, timeScale, count - 1);
  173. }
  174. }
  175. public LTSeq reverse(){
  176. return addOn();
  177. }
  178. }