Ejemplo n.º 1
0
        IEnumerator timeBasedTesting()
        {
            yield return(new WaitForEndOfFrame());

            GameObject cubeNormal = cubeNamed("normalTimeScale");

            // float timeElapsedNormal = Time.time;
            LeanTween.moveX(cubeNormal, 12f, 1.5f).setIgnoreTimeScale(false).setOnComplete(() =>
            {
                timeElapsedNormalTimeScale = Time.time;
            });

            LTDescr[] descr = LeanTween.descriptions(cubeNormal);
            LeanTest.expect(descr.Length >= 0 && descr[0].to.x == 12f, "WE CAN RETRIEVE A DESCRIPTION");

            GameObject cubeIgnore = cubeNamed("ignoreTimeScale");

            LeanTween.moveX(cubeIgnore, 5f, 1.5f).setIgnoreTimeScale(true).setOnComplete(() =>
            {
                timeElapsedIgnoreTimeScale = Time.time;
            });

            yield return(new WaitForSeconds(1.5f));

            LeanTest.expect(Mathf.Abs(timeElapsedNormalTimeScale - timeElapsedIgnoreTimeScale) < 0.7f, "START IGNORE TIMING", "timeElapsedIgnoreTimeScale:" + timeElapsedIgnoreTimeScale + " timeElapsedNormalTimeScale:" + timeElapsedNormalTimeScale);

            //          yield return new WaitForSeconds(100f);
            Time.timeScale = 4f;

            int pauseCount = 0;

            LeanTween.value(gameObject, 0f, 1f, 1f).setOnUpdate((float val) =>
            {
                pauseCount++;
            }).pause();

            // Bezier should end at exact end position not just 99% close to it
            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) };
            GameObject cubeRound  = cubeNamed("bRound");
            Vector3    onStartPos = cubeRound.transform.position;

            LeanTween.moveLocal(cubeRound, roundCirc, 0.5f).setOnComplete(() =>
            {
                LeanTest.expect(cubeRound.transform.position == onStartPos, "BEZIER CLOSED LOOP SHOULD END AT START", "onStartPos:" + onStartPos + " onEnd:" + cubeRound.transform.position);
            });

            // should be able to retrieve a point
            LTBezierPath roundCircPath = new LTBezierPath(roundCirc);
            float        ratioPoint    = roundCircPath.ratioAtPoint(new Vector3(-25f, 25f, 0f));

            LeanTest.expect(Mathf.Equals(ratioPoint, 0.25f), "BEZIER RATIO POINT");

            // Spline should end at exact end position not just 99% close to it
            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) };
            GameObject cubeSpline       = cubeNamed("bSpline");
            Vector3    onStartPosSpline = cubeSpline.transform.position;

            LeanTween.moveSplineLocal(cubeSpline, roundSpline, 0.5f).setOnComplete(() =>
            {
                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));
            });

            // Sequence test, do three tweens and make sure they end at the right points
            GameObject cubeSeq = cubeNamed("cSeq");
            var        seq     = LeanTween.sequence().append(LeanTween.moveX(cubeSeq, 100f, 0.2f));

            seq.append(0.1f).append(LeanTween.scaleX(cubeSeq, 2f, 0.1f));
            seq.append(() =>
            {
                LeanTest.expect(cubeSeq.transform.position.x == 100f, "SEQ MOVE X FINISHED", "move x:" + cubeSeq.transform.position.x);
                LeanTest.expect(cubeSeq.transform.localScale.x == 2f, "SEQ SCALE X FINISHED", "scale x:" + cubeSeq.transform.localScale.x);
            }).setScale(0.2f);

            // Bounds check
            GameObject cubeBounds    = cubeNamed("cBounds");
            bool       didPassBounds = true;
            Vector3    failPoint     = Vector3.zero;

            LeanTween.move(cubeBounds, new Vector3(10, 10, 10), 0.1f).setOnUpdate((float val) =>
            {
                //              Debug.LogWarning("cubeBounds x:"+cubeBounds.transform.position.x + " y:"+ cubeBounds.transform.position.y+" z:"+cubeBounds.transform.position.z);
                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)
                {
                    didPassBounds = false;
                    failPoint     = cubeBounds.transform.position;
                    //                  Debug.LogError("OUT OF BOUNDS");
                }
            }).setLoopPingPong().setRepeat(8).setOnComplete(() =>
            {
                LeanTest.expect(didPassBounds, "OUT OF BOUNDS", "pos x:" + failPoint.x + " y:" + failPoint.y + " z:" + failPoint.z);
            });

            // Local scale check
            //GameObject cubeLocal = cubeNamed("cLocal");
            //LeanTween.scale(cubeLocal, new Vector3(0.5f, 0.5f, 0.5f), 0.2f).setOnComplete(() =>
            //{
            //    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);
            //});

            // Groups of tweens testing
            groupTweens    = new LTDescr[1200];
            groupGOs       = new GameObject[groupTweens.Length];
            groupTweensCnt = 0;
            int descriptionMatchCount = 0;

            for (int i = 0; i < groupTweens.Length; i++)
            {
                GameObject cube = cubeNamed("c" + i);
                cube.transform.position = new Vector3(0, 0, i * 3);

                groupGOs[i] = cube;
            }

            yield return(new WaitForEndOfFrame());

            bool hasGroupTweensCheckStarted = false;
            int  setOnStartNum  = 0;
            int  setPosNum      = 0;
            bool setPosOnUpdate = true;

            for (int i = 0; i < groupTweens.Length; i++)
            {
                Vector3 finalPos = transform.position + Vector3.one * 3f;
                Dictionary <string, object> finalDict = new Dictionary <string, object> {
                    { "final", finalPos }, { "go", groupGOs[i] }
                };
                groupTweens[i] = LeanTween.move(groupGOs[i], finalPos, 3f).setOnStart(() =>
                {
                    setOnStartNum++;
                }).setOnUpdate((Vector3 newPosition) =>
                {
                    if (transform.position.z > newPosition.z)
                    {
                        setPosOnUpdate = false;
                    }
                    //                  Debug.LogWarning("New Position: " + newPosition.ToString());
                }).
                                 setOnCompleteParam(finalDict).
                                 setOnComplete((object param) =>
                {
                    Dictionary <string, object> finalDictRetr = param as Dictionary <string, object>;
                    Vector3 neededPos    = (Vector3)finalDictRetr["final"];
                    GameObject tweenedGo = finalDictRetr["go"] as GameObject;
                    if (neededPos.ToString() == tweenedGo.transform.position.ToString())
                    {
                        setPosNum++;
                    }
                    else
                    {
                        //                      Debug.Log("neededPos:"+neededPos+" tweenedGo.transform.position:"+tweenedGo.transform.position);
                    }
                    if (hasGroupTweensCheckStarted == false)
                    {
                        hasGroupTweensCheckStarted = true;
                        LeanTween.delayedCall(gameObject, 0.1f, () =>
                        {
                            LeanTest.expect(setOnStartNum == groupTweens.Length, "SETONSTART CALLS", "expected:" + groupTweens.Length + " was:" + setOnStartNum);
                            LeanTest.expect(groupTweensCnt == groupTweens.Length, "GROUP FINISH", "expected " + groupTweens.Length + " tweens but got " + groupTweensCnt);
                            LeanTest.expect(setPosNum == groupTweens.Length, "GROUP POSITION FINISH", "expected " + groupTweens.Length + " tweens but got " + setPosNum);
                            LeanTest.expect(setPosOnUpdate, "GROUP POSITION ON UPDATE");
                        });
                    }
                    groupTweensCnt++;
                });

                if (LeanTween.description(groupTweens[i].id).trans == groupTweens[i].trans)
                {
                    descriptionMatchCount++;
                }
            }

            while (LeanTween.tweensRunning < groupTweens.Length)
            {
                yield return(null);
            }

            LeanTest.expect(descriptionMatchCount == groupTweens.Length, "GROUP IDS MATCH");
            int expectedSearch = groupTweens.Length + 7;

            LeanTest.expect(LeanTween.maxSearch <= expectedSearch, "MAX SEARCH OPTIMIZED", "maxSearch:" + LeanTween.maxSearch + " should be:" + expectedSearch);
            LeanTest.expect(LeanTween.isTweening() == true, "SOMETHING IS TWEENING");

            // resume item before calling pause should continue item along it's way
            float previousXlt4 = cube4.transform.position.x;

            lt4 = LeanTween.moveX(cube4, 5.0f, 1.1f).setOnComplete(() =>
            {
                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));
            }).setDestroyOnComplete(true);
            lt4.resume();

            rotateRepeat = rotateRepeatAngle = 0;
            LeanTween.rotateAround(cube3, Vector3.forward, 360f, 0.1f).setRepeat(3).setOnComplete(rotateRepeatFinished).setOnCompleteOnRepeat(true).setDestroyOnComplete(true);
            yield return(new WaitForEndOfFrame());

            LeanTween.delayedCall(0.1f * 8f + 1f, rotateRepeatAllFinished);

            int countBeforeCancel = LeanTween.tweensRunning;

            LeanTween.cancel(lt1Id);
            LeanTest.expect(countBeforeCancel == LeanTween.tweensRunning, "CANCEL AFTER RESET SHOULD FAIL", "expected " + countBeforeCancel + " but got " + LeanTween.tweensRunning);
            LeanTween.cancel(cube2);

            int tweenCount = 0;

            for (int i = 0; i < groupTweens.Length; i++)
            {
                if (LeanTween.isTweening(groupGOs[i]))
                {
                    tweenCount++;
                }
                if (i % 3 == 0)
                {
                    LeanTween.pause(groupGOs[i]);
                }
                else if (i % 3 == 1)
                {
                    groupTweens[i].pause();
                }
                else
                {
                    LeanTween.pause(groupTweens[i].id);
                }
            }
            LeanTest.expect(tweenCount == groupTweens.Length, "GROUP ISTWEENING", "expected " + groupTweens.Length + " tweens but got " + tweenCount);

            yield return(new WaitForEndOfFrame());

            tweenCount = 0;
            for (int i = 0; i < groupTweens.Length; i++)
            {
                if (i % 3 == 0)
                {
                    LeanTween.resume(groupGOs[i]);
                }
                else if (i % 3 == 1)
                {
                    groupTweens[i].resume();
                }
                else
                {
                    LeanTween.resume(groupTweens[i].id);
                }

                if (i % 2 == 0 ? LeanTween.isTweening(groupTweens[i].id) : LeanTween.isTweening(groupGOs[i]))
                {
                    tweenCount++;
                }
            }
            LeanTest.expect(tweenCount == groupTweens.Length, "GROUP RESUME");

            LeanTest.expect(LeanTween.isTweening(cube1) == false, "CANCEL TWEEN LTDESCR");
            LeanTest.expect(LeanTween.isTweening(cube2) == false, "CANCEL TWEEN LEANTWEEN");

            LeanTest.expect(pauseCount == 0, "ON UPDATE NOT CALLED DURING PAUSE", "expect pause count of 0, but got " + pauseCount);


            yield return(new WaitForEndOfFrame());

            Time.timeScale = 0.25f;
            float tweenTime         = 0.2f;
            float expectedTime      = tweenTime * (1f / Time.timeScale);
            float start             = Time.realtimeSinceStartup;
            bool  onUpdateWasCalled = false;

            LeanTween.moveX(cube1, -5f, tweenTime).setOnUpdate((float val) =>
            {
                onUpdateWasCalled = true;
            }).setOnComplete(() =>
            {
                float end  = Time.realtimeSinceStartup;
                float diff = end - start;

                LeanTest.expect(Mathf.Abs(expectedTime - diff) < 0.06f, "SCALED TIMING DIFFERENCE", "expected to complete in roughly " + expectedTime + " but completed in " + diff);
                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);
                LeanTest.expect(onUpdateWasCalled, "ON UPDATE FIRED");
            });

            bool didGetCorrectOnUpdate = false;

            LeanTween.value(gameObject, new Vector3(1f, 1f, 1f), new Vector3(10f, 10f, 10f), 1f).setOnUpdate((Vector3 val) =>
            {
                didGetCorrectOnUpdate = val.x >= 1f && val.y >= 1f && val.z >= 1f;
            }).setOnComplete(() =>
            {
                LeanTest.expect(didGetCorrectOnUpdate, "VECTOR3 CALLBACK CALLED");
            });

            yield return(new WaitForSeconds(expectedTime));

            Time.timeScale = 1f;

            int ltCount = 0;

            GameObject[] allGos = FindObjectsOfType(typeof(GameObject)) as GameObject[];
            foreach (GameObject go in allGos)
            {
                if (go.name == "~LeanTween")
                {
                    ltCount++;
                }
            }
            LeanTest.expect(ltCount == 1, "RESET CORRECTLY CLEANS UP");

            StartCoroutine(lotsOfCancels());
        }