예제 #1
0
        public bool ResetTimeAnimation()
        {
            if (m_sceneCache.sceneCount < 2)
            {
                return(false);
            }

            var           animator = Misc.GetOrAddComponent <Animator>(gameObject);
            AnimationClip clip     = null;

            if (animator.runtimeAnimatorController != null)
            {
                var clips = animator.runtimeAnimatorController.animationClips;
                if (clips != null && clips.Length > 0)
                {
                    var tmp = animator.runtimeAnimatorController.animationClips[0];
                    if (tmp != null)
                    {
                        clip = tmp;
                        Undo.RegisterCompleteObjectUndo(clip, "SceneCachePlayer");
                    }
                }
            }

            if (clip == null)
            {
                clip = new AnimationClip();

                var animPath       = string.Format("{0}/{1}.anim", assetPath, gameObject.name);
                var controllerPath = string.Format("{0}/{1}.controller", assetPath, gameObject.name);
                clip = Misc.SaveAsset(clip, animPath);
                if (clip == null)
                {
                    return(false);
                }

                animator.runtimeAnimatorController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPathWithClip(controllerPath, clip);
            }
            var sampleRate = m_sceneCache.sampleRate;

            if (sampleRate > 0.0f)
            {
                clip.frameRate = sampleRate;
            }

            var tPlayer = typeof(SceneCachePlayer);

            clip.SetCurve("", tPlayer, "m_time", null);
            clip.SetCurve("", tPlayer, "m_frame", null);
            if (m_timeUnit == TimeUnit.Seconds)
            {
                var curve = m_sceneCache.GetTimeCurve(InterpolationMode.Constant);
                clip.SetCurve("", tPlayer, "m_time", curve);
            }
            else if (m_timeUnit == TimeUnit.Frames)
            {
                var curve = m_sceneCache.GetFrameCurve((int)m_baseFrame);
                clip.SetCurve("", tPlayer, "m_frame", curve);
            }

            AssetDatabase.SaveAssets();
            UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
            return(true);
        }