TestingUnitTests.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using DentedPixel;
  5. namespace DentedPixel.LTExamples
  6. {
  7. public class TestingUnitTests : MonoBehaviour
  8. {
  9. public GameObject cube1;
  10. public GameObject cube2;
  11. public GameObject cube3;
  12. public GameObject cube4;
  13. public GameObject cubeAlpha1;
  14. public GameObject cubeAlpha2;
  15. private bool eventGameObjectWasCalled = false, eventGeneralWasCalled = false;
  16. private int lt1Id;
  17. private LTDescr lt2;
  18. private LTDescr lt3;
  19. private LTDescr lt4;
  20. private LTDescr[] groupTweens;
  21. private GameObject[] groupGOs;
  22. private int groupTweensCnt;
  23. private int rotateRepeat;
  24. private int rotateRepeatAngle;
  25. private GameObject boxNoCollider;
  26. private float timeElapsedNormalTimeScale;
  27. private float timeElapsedIgnoreTimeScale;
  28. private bool pauseTweenDidFinish = false;
  29. void Awake()
  30. {
  31. boxNoCollider = GameObject.CreatePrimitive(PrimitiveType.Cube);
  32. Destroy(boxNoCollider.GetComponent(typeof(BoxCollider)) as Component);
  33. }
  34. void Start()
  35. {
  36. // Time.timeScale = 0.25f;
  37. LeanTest.timeout = 46f;
  38. LeanTest.expected = 62;
  39. LeanTween.init(1300);
  40. // add a listener
  41. LeanTween.addListener(cube1, 0, eventGameObjectCalled);
  42. LeanTest.expect(LeanTween.isTweening() == false, "NOTHING TWEEENING AT BEGINNING");
  43. LeanTest.expect(LeanTween.isTweening(cube1) == false, "OBJECT NOT TWEEENING AT BEGINNING");
  44. LeanTween.scaleX(cube4, 2f, 0f).setOnComplete(() => {
  45. LeanTest.expect(cube4.transform.localScale.x == 2f, "TWEENED WITH ZERO TIME");
  46. });
  47. // dispatch event that is received
  48. LeanTween.dispatchEvent(0);
  49. LeanTest.expect(eventGameObjectWasCalled, "EVENT GAMEOBJECT RECEIVED");
  50. // do not remove listener
  51. LeanTest.expect(LeanTween.removeListener(cube2, 0, eventGameObjectCalled) == false, "EVENT GAMEOBJECT NOT REMOVED");
  52. // remove listener
  53. LeanTest.expect(LeanTween.removeListener(cube1, 0, eventGameObjectCalled), "EVENT GAMEOBJECT REMOVED");
  54. // add a listener
  55. LeanTween.addListener(1, eventGeneralCalled);
  56. // dispatch event that is received
  57. LeanTween.dispatchEvent(1);
  58. LeanTest.expect(eventGeneralWasCalled, "EVENT ALL RECEIVED");
  59. // remove listener
  60. LeanTest.expect(LeanTween.removeListener(1, eventGeneralCalled), "EVENT ALL REMOVED");
  61. lt1Id = LeanTween.move(cube1, new Vector3(3f, 2f, 0.5f), 1.1f).id;
  62. LeanTween.move(cube2, new Vector3(-3f, -2f, -0.5f), 1.1f);
  63. LeanTween.reset();
  64. // Queue up a bunch of tweens, cancel some of them but expect the remainder to finish
  65. GameObject[] cubes = new GameObject[99];
  66. int[] tweenIds = new int[cubes.Length];
  67. for (int i = 0; i < cubes.Length; i++)
  68. {
  69. GameObject c = cubeNamed("cancel" + i);
  70. tweenIds[i] = LeanTween.moveX(c, 100f, 1f).id;
  71. cubes[i] = c;
  72. }
  73. int onCompleteCount = 0;
  74. LeanTween.delayedCall(cubes[0], 0.2f, () => {
  75. for (int i = 0; i < cubes.Length; i++)
  76. {
  77. if (i % 3 == 0)
  78. {
  79. LeanTween.cancel(cubes[i]);
  80. }
  81. else if (i % 3 == 1)
  82. {
  83. LeanTween.cancel(tweenIds[i]);
  84. }
  85. else if (i % 3 == 2)
  86. {
  87. LTDescr descr = LeanTween.descr(tweenIds[i]);
  88. // Debug.Log("descr:"+descr);
  89. descr.setOnComplete(() => {
  90. onCompleteCount++;
  91. // Debug.Log("onCompleteCount:"+onCompleteCount);
  92. if (onCompleteCount >= 33)
  93. {
  94. LeanTest.expect(true, "CANCELS DO NOT EFFECT FINISHING");
  95. }
  96. });
  97. }
  98. }
  99. });
  100. Vector3[] splineArr = new Vector3[] { new Vector3(-1f, 0f, 0f), new Vector3(0f, 0f, 0f), new Vector3(4f, 0f, 0f), new Vector3(20f, 0f, 0f), new Vector3(30f, 0f, 0f) };
  101. LTSpline cr = new LTSpline(splineArr);
  102. cr.place(cube4.transform, 0.5f);
  103. LeanTest.expect((Vector3.Distance(cube4.transform.position, new Vector3(10f, 0f, 0f)) <= 0.7f), "SPLINE POSITIONING AT HALFWAY", "position is:" + cube4.transform.position + " but should be:(10f,0f,0f)");
  104. LeanTween.color(cube4, Color.green, 0.01f);
  105. // Debug.Log("Point 2:"+cr.ratioAtPoint(splineArr[2]));
  106. // OnStart Speed Test for ignoreTimeScale vs normal timeScale
  107. GameObject cubeDest = cubeNamed("cubeDest");
  108. Vector3 cubeDestEnd = new Vector3(100f, 20f, 0f);
  109. LeanTween.move(cubeDest, cubeDestEnd, 0.7f);
  110. GameObject cubeToTrans = cubeNamed("cubeToTrans");
  111. LeanTween.move(cubeToTrans, cubeDest.transform, 1.2f).setEase(LeanTweenType.easeOutQuad).setOnComplete(() => {
  112. LeanTest.expect(cubeToTrans.transform.position == cubeDestEnd, "MOVE TO TRANSFORM WORKS");
  113. });
  114. GameObject cubeDestroy = cubeNamed("cubeDestroy");
  115. LeanTween.moveX(cubeDestroy, 200f, 0.05f).setDelay(0.02f).setDestroyOnComplete(true);
  116. LeanTween.moveX(cubeDestroy, 200f, 0.1f).setDestroyOnComplete(true).setOnComplete(() => {
  117. LeanTest.expect(true, "TWO DESTROY ON COMPLETE'S SUCCEED");
  118. });
  119. GameObject cubeSpline = cubeNamed("cubeSpline");
  120. LeanTween.moveSpline(cubeSpline, new Vector3[] { new Vector3(0.5f, 0f, 0.5f), new Vector3(0.75f, 0f, 0.75f), new Vector3(1f, 0f, 1f), new Vector3(1f, 0f, 1f) }, 0.1f).setOnComplete(() => {
  121. LeanTest.expect(Vector3.Distance(new Vector3(1f, 0f, 1f), cubeSpline.transform.position) < 0.01f, "SPLINE WITH TWO POINTS SUCCEEDS");
  122. });
  123. // This test works when it is positioned last in the test queue (probably worth fixing when you have time)
  124. GameObject jumpCube = cubeNamed("jumpTime");
  125. jumpCube.transform.position = new Vector3(100f, 0f, 0f);
  126. jumpCube.transform.localScale *= 100f;
  127. int jumpTimeId = LeanTween.moveX(jumpCube, 200f, 1f).id;
  128. LeanTween.delayedCall(gameObject, 0.2f, () => {
  129. LTDescr d = LeanTween.descr(jumpTimeId);
  130. float beforeX = jumpCube.transform.position.x;
  131. d.setTime(0.5f);
  132. LeanTween.delayedCall(0.0f, () => { }).setOnStart(() => {
  133. float diffAmt = 1f;// This variable is dependent on a good frame-rate because it evalutes at the next Update
  134. beforeX += Time.deltaTime * 100f * 2f;
  135. LeanTest.expect(Mathf.Abs(jumpCube.transform.position.x - beforeX) < diffAmt, "CHANGING TIME DOESN'T JUMP AHEAD", "Difference:" + Mathf.Abs(jumpCube.transform.position.x - beforeX) + " beforeX:" + beforeX + " now:" + jumpCube.transform.position.x + " dt:" + Time.deltaTime);
  136. });
  137. });
  138. // Tween with time of zero is needs to be set to it's final value
  139. GameObject zeroCube = cubeNamed("zeroCube");
  140. LeanTween.moveX(zeroCube, 10f, 0f).setOnComplete(() => {
  141. LeanTest.expect(zeroCube.transform.position.x == 10f, "ZERO TIME FINSHES CORRECTLY", "final x:" + zeroCube.transform.position.x);
  142. });
  143. // Scale, and OnStart
  144. GameObject cubeScale = cubeNamed("cubeScale");
  145. LeanTween.scale(cubeScale, new Vector3(5f, 5f, 5f), 0.01f).setOnStart(() => {
  146. LeanTest.expect(true, "ON START WAS CALLED");
  147. }).setOnComplete(() => {
  148. LeanTest.expect(cubeScale.transform.localScale.z == 5f, "SCALE", "expected scale z:" + 5f + " returned:" + cubeScale.transform.localScale.z);
  149. });
  150. // Rotate
  151. GameObject cubeRotate = cubeNamed("cubeRotate");
  152. LeanTween.rotate(cubeRotate, new Vector3(0f, 180f, 0f), 0.02f).setOnComplete(() => {
  153. LeanTest.expect(cubeRotate.transform.eulerAngles.y == 180f, "ROTATE", "expected rotate y:" + 180f + " returned:" + cubeRotate.transform.eulerAngles.y);
  154. });
  155. // RotateAround
  156. GameObject cubeRotateA = cubeNamed("cubeRotateA");
  157. LeanTween.rotateAround(cubeRotateA, Vector3.forward, 90f, 0.3f).setOnComplete(() => {
  158. LeanTest.expect(cubeRotateA.transform.eulerAngles.z == 90f, "ROTATE AROUND", "expected rotate z:" + 90f + " returned:" + cubeRotateA.transform.eulerAngles.z);
  159. });
  160. // RotateAround 360
  161. GameObject cubeRotateB = cubeNamed("cubeRotateB");
  162. cubeRotateB.transform.position = new Vector3(200f, 10f, 8f);
  163. LeanTween.rotateAround(cubeRotateB, Vector3.forward, 360f, 0.3f).setPoint(new Vector3(5f, 3f, 2f)).setOnComplete(() => {
  164. LeanTest.expect(cubeRotateB.transform.position.ToString() == (new Vector3(200f, 10f, 8f)).ToString(), "ROTATE AROUND 360", "expected rotate pos:" + (new Vector3(200f, 10f, 8f)) + " returned:" + cubeRotateB.transform.position);
  165. });
  166. // Alpha, onUpdate with passing value, onComplete value
  167. LeanTween.alpha(cubeAlpha1, 0.5f, 0.1f).setOnUpdate((float val) => {
  168. LeanTest.expect(val != 0f, "ON UPDATE VAL");
  169. }).setOnCompleteParam("Hi!").setOnComplete((object completeObj) => {
  170. LeanTest.expect(((string)completeObj) == "Hi!", "ONCOMPLETE OBJECT");
  171. LeanTest.expect(cubeAlpha1.GetComponent<Renderer>().material.color.a == 0.5f, "ALPHA");
  172. });
  173. // Color
  174. float onStartTime = -1f;
  175. LeanTween.color(cubeAlpha2, Color.cyan, 0.3f).setOnComplete(() => {
  176. LeanTest.expect(cubeAlpha2.GetComponent<Renderer>().material.color == Color.cyan, "COLOR");
  177. LeanTest.expect(onStartTime >= 0f && onStartTime < Time.time, "ON START", "onStartTime:" + onStartTime + " time:" + Time.time);
  178. }).setOnStart(() => {
  179. onStartTime = Time.time;
  180. });
  181. // moveLocalY (make sure uses y values)
  182. Vector3 beforePos = cubeAlpha1.transform.position;
  183. LeanTween.moveY(cubeAlpha1, 3f, 0.2f).setOnComplete(() => {
  184. LeanTest.expect(cubeAlpha1.transform.position.x == beforePos.x && cubeAlpha1.transform.position.z == beforePos.z, "MOVE Y");
  185. });
  186. Vector3 beforePos2 = cubeAlpha2.transform.localPosition;
  187. LeanTween.moveLocalZ(cubeAlpha2, 12f, 0.2f).setOnComplete(() => {
  188. LeanTest.expect(cubeAlpha2.transform.localPosition.x == beforePos2.x && cubeAlpha2.transform.localPosition.y == beforePos2.y, "MOVE LOCAL Z", "ax:" + cubeAlpha2.transform.localPosition.x + " bx:" + beforePos.x + " ay:" + cubeAlpha2.transform.localPosition.y + " by:" + beforePos2.y);
  189. });
  190. AudioClip audioClip = LeanAudio.createAudio(new AnimationCurve(new Keyframe(0f, 1f, 0f, -1f), new Keyframe(1f, 0f, -1f, 0f)), new AnimationCurve(new Keyframe(0f, 0.001f, 0f, 0f), new Keyframe(1f, 0.001f, 0f, 0f)), LeanAudio.options());
  191. LeanTween.delayedSound(gameObject, audioClip, new Vector3(0f, 0f, 0f), 0.1f).setDelay(0.2f).setOnComplete(() => {
  192. LeanTest.expect(Time.time > 0, "DELAYED SOUND");
  193. });
  194. // Easing Methods
  195. int totalEasingCheck = 0;
  196. int totalEasingCheckSuccess = 0;
  197. for (int j = 0; j < 2; j++)
  198. {
  199. bool isCheckingFrom = j == 1;
  200. int totalTweenTypeLength = (int)LeanTweenType.easeShake;
  201. for (int i = (int)LeanTweenType.notUsed; i < totalTweenTypeLength; i++)
  202. {
  203. LeanTweenType easeType = (LeanTweenType)i;
  204. GameObject cube = cubeNamed("cube" + easeType);
  205. LTDescr descr = LeanTween.moveLocalX(cube, 5, 0.1f).setOnComplete((object obj) => {
  206. GameObject cubeIn = obj as GameObject;
  207. totalEasingCheck++;
  208. if (cubeIn.transform.position.x == 5f)
  209. {
  210. totalEasingCheckSuccess++;
  211. }
  212. if (totalEasingCheck == (2 * totalTweenTypeLength))
  213. {
  214. LeanTest.expect(totalEasingCheck == totalEasingCheckSuccess, "EASING TYPES");
  215. }
  216. }).setOnCompleteParam(cube);
  217. if (isCheckingFrom)
  218. descr.setFrom(-5f);
  219. }
  220. }
  221. // value2
  222. bool value2UpdateCalled = false;
  223. LeanTween.value(gameObject, new Vector2(0, 0), new Vector2(256, 96), 0.1f).setOnUpdate((Vector2 value) => {
  224. value2UpdateCalled = true;
  225. });
  226. LeanTween.delayedCall(0.2f, () => {
  227. LeanTest.expect(value2UpdateCalled, "VALUE2 UPDATE");
  228. });
  229. // check descr
  230. // LTDescr descr2 = LeanTween.descr( descrId );
  231. // LeanTest.expect(descr2 == null,"DESCRIPTION STARTS AS NULL");
  232. StartCoroutine(timeBasedTesting());
  233. }
  234. private GameObject cubeNamed(string name)
  235. {
  236. GameObject cube = Instantiate(boxNoCollider) as GameObject;
  237. cube.name = name;
  238. return cube;
  239. }
  240. IEnumerator timeBasedTesting()
  241. {
  242. yield return new WaitForEndOfFrame();
  243. GameObject cubeNormal = cubeNamed("normalTimeScale");
  244. // float timeElapsedNormal = Time.time;
  245. LeanTween.moveX(cubeNormal, 12f, 1.5f).setIgnoreTimeScale(false).setOnComplete(() => {
  246. timeElapsedNormalTimeScale = Time.time;
  247. });
  248. LTDescr[] descr = LeanTween.descriptions(cubeNormal);
  249. LeanTest.expect(descr.Length >= 0 && descr[0].to.x == 12f, "WE CAN RETRIEVE A DESCRIPTION");
  250. GameObject cubeIgnore = cubeNamed("ignoreTimeScale");
  251. LeanTween.moveX(cubeIgnore, 5f, 1.5f).setIgnoreTimeScale(true).setOnComplete(() => {
  252. timeElapsedIgnoreTimeScale = Time.time;
  253. });
  254. yield return new WaitForSeconds(1.5f);
  255. LeanTest.expect(Mathf.Abs(timeElapsedNormalTimeScale - timeElapsedIgnoreTimeScale) < 0.7f, "START IGNORE TIMING", "timeElapsedIgnoreTimeScale:" + timeElapsedIgnoreTimeScale + " timeElapsedNormalTimeScale:" + timeElapsedNormalTimeScale);
  256. // yield return new WaitForSeconds(100f);
  257. Time.timeScale = 4f;
  258. int pauseCount = 0;
  259. LeanTween.value(gameObject, 0f, 1f, 1f).setOnUpdate((float val) => {
  260. pauseCount++;
  261. }).pause();
  262. // Bezier should end at exact end position not just 99% close to it
  263. Vector3[] roundCirc = new Vector3[] { new Vector3(0f, 0f, 0f), new Vector3(-9.1f, 25.1f, 0f), new Vector3(-1.2f, 15.9f, 0f), new Vector3(-25f, 25f, 0f), new Vector3(-25f, 25f, 0f), new Vector3(-50.1f, 15.9f, 0f), new Vector3(-40.9f, 25.1f, 0f), new Vector3(-50f, 0f, 0f), new Vector3(-50f, 0f, 0f), new Vector3(-40.9f, -25.1f, 0f), new Vector3(-50.1f, -15.9f, 0f), new Vector3(-25f, -25f, 0f), new Vector3(-25f, -25f, 0f), new Vector3(0f, -15.9f, 0f), new Vector3(-9.1f, -25.1f, 0f), new Vector3(0f, 0f, 0f) };
  264. GameObject cubeRound = cubeNamed("bRound");
  265. Vector3 onStartPos = cubeRound.transform.position;
  266. LeanTween.moveLocal(cubeRound, roundCirc, 0.5f).setOnComplete(() => {
  267. LeanTest.expect(cubeRound.transform.position == onStartPos, "BEZIER CLOSED LOOP SHOULD END AT START", "onStartPos:" + onStartPos + " onEnd:" + cubeRound.transform.position);
  268. });
  269. // should be able to retrieve a point
  270. LTBezierPath roundCircPath = new LTBezierPath(roundCirc);
  271. float ratioPoint = roundCircPath.ratioAtPoint(new Vector3(-25f, 25f, 0f));
  272. LeanTest.expect(Mathf.Equals(ratioPoint, 0.25f), "BEZIER RATIO POINT");
  273. // Spline should end at exact end position not just 99% close to it
  274. Vector3[] roundSpline = new Vector3[] { new Vector3(0f, 0f, 0f), new Vector3(0f, 0f, 0f), new Vector3(2f, 0f, 0f), new Vector3(0.9f, 2f, 0f), new Vector3(0f, 0f, 0f), new Vector3(0f, 0f, 0f) };
  275. GameObject cubeSpline = cubeNamed("bSpline");
  276. Vector3 onStartPosSpline = cubeSpline.transform.position;
  277. LeanTween.moveSplineLocal(cubeSpline, roundSpline, 0.5f).setOnComplete(() => {
  278. LeanTest.expect(Vector3.Distance(onStartPosSpline, cubeSpline.transform.position) <= 0.01f, "SPLINE CLOSED LOOP SHOULD END AT START", "onStartPos:" + onStartPosSpline + " onEnd:" + cubeSpline.transform.position + " dist:" + Vector3.Distance(onStartPosSpline, cubeSpline.transform.position));
  279. });
  280. // Sequence test, do three tweens and make sure they end at the right points
  281. GameObject cubeSeq = cubeNamed("cSeq");
  282. var seq = LeanTween.sequence().append(LeanTween.moveX(cubeSeq, 100f, 0.2f));
  283. seq.append(0.1f).append(LeanTween.scaleX(cubeSeq, 2f, 0.1f));
  284. seq.append(() => {
  285. LeanTest.expect(cubeSeq.transform.position.x == 100f, "SEQ MOVE X FINISHED", "move x:" + cubeSeq.transform.position.x);
  286. LeanTest.expect(cubeSeq.transform.localScale.x == 2f, "SEQ SCALE X FINISHED", "scale x:" + cubeSeq.transform.localScale.x);
  287. }).setScale(0.2f);
  288. // Bounds check
  289. GameObject cubeBounds = cubeNamed("cBounds");
  290. bool didPassBounds = true;
  291. Vector3 failPoint = Vector3.zero;
  292. LeanTween.move(cubeBounds, new Vector3(10, 10, 10), 0.1f).setOnUpdate((float val) => {
  293. // Debug.LogWarning("cubeBounds x:"+cubeBounds.transform.position.x + " y:"+ cubeBounds.transform.position.y+" z:"+cubeBounds.transform.position.z);
  294. if (cubeBounds.transform.position.x < 0f || cubeBounds.transform.position.x > 10f || cubeBounds.transform.position.y < 0f || cubeBounds.transform.position.y > 10f || cubeBounds.transform.position.z < 0f || cubeBounds.transform.position.z > 10f)
  295. {
  296. didPassBounds = false;
  297. failPoint = cubeBounds.transform.position;
  298. // Debug.LogError("OUT OF BOUNDS");
  299. }
  300. }).setLoopPingPong().setRepeat(8).setOnComplete(() => {
  301. LeanTest.expect(didPassBounds, "OUT OF BOUNDS", "pos x:" + failPoint.x + " y:" + failPoint.y + " z:" + failPoint.z);
  302. });
  303. // Local scale check
  304. //GameObject cubeLocal = cubeNamed("cLocal");
  305. //LeanTween.scale(cubeLocal, new Vector3(0.5f, 0.5f, 0.5f), 0.2f).setOnComplete(() =>
  306. //{
  307. // LeanTest.expect((cubeLocal.transform.localScale.x == 0.5f && cubeLocal.transform.localScale.y == 0.5f && cubeLocal.transform.localScale.z == 0.5f), "SCALE WORKS", "scale x:" + cubeLocal.transform.localScale.x + " y:" + cubeLocal.transform.localScale.y + " z:" + cubeLocal.transform.localScale.z);
  308. //});
  309. // Groups of tweens testing
  310. groupTweens = new LTDescr[1200];
  311. groupGOs = new GameObject[groupTweens.Length];
  312. groupTweensCnt = 0;
  313. int descriptionMatchCount = 0;
  314. for (int i = 0; i < groupTweens.Length; i++)
  315. {
  316. GameObject cube = cubeNamed("c" + i);
  317. cube.transform.position = new Vector3(0, 0, i * 3);
  318. groupGOs[i] = cube;
  319. }
  320. yield return new WaitForEndOfFrame();
  321. bool hasGroupTweensCheckStarted = false;
  322. int setOnStartNum = 0;
  323. int setPosNum = 0;
  324. bool setPosOnUpdate = true;
  325. for (int i = 0; i < groupTweens.Length; i++)
  326. {
  327. Vector3 finalPos = transform.position + Vector3.one * 3f;
  328. Dictionary<string, object> finalDict = new Dictionary<string, object> { { "final", finalPos }, { "go", groupGOs[i] } };
  329. groupTweens[i] = LeanTween.move(groupGOs[i], finalPos, 3f).setOnStart(() => {
  330. setOnStartNum++;
  331. }).setOnUpdate((Vector3 newPosition) => {
  332. if (transform.position.z > newPosition.z)
  333. {
  334. setPosOnUpdate = false;
  335. }
  336. // Debug.LogWarning("New Position: " + newPosition.ToString());
  337. }).
  338. setOnCompleteParam(finalDict).
  339. setOnComplete((object param) => {
  340. Dictionary<string, object> finalDictRetr = param as Dictionary<string, object>;
  341. Vector3 neededPos = (Vector3)finalDictRetr["final"];
  342. GameObject tweenedGo = finalDictRetr["go"] as GameObject;
  343. if (neededPos.ToString() == tweenedGo.transform.position.ToString())
  344. setPosNum++;
  345. else
  346. {
  347. // Debug.Log("neededPos:"+neededPos+" tweenedGo.transform.position:"+tweenedGo.transform.position);
  348. }
  349. if (hasGroupTweensCheckStarted == false)
  350. {
  351. hasGroupTweensCheckStarted = true;
  352. LeanTween.delayedCall(gameObject, 0.1f, () => {
  353. LeanTest.expect(setOnStartNum == groupTweens.Length, "SETONSTART CALLS", "expected:" + groupTweens.Length + " was:" + setOnStartNum);
  354. LeanTest.expect(groupTweensCnt == groupTweens.Length, "GROUP FINISH", "expected " + groupTweens.Length + " tweens but got " + groupTweensCnt);
  355. LeanTest.expect(setPosNum == groupTweens.Length, "GROUP POSITION FINISH", "expected " + groupTweens.Length + " tweens but got " + setPosNum);
  356. LeanTest.expect(setPosOnUpdate, "GROUP POSITION ON UPDATE");
  357. });
  358. }
  359. groupTweensCnt++;
  360. });
  361. if (LeanTween.description(groupTweens[i].id).trans == groupTweens[i].trans)
  362. descriptionMatchCount++;
  363. }
  364. while (LeanTween.tweensRunning < groupTweens.Length)
  365. yield return null;
  366. LeanTest.expect(descriptionMatchCount == groupTweens.Length, "GROUP IDS MATCH");
  367. int expectedSearch = groupTweens.Length + 7;
  368. LeanTest.expect(LeanTween.maxSearch <= expectedSearch, "MAX SEARCH OPTIMIZED", "maxSearch:" + LeanTween.maxSearch + " should be:" + expectedSearch);
  369. LeanTest.expect(LeanTween.isTweening() == true, "SOMETHING IS TWEENING");
  370. // resume item before calling pause should continue item along it's way
  371. float previousXlt4 = cube4.transform.position.x;
  372. lt4 = LeanTween.moveX(cube4, 5.0f, 1.1f).setOnComplete(() => {
  373. LeanTest.expect(cube4 != null && previousXlt4 != cube4.transform.position.x, "RESUME OUT OF ORDER", "cube4:" + cube4 + " previousXlt4:" + previousXlt4 + " cube4.transform.position.x:" + (cube4 != null ? cube4.transform.position.x : 0));
  374. }).setDestroyOnComplete(true);
  375. lt4.resume();
  376. rotateRepeat = rotateRepeatAngle = 0;
  377. LeanTween.rotateAround(cube3, Vector3.forward, 360f, 0.1f).setRepeat(3).setOnComplete(rotateRepeatFinished).setOnCompleteOnRepeat(true).setDestroyOnComplete(true);
  378. yield return new WaitForEndOfFrame();
  379. LeanTween.delayedCall(0.1f * 8f + 1f, rotateRepeatAllFinished);
  380. int countBeforeCancel = LeanTween.tweensRunning;
  381. LeanTween.cancel(lt1Id);
  382. LeanTest.expect(countBeforeCancel == LeanTween.tweensRunning, "CANCEL AFTER RESET SHOULD FAIL", "expected " + countBeforeCancel + " but got " + LeanTween.tweensRunning);
  383. LeanTween.cancel(cube2);
  384. int tweenCount = 0;
  385. for (int i = 0; i < groupTweens.Length; i++)
  386. {
  387. if (LeanTween.isTweening(groupGOs[i]))
  388. tweenCount++;
  389. if (i % 3 == 0)
  390. LeanTween.pause(groupGOs[i]);
  391. else if (i % 3 == 1)
  392. groupTweens[i].pause();
  393. else
  394. LeanTween.pause(groupTweens[i].id);
  395. }
  396. LeanTest.expect(tweenCount == groupTweens.Length, "GROUP ISTWEENING", "expected " + groupTweens.Length + " tweens but got " + tweenCount);
  397. yield return new WaitForEndOfFrame();
  398. tweenCount = 0;
  399. for (int i = 0; i < groupTweens.Length; i++)
  400. {
  401. if (i % 3 == 0)
  402. LeanTween.resume(groupGOs[i]);
  403. else if (i % 3 == 1)
  404. groupTweens[i].resume();
  405. else
  406. LeanTween.resume(groupTweens[i].id);
  407. if (i % 2 == 0 ? LeanTween.isTweening(groupTweens[i].id) : LeanTween.isTweening(groupGOs[i]))
  408. tweenCount++;
  409. }
  410. LeanTest.expect(tweenCount == groupTweens.Length, "GROUP RESUME");
  411. LeanTest.expect(LeanTween.isTweening(cube1) == false, "CANCEL TWEEN LTDESCR");
  412. LeanTest.expect(LeanTween.isTweening(cube2) == false, "CANCEL TWEEN LEANTWEEN");
  413. LeanTest.expect(pauseCount == 0, "ON UPDATE NOT CALLED DURING PAUSE", "expect pause count of 0, but got " + pauseCount);
  414. yield return new WaitForEndOfFrame();
  415. Time.timeScale = 0.25f;
  416. float tweenTime = 0.2f;
  417. float expectedTime = tweenTime * (1f / Time.timeScale);
  418. float start = Time.realtimeSinceStartup;
  419. bool onUpdateWasCalled = false;
  420. LeanTween.moveX(cube1, -5f, tweenTime).setOnUpdate((float val) => {
  421. onUpdateWasCalled = true;
  422. }).setOnComplete(() => {
  423. float end = Time.realtimeSinceStartup;
  424. float diff = end - start;
  425. LeanTest.expect(Mathf.Abs(expectedTime - diff) < 0.06f, "SCALED TIMING DIFFERENCE", "expected to complete in roughly " + expectedTime + " but completed in " + diff);
  426. LeanTest.expect(Mathf.Approximately(cube1.transform.position.x, -5f), "SCALED ENDING POSITION", "expected to end at -5f, but it ended at " + cube1.transform.position.x);
  427. LeanTest.expect(onUpdateWasCalled, "ON UPDATE FIRED");
  428. });
  429. bool didGetCorrectOnUpdate = false;
  430. LeanTween.value(gameObject, new Vector3(1f, 1f, 1f), new Vector3(10f, 10f, 10f), 1f).setOnUpdate((Vector3 val) => {
  431. didGetCorrectOnUpdate = val.x >= 1f && val.y >= 1f && val.z >= 1f;
  432. }).setOnComplete(() => {
  433. LeanTest.expect(didGetCorrectOnUpdate, "VECTOR3 CALLBACK CALLED");
  434. });
  435. yield return new WaitForSeconds(expectedTime);
  436. Time.timeScale = 1f;
  437. int ltCount = 0;
  438. GameObject[] allGos = FindObjectsOfType(typeof(GameObject)) as GameObject[];
  439. foreach (GameObject go in allGos)
  440. {
  441. if (go.name == "~LeanTween")
  442. ltCount++;
  443. }
  444. LeanTest.expect(ltCount == 1, "RESET CORRECTLY CLEANS UP");
  445. StartCoroutine(lotsOfCancels());
  446. }
  447. IEnumerator lotsOfCancels()
  448. {
  449. yield return new WaitForEndOfFrame();
  450. Time.timeScale = 4f;
  451. int cubeCount = 10;
  452. int[] tweensA = new int[cubeCount];
  453. GameObject[] aGOs = new GameObject[cubeCount];
  454. for (int i = 0; i < aGOs.Length; i++)
  455. {
  456. GameObject cube = Instantiate(boxNoCollider) as GameObject;
  457. cube.transform.position = new Vector3(0, 0, i * 2f);
  458. cube.name = "a" + i;
  459. aGOs[i] = cube;
  460. tweensA[i] = LeanTween.move(cube, cube.transform.position + new Vector3(10f, 0, 0), 0.5f + 1f * (1.0f / (float)aGOs.Length)).id;
  461. LeanTween.color(cube, Color.red, 0.01f);
  462. }
  463. yield return new WaitForSeconds(1.0f);
  464. int[] tweensB = new int[cubeCount];
  465. GameObject[] bGOs = new GameObject[cubeCount];
  466. for (int i = 0; i < bGOs.Length; i++)
  467. {
  468. GameObject cube = Instantiate(boxNoCollider) as GameObject;
  469. cube.transform.position = new Vector3(0, 0, i * 2f);
  470. cube.name = "b" + i;
  471. bGOs[i] = cube;
  472. tweensB[i] = LeanTween.move(cube, cube.transform.position + new Vector3(10f, 0, 0), 2f).id;
  473. }
  474. for (int i = 0; i < aGOs.Length; i++)
  475. {
  476. LeanTween.cancel(aGOs[i]);
  477. GameObject cube = aGOs[i];
  478. tweensA[i] = LeanTween.move(cube, new Vector3(0, 0, i * 2f), 2f).id;
  479. }
  480. yield return new WaitForSeconds(0.5f);
  481. for (int i = 0; i < aGOs.Length; i++)
  482. {
  483. LeanTween.cancel(aGOs[i]);
  484. GameObject cube = aGOs[i];
  485. tweensA[i] = LeanTween.move(cube, new Vector3(0, 0, i * 2f) + new Vector3(10f, 0, 0), 2f).id;
  486. }
  487. for (int i = 0; i < bGOs.Length; i++)
  488. {
  489. LeanTween.cancel(bGOs[i]);
  490. GameObject cube = bGOs[i];
  491. tweensB[i] = LeanTween.move(cube, new Vector3(0, 0, i * 2f), 2f).id;
  492. }
  493. yield return new WaitForSeconds(2.1f);
  494. bool inFinalPlace = true;
  495. for (int i = 0; i < aGOs.Length; i++)
  496. {
  497. if (Vector3.Distance(aGOs[i].transform.position, new Vector3(0, 0, i * 2f) + new Vector3(10f, 0, 0)) > 0.1f)
  498. inFinalPlace = false;
  499. }
  500. for (int i = 0; i < bGOs.Length; i++)
  501. {
  502. if (Vector3.Distance(bGOs[i].transform.position, new Vector3(0, 0, i * 2f)) > 0.1f)
  503. inFinalPlace = false;
  504. }
  505. LeanTest.expect(inFinalPlace, "AFTER LOTS OF CANCELS");
  506. GameObject cubePaused = cubeNamed("cPaused");
  507. cubePaused.LeanMoveX(10f, 1f).setOnComplete(() => {
  508. pauseTweenDidFinish = true;
  509. });
  510. StartCoroutine(pauseTimeNow());
  511. }
  512. IEnumerator pauseTimeNow()
  513. {
  514. yield return new WaitForSeconds(0.5f);
  515. Time.timeScale = 0;
  516. LeanTween.delayedCall(0.5f, () => {
  517. Time.timeScale = 1f;
  518. }).setUseEstimatedTime(true);
  519. LeanTween.delayedCall(1.5f, () => {
  520. LeanTest.expect(pauseTweenDidFinish, "PAUSE BY TIMESCALE FINISHES");
  521. }).setUseEstimatedTime(true);
  522. }
  523. void rotateRepeatFinished()
  524. {
  525. if (Mathf.Abs(cube3.transform.eulerAngles.z) < 0.0001f)
  526. rotateRepeatAngle++;
  527. rotateRepeat++;
  528. }
  529. void rotateRepeatAllFinished()
  530. {
  531. LeanTest.expect(rotateRepeatAngle == 3, "ROTATE AROUND MULTIPLE", "expected 3 times received " + rotateRepeatAngle + " times");
  532. LeanTest.expect(rotateRepeat == 3, "ROTATE REPEAT", "expected 3 times received " + rotateRepeat + " times");
  533. LeanTest.expect(cube3 == null, "DESTROY ON COMPLETE", "cube3:" + cube3);
  534. }
  535. void eventGameObjectCalled(LTEvent e)
  536. {
  537. eventGameObjectWasCalled = true;
  538. }
  539. void eventGeneralCalled(LTEvent e)
  540. {
  541. eventGeneralWasCalled = true;
  542. }
  543. }
  544. }