Exemplo n.º 1
0
    static public void MoveTo(GameObject target, Vector3 position, float time)
    {
        Vector3 fromPos = target.transform.localPosition;

        Animation animation = target.GetComponent <Animation>();

        if (animation == null)
        {
            animation = target.AddComponent <Animation>();
        }
        AnimationClip clip = new AnimationClip();
//        clip.wrapMode = WrapMode.;
        AnimationCurve animCurveX = AnimationCurve.Linear(0f, fromPos.x, time, position.x);
        AnimationCurve animCurveY = AnimationCurve.Linear(0f, fromPos.y, time, position.y);
        AnimationCurve animCurveZ = AnimationCurve.Linear(0f, fromPos.z, time, position.z);

        clip.SetCurve("", typeof(Transform), "localPosition.x", animCurveX);
        clip.SetCurve("", typeof(Transform), "localPosition.y", animCurveY);
        clip.SetCurve("", typeof(Transform), "localPosition.z", animCurveZ);

        ViNoAnimationListener animcb = target.GetComponent <ViNoAnimationListener>();

        if (animcb == null)
        {
            target.AddComponent <ViNoAnimationListener>();
        }

        AnimationEvent tweenFinished = new AnimationEvent();

        tweenFinished.time            = time;
        tweenFinished.intParameter    = 123;
        tweenFinished.stringParameter = "end";
        tweenFinished.functionName    = "AnimationFinishedCallback";

        clip.AddEvent(tweenFinished);

        animation.AddClip(clip, target.name);

        // Now, Start the Animation.
        animation.Play(target.name);
    }
Exemplo n.º 2
0
    protected void AnimationMove(TweenOperandData tweenData)
    {
        Hashtable paramHash = tweenData.paramTable;
        string    name      = ContainsKey_name(ref paramHash);

        GameObject root = tweenData.tweenTarget;

        if (root == null)
        {
            ViNoDebugger.LogError("tween target not found !");
            return;
        }

        Transform rootra = root.transform;

        if (string.IsNullOrEmpty(name))
        {
            name = "__animation";            // default animation name.
        }

        Animation animation = root.GetComponent <Animation>();

        if (animation == null)
        {
            animation = root.AddComponent <Animation>();
        }
        AnimationClip clip        = new AnimationClip();
        float         duration    = ContainsKey_duration(ref paramHash);
        bool          isMove      = false;
        bool          isRotate    = false;
        bool          isScale     = false;
        bool          isFromPos   = false;
        bool          isFromRot   = false;
        bool          isFromScale = false;
        WrapMode      wrapmode    = ContainsKey_mode(ref paramHash);
        int           method      = 0;

        if (paramHash.ContainsKey("method"))
        {
            string methodStr = paramHash["method"] as string;
            method = int.Parse(methodStr);
        }
        Vector3 move      = ContainsKey_moveXorYorZ(out isMove, ref paramHash, rootra.localPosition);
        Vector3 euler     = ContainsKey_rotateXorYorZ(out isRotate, ref paramHash, rootra.localEulerAngles);        //localRotation.eulerAngles );
        Vector3 scale     = ContainsKey_scaleXorYorZ(out isScale, ref paramHash, rootra.localScale);
        Vector3 fromPos   = ContainsKey_StrAndXorYorZ(out isFromPos, "fromPos", ref paramHash, rootra.localPosition);
        Vector3 fromRot   = ContainsKey_StrAndXorYorZ(out isFromRot, "fromRot", ref paramHash, rootra.localEulerAngles);
        Vector3 fromScale = ContainsKey_StrAndXorYorZ(out isFromScale, "fromScale", ref paramHash, rootra.localScale);

        clip.wrapMode = wrapmode;

        if (isMove)
        {
            switch (method)
            {
            case 0:                      // Linear.
                AnimationCurve animCurveX = AnimationCurve.Linear(0f, fromPos.x, duration, move.x);
                AnimationCurve animCurveY = AnimationCurve.Linear(0f, fromPos.y, duration, move.y);
                AnimationCurve animCurveZ = AnimationCurve.Linear(0f, fromPos.z, duration, move.z);
                clip.SetCurve("", typeof(Transform), "localPosition.x", animCurveX);
                clip.SetCurve("", typeof(Transform), "localPosition.y", animCurveY);
                clip.SetCurve("", typeof(Transform), "localPosition.z", animCurveZ);
                break;

            case 1:                     // Easeinout.
                animCurveX = AnimationCurve.EaseInOut(0f, fromPos.x, duration, move.x);
                animCurveY = AnimationCurve.EaseInOut(0f, fromPos.y, duration, move.y);
                animCurveZ = AnimationCurve.EaseInOut(0f, fromPos.z, duration, move.z);
                clip.SetCurve("", typeof(Transform), "localPosition.x", animCurveX);
                clip.SetCurve("", typeof(Transform), "localPosition.y", animCurveY);
                clip.SetCurve("", typeof(Transform), "localPosition.z", animCurveZ);
                break;
            }
        }

        if (isRotate)
        {
            Quaternion q1 = Quaternion.Euler(fromRot);
            Quaternion q2 = Quaternion.Euler(euler);

            switch (method)
            {
            case 0:                      // Linear.
                AnimationCurve animCurveX = AnimationCurve.Linear(0f, q1.x, duration, q2.x);
                AnimationCurve animCurveY = AnimationCurve.Linear(0f, q1.y, duration, q2.y);
                AnimationCurve animCurveZ = AnimationCurve.Linear(0f, q1.z, duration, q2.z);
                AnimationCurve animCurveW = AnimationCurve.Linear(0f, q1.w, duration, q2.w);

                clip.SetCurve("", typeof(Transform), "localRotation.x", animCurveX);
                clip.SetCurve("", typeof(Transform), "localRotation.y", animCurveY);
                clip.SetCurve("", typeof(Transform), "localRotation.z", animCurveZ);
                clip.SetCurve("", typeof(Transform), "localRotation.w", animCurveW);
                break;

            case 1:                     // Easeinout.
                animCurveX = AnimationCurve.EaseInOut(0f, q1.x, duration, q2.x);
                animCurveY = AnimationCurve.EaseInOut(0f, q1.y, duration, q2.y);
                animCurveZ = AnimationCurve.EaseInOut(0f, q1.z, duration, q2.z);
                animCurveW = AnimationCurve.EaseInOut(0f, q1.w, duration, q2.w);

                clip.SetCurve("", typeof(Transform), "localRotation.x", animCurveX);
                clip.SetCurve("", typeof(Transform), "localRotation.y", animCurveY);
                clip.SetCurve("", typeof(Transform), "localRotation.z", animCurveZ);
                clip.SetCurve("", typeof(Transform), "localRotation.w", animCurveW);
                break;
            }
        }

        if (isScale)
        {
            switch (method)
            {
            case 0:                      // Linear.
                AnimationCurve animCurveX = AnimationCurve.Linear(0f, fromScale.x, duration, scale.x);
                AnimationCurve animCurveY = AnimationCurve.Linear(0f, fromScale.y, duration, scale.y);
                AnimationCurve animCurveZ = AnimationCurve.Linear(0f, fromScale.z, duration, scale.z);
                clip.SetCurve("", typeof(Transform), "localScale.x", animCurveX);
                clip.SetCurve("", typeof(Transform), "localScale.y", animCurveY);
                clip.SetCurve("", typeof(Transform), "localScale.z", animCurveZ);
                break;

            case 1:                     // Easeinout.
                animCurveX = AnimationCurve.EaseInOut(0f, fromScale.x, duration, scale.x);
                animCurveY = AnimationCurve.EaseInOut(0f, fromScale.y, duration, scale.y);
                animCurveZ = AnimationCurve.EaseInOut(0f, fromScale.z, duration, scale.z);
                clip.SetCurve("", typeof(Transform), "localScale.x", animCurveX);
                clip.SetCurve("", typeof(Transform), "localScale.y", animCurveY);
                clip.SetCurve("", typeof(Transform), "localScale.z", animCurveZ);
                break;
            }
        }

        ViNoAnimationListener animcb = root.GetComponent <ViNoAnimationListener>();

        if (animcb == null)
        {
            root.AddComponent <ViNoAnimationListener>();
        }

        AnimationEvent tweenFinished = new AnimationEvent();

        tweenFinished.time            = duration;
        tweenFinished.intParameter    = 123;
        tweenFinished.stringParameter = "end";
        tweenFinished.functionName    = "AnimationFinishedCallback";

        clip.AddEvent(tweenFinished);

        animation.AddClip(clip, name);

        // Now, Start the Animation.
        animation.Play(name);

        // Is  paramHash Contains Key "sendDelay" ? .
        ContainsKey_sendDelayAndStartCoroutine(ref paramHash);
    }