Exemplo n.º 1
0
        static void ExtractCurve()
        {
            GameObject obj = UnityEditor.Selection.activeGameObject;

            if (obj == null)
            {
                return;
            }

            // Find old extractive data.
            AnimCurve crvDt = obj.GetComponent <AnimCurve>();

            // Add new data.
            if (crvDt == null)
            {
                crvDt = obj.AddComponent <AnimCurve>();
            }

            // Reset the data.
            crvDt.Reset();

            // Extract animation clips.
#if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9
            AnimationClip[] clips = UnityEditor.AnimationUtility.GetAnimationClips(obj);
#else
            AnimationClip[] clips = UnityEditor.AnimationUtility.GetAnimationClips(obj);
#endif

            foreach (AnimationClip clip in clips)
            {
                UnityEditor.EditorCurveBinding[] curves = UnityEditor.AnimationUtility.GetCurveBindings(clip);

                //UnityEditor.AnimationClipCurveData[] curves = UnityEditor.AnimationUtility.GetAllCurves(clip);


                // If has no curves, not extract the clip.
                if (curves == null)
                {
                    continue;
                }

                // Add this clip.
                crvDt.AddClip(clip.name, clip.wrapMode, clip.frameRate, clip.length);

                // Add curves.
                foreach (UnityEditor.EditorCurveBinding crv in curves)
                {
                    //public void AddCurve(string clipName, string path, string type, string propertyName, AnimationCurve curve)

                    AnimationCurve cuve = UnityEditor.AnimationUtility.GetEditorCurve(clip, crv);
                    crvDt.AddCurve(clip.name, crv.path, crv.type.AssemblyQualifiedName, crv.propertyName, cuve);
                }

                // Add events.
                foreach (AnimationEvent evn in UnityEditor.AnimationUtility.GetAnimationEvents(clip))
                {
                    crvDt.AddEvent(clip.name, evn);
                }
            }
        }
Exemplo n.º 2
0
        // Add curve to one game object.
        public bool AddCrvClipToObj(GameObject obj, string clipName, Dictionary <string, float> valRange, float timeScale)
        {
            // Find animation dstAnimClip data.
            AnimClip srcAnimClip = FindClip(clipName);

            if (srcAnimClip == null)
            {
                return(false);
            }

            // Get animation from the target object.
            Animation dstAnim = obj.GetComponent <Animation>();

            if (dstAnim == null)
            {
                dstAnim = obj.AddComponent <Animation>();
            }

            AnimCurve dstAnimCurve = obj.GetComponent <AnimCurve>();

            if (dstAnimCurve == null)
            {
                dstAnimCurve = obj.AddComponent <AnimCurve>();
            }

            // Create one animation dstAnimClip for this dstAnimClip data.
            AnimationClip dstAnimClip = new AnimationClip();

            dstAnimClip.name = srcAnimClip.name;

            // Add curve.
            foreach (Curve srcCurve in srcAnimClip.curves)
            {
                Keyframe[] keys = srcCurve.curve.keys;

                float startTime = -1;
                float endTime   = -1;
                float startVal  = srcCurve.minVal;
                float endVal    = srcCurve.maxVal;

                // Get start/end value
                foreach (Event e in srcAnimClip.events)
                {
                    if (e.functionName == "TranslateStart")
                    {
                        foreach (Keyframe key in keys)
                        {
                            if (Mathf.Approximately(key.time, e.time))
                            {
                                startTime = key.time;
                                startVal  = key.value;
                                break;
                            }
                        }
                    }
                    else if (e.functionName == "TranslateEnd")
                    {
                        foreach (Keyframe key in keys)
                        {
                            if (Mathf.Approximately(key.time, e.time))
                            {
                                endTime = key.time;
                                endVal  = key.value;
                                break;
                            }
                        }
                    }
                }

                float curRange = endVal - startVal;

                if (curRange != 0)
                {
                    float range = curRange;

                    foreach (KeyValuePair <string, float> kvp in valRange)
                    {
                        if (kvp.Key == srcCurve.propertyName)
                        {
                            range = kvp.Value;
                            break;
                        }
                    }

                    // Scale value.
                    if (range != curRange)
                    {
                        float valueScale     = range / curRange;
                        float lastValueDelta = 0;
                        float lastTimeDelta  = 0;

                        for (int i = 0; i < keys.Length; i++)
                        {
                            if (startTime >= 0 && keys[i].time <= startTime)
                            {
                                continue;
                            }

                            if (endTime >= 0 && keys[i].time > endTime)
                            {
                                keys[i].value += lastValueDelta;
                                keys[i].time  += lastTimeDelta;
                            }
                            else
                            {
                                float _value = keys[i].value;
                                keys[i].value *= valueScale;
                                lastValueDelta = keys[i].value - _value;

                                float _time = keys[i].time;
                                keys[i].time  = startTime + (keys[i].time - startTime) / timeScale;
                                lastTimeDelta = keys[i].time - _time;
                            }
                        }
                    }
                }
                else
                {
                    continue;
                    //Debug.LogWarning(string.Format("Can not scale curve {0} in {1}", srcCurve.propertyName, clipName));
                }

                dstAnimClip.SetCurve(srcCurve.path, System.Type.GetType(srcCurve.type), srcCurve.propertyName, new AnimationCurve(keys));
            }

            // Add events.
            foreach (Event e in srcAnimClip.events)
            {
                if (e.functionName == "TranslateStart" || e.functionName == "TranslateEnd")
                {
                    continue;
                }

                AnimationEvent animationEvent = e.ToAnimEvent();

                animationEvent.time = e.time / timeScale;

                dstAnimClip.AddEvent(animationEvent);
            }

            // Add this dstAnimClip.
            if (dstAnim[dstAnimClip.name] != null)
            {
                dstAnim.RemoveClip(dstAnimClip.name);
            }

            dstAnim.AddClip(dstAnimClip, dstAnimClip.name);

            return(true);
        }
Exemplo n.º 3
0
    //private KodGames.AnimCurve animCurve;

    public DelayAnimCurveTranslateEnd(WeihuaGames.AnimCurve animCurve, DelayDelegate delayDelegate, object userData0, object userData1)
        : base(delayDelegate, userData0, userData1)
    {
        reachEnd = false;
        animCurve.SetTranslateEndDelegate(OnTranslateEnd, null);
    }