Beispiel #1
0
        public static AnimatorStateTransition GetTransitionTo(FPlayAnimationEvent animEvt)
        {
            FAnimationTrack animTrack = (FAnimationTrack)animEvt.Track;

            if (animTrack.AnimatorController == null)
            {
                return(null);
            }

            AnimatorController controller = (AnimatorController)animTrack.AnimatorController;

            AnimatorState animState = null;

            AnimatorControllerLayer layer = FindLayer(controller, ((FAnimationTrack)animEvt.Track).LayerName);

            if (layer == null)
            {
                return(null);
            }

            if (layer.stateMachine.stateMachines.Length == 0)
            {
                return(null);
            }

            ChildAnimatorStateMachine fluxSM = layer.stateMachine.stateMachines[0];

            AnimatorStateMachine stateMachine = fluxSM.stateMachine;

            for (int i = 0; i != stateMachine.states.Length; ++i)
            {
                if (stateMachine.states[i].state.nameHash == animEvt._stateHash)
                {
                    animState = stateMachine.states[i].state;
                    break;
                }
            }

            if (animState == null)
            {
                //				Debug.LogError("Couldn't find state " + animEvt._animationClip );
                return(null);
            }

            for (int i = 0; i != stateMachine.states.Length; ++i)
            {
                AnimatorState state = stateMachine.states[i].state;

                AnimatorStateTransition[] transitions = state.transitions;
                for (int j = 0; j != transitions.Length; ++j)
                {
                    if (transitions[j].destinationState == animState)
                    {
                        return(transitions[j]);
                    }
                }
            }

            return(null);
        }
        public override void OnEnable()
        {
            base.OnEnable();

            _animTrack          = (FAnimationTrack)target;
            _animatorController = serializedObject.FindProperty("_animatorController");
        }
        public static AnimatorController CreateAnimatorController(FAnimationTrack animTrack)
        {
            string defaultFolder   = "Assets/";
            string defaultFileName = string.Format("{0}_{1}.controller", animTrack.Timeline.Sequence.name, animTrack.Owner.name);
            string defaultFilePath = AssetDatabase.GenerateUniqueAssetPath(defaultFolder + defaultFileName);

            defaultFileName = System.IO.Path.GetFileNameWithoutExtension(defaultFilePath);

            string filePath = EditorUtility.SaveFilePanel("Create new AnimatorController...", defaultFolder, defaultFileName, "controller");

            if (string.IsNullOrEmpty(filePath))
            {
                return(null);
            }

            // transform the path into a local path
            if (!filePath.StartsWith(Application.dataPath))
            {
                Debug.LogError("Cannot save controller outside of the project");
                return(null);
            }

            string fileLocalPath = "Assets" + filePath.Substring(Application.dataPath.Length, filePath.Length - Application.dataPath.Length);

//			AnimatorController animatorAtPath = (AnimatorController)AssetDatabase.LoadAssetAtPath( fileLocalPath, typeof(AnimatorController) );

            return(AnimatorController.CreateAnimatorControllerAtPath(fileLocalPath));
        }
        public override void OnTrackChanged()
        {
            FAnimationTrack animTrack = (FAnimationTrack)Track;

            FAnimationTrackInspector.RebuildStateMachine(animTrack);
            FAnimationTrackInspector.SetAnimator(animTrack);
        }
        protected override void Init(FObject obj)
        {
            base.Init(obj);

            _animEvt = (FPlayAnimationEvent)_evt;

            _animTrack = (FAnimationTrack)_animEvt.GetTrack();
        }
        public override void Init(FObject obj, FEditor owner)
        {
            base.Init(obj, owner);

            FAnimationTrack animTrack = (FAnimationTrack)Obj;

            if (animTrack.Owner.GetComponent <Animator>() == null)
            {
                Animator animator = animTrack.Owner.gameObject.AddComponent <Animator>();
                Undo.RegisterCreatedObjectUndo(animator, string.Empty);
            }
        }
        public override void OnEnable()
        {
            base.OnEnable();

            if (target == null)
            {
                DestroyImmediate(this);
                return;
            }
            _animTrack          = (FAnimationTrack)target;
            _animatorController = serializedObject.FindProperty("_animatorController");
            _layerName          = serializedObject.FindProperty("_layerName");
            _layerId            = serializedObject.FindProperty("_layerId");
        }
        protected override void OnDestroy()
        {
            base.OnDestroy();

            //SceneView.onSceneGUIDelegate -= OnSceneGUI;

            FAnimationTrack animTrack = (FAnimationTrack)Obj;

            DestroyImmediate(animTrack.AnimatorController);
            animTrack.AnimatorController = null;
            Resources.UnloadUnusedAssets();

            //if (SyncWithAnimationWindow)
            //    SyncWithAnimationWindow = false;
        }
Beispiel #9
0
 public static void SetAnimator(FAnimationTrack animTrack)
 {
     if (animTrack.Owner.GetComponent <Animator>() == null)
     {
         Animator animator = animTrack.Owner.gameObject.AddComponent <Animator>();
         Undo.RegisterCreatedObjectUndo(animator, string.Empty);
     }
     if (animTrack.AnimatorController == null)
     {
         var temp = FUtility.GetFluxAssets <UnityEditor.Animations.AnimatorController>("EditorAnimatorController.controller");
         var animatorController = Instantiate(temp);
         animTrack.LayerId   = 0;
         animTrack.LayerName = "Base Layer";
         var layers = animatorController.layers;
         layers[animTrack.LayerId].name = animTrack.LayerName;
         layers[animTrack.LayerId].stateMachine.name = animTrack.LayerName;
         animatorController.layers    = layers;
         animTrack.AnimatorController = animatorController;
         FAnimationTrackInspector.RebuildStateMachine(animTrack);
     }
 }
Beispiel #10
0
//		public static EditorWindow GetAnimationWindow( bool showWindow )
//		{
//			System.Type t = typeof(EditorWindow).Assembly.GetType("UnityEditor.AnimationWindow");
//			if( showWindow )
//			{
//				return EditorWindow.GetWindow( t );
//			}
//
//			System.Reflection.MethodInfo getWindow = typeof(EditorWindow).GetMethod( "GetWindowDontShow", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static  );
//			System.Reflection.MethodInfo getWindowGeneric = getWindow.MakeGenericMethod( t );
//			return (EditorWindow)getWindowGeneric.Invoke(null, null);
//		}

        #endregion

        #region Upgrade Sequences

        public static void Upgrade(FSequence sequence)
        {
            const int fluxVersion = Flux.FUtility.FLUX_VERSION;

            if (sequence.Version == fluxVersion)
            {
                return;
            }

            Debug.LogFormat("Upgrading sequence '{0}' from version '{1}' to '{2}'", sequence.name, sequence.Version, fluxVersion);

            // is it before 2.0.0 release?
            if (sequence.Version < 200)
            {
                // set TimelineContainer as Content
                Transform timelineContainer = null;
                foreach (Transform t in sequence.transform)
                {
                    if (t.name == "Timeline Container")
                    {
                        timelineContainer = t;
                        break;
                    }
                }

                if (timelineContainer == null)
                {
                    timelineContainer            = new GameObject("SequenceContent").transform;
                    timelineContainer.hideFlags |= HideFlags.HideInHierarchy;
                }

                sequence.Content = timelineContainer;

                // create a container, and add it to the sequence

                FContainer container = FContainer.Create(FContainer.DEFAULT_COLOR);
                sequence.Add(container);

                FTimeline[] timelines = timelineContainer.GetComponentsInChildren <FTimeline>();
                foreach (FTimeline timeline in timelines)
                {
                    container.Add(timeline);
                    List <FTrack> tracks = timeline.Tracks;

                    foreach (FTrack track in tracks)
                    {
                        if (track is FAnimationTrack)
                        {
                            FAnimationTrack animTrack = (FAnimationTrack)track;
                            if (animTrack.AnimatorController != null)
                            {
                                FAnimationTrackInspector inspector  = (FAnimationTrackInspector)FAnimationTrackInspector.CreateEditor(animTrack);
                                AnimatorController       controller = (AnimatorController)animTrack.AnimatorController;
                                inspector.UpdateLayer(controller == null || controller.layers.Length == 0 ? null : controller.layers[0]);
                                inspector.serializedObject.ApplyModifiedProperties();
                                if (controller.layers.Length > 0)
                                {
                                    while (controller.layers[0].stateMachine.stateMachines.Length > 0)
                                    {
                                        controller.layers[0].stateMachine.RemoveStateMachine(controller.layers[0].stateMachine.stateMachines[controller.layers[0].stateMachine.stateMachines.Length - 1].stateMachine);
                                    }
                                    while (controller.layers[0].stateMachine.states.Length > 0)
                                    {
                                        controller.layers[0].stateMachine.RemoveState(controller.layers[0].stateMachine.states[controller.layers[0].stateMachine.states.Length - 1].state);
                                    }
                                }
                                Editor.DestroyImmediate(inspector);
                                FAnimationTrackInspector.RebuildStateMachine(animTrack);
                            }
                        }

                        track.CacheMode = track.RequiredCacheMode;
                    }
                    //					foreach( FTrack track in tracks )
                    //					{
                    //						track.CacheType = track.DefaultCacheType();
                    //					}
                }
            }

            sequence.Version = fluxVersion;
        }
Beispiel #11
0
        public override void OnInspectorGUI()
        {
            bool rebuildTrack = false;

            base.OnInspectorGUI();

            serializedObject.Update();

            if (_animationClip.objectReferenceValue == null)
            {
                EditorGUILayout.HelpBox("There's no Animation Clip", MessageType.Warning);
                Rect helpBoxRect = GUILayoutUtility.GetLastRect();

                float yCenter = helpBoxRect.center.y;

                helpBoxRect.xMax  -= 3;
                helpBoxRect.xMin   = helpBoxRect.xMax - 50;
                helpBoxRect.yMin   = yCenter - 12.5f;
                helpBoxRect.height = 25f;

                FAnimationTrack animTrack = (FAnimationTrack)_animEvent.Track;

                if (animTrack.AnimatorController == null || animTrack.LayerId == -1)
                {
                    GUI.enabled = false;
                }

                if (GUI.Button(helpBoxRect, "Create"))
                {
                    CreateAnimationClip(_animEvent);
                }

                GUI.enabled = true;
            }

            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(_animationClip);

            Rect animationClipRect = GUILayoutUtility.GetLastRect();

            if (Event.current.type == EventType.DragUpdated && animationClipRect.Contains(Event.current.mousePosition))
            {
                int numAnimations = NumAnimationsDragAndDrop(_animEvent.Sequence.FrameRate);
                if (numAnimations > 0)
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Link;
                }
            }
            else if (Event.current.type == EventType.DragPerform && animationClipRect.Contains(Event.current.mousePosition))
            {
                if (NumAnimationsDragAndDrop(_animEvent.Sequence.FrameRate) > 0)
                {
                    DragAndDrop.AcceptDrag();
                    AnimationClip clip = GetAnimationClipDragAndDrop(_animEvent.Sequence.FrameRate);
                    if (clip != null)
                    {
                        _animationClip.objectReferenceValue = clip;
                    }
                }
            }

            if (EditorGUI.EndChangeCheck())
            {
                AnimationClip clip = (AnimationClip)_animationClip.objectReferenceValue;
                if (clip)
                {
                    if (clip.frameRate != _animEvent.Track.Timeline.Sequence.FrameRate)
                    {
                        Debug.LogError(string.Format("Can't add animation, it has a different frame rate from the sequence ({0} vs {1})",
                                                     clip.frameRate, _animEvent.Track.Timeline.Sequence.FrameRate));

                        _animationClip.objectReferenceValue = null;
                    }
                    else
                    {
                        SerializedProperty frameRangeEnd = _frameRange.FindPropertyRelative("_end");
                        FrameRange         maxFrameRange = _animEvent.GetMaxFrameRange();
                        frameRangeEnd.intValue = Mathf.Min(_animEvent.FrameRange.Start + Mathf.RoundToInt(clip.length * clip.frameRate), maxFrameRange.End);
                    }

                    rebuildTrack = true;
                }
                else
                {
                    CheckDeleteAnimation(_animEvent);
                }
            }

            bool isAnimationEditable = Flux.FUtility.IsAnimationEditable(_animEvent._animationClip);

            if (isAnimationEditable)
            {
                EditorGUILayout.PropertyField(_controlsAnimation);
            }
            else
            {
                if (_controlsAnimation.boolValue)
                {
                    _controlsAnimation.boolValue = false;
                }
            }

            if (!_controlsAnimation.boolValue)
            {
                if (_animEvent.IsBlending())
                {
                    float offset      = _startOffset.intValue / _animEvent._animationClip.frameRate;
                    float blendFinish = offset + (_blendLength.intValue / _animEvent._animationClip.frameRate);

                    EditorGUILayout.BeginHorizontal();

                    EditorGUILayout.BeginHorizontal(GUILayout.Width(EditorGUIUtility.labelWidth - 5));                     // hack to get around some layout issue with imgui
                    _showBlendAndOffsetContent = EditorGUILayout.Foldout(_showBlendAndOffsetContent, new GUIContent("Offset+Blend"));
                    EditorGUILayout.EndHorizontal();

                    EditorGUI.BeginChangeCheck();
                    EditorGUILayout.MinMaxSlider(ref offset, ref blendFinish, 0, _animEvent.GetMaxStartOffset() / _animEvent._animationClip.frameRate + _animEvent.LengthTime);
                    if (EditorGUI.EndChangeCheck())
                    {
                        _startOffset.intValue = Mathf.Clamp(Mathf.RoundToInt(offset * _animEvent.Sequence.FrameRate), 0, _animEvent.GetMaxStartOffset());
                        _blendLength.intValue = Mathf.Clamp(Mathf.RoundToInt((blendFinish - offset) * _animEvent.Sequence.FrameRate), 0, _animEvent.Length);
                        rebuildTrack          = true;
                    }

                    EditorGUILayout.EndHorizontal();

                    if (_showBlendAndOffsetContent)
                    {
                        EditorGUI.BeginChangeCheck();
                        ++EditorGUI.indentLevel;
                        if (!isAnimationEditable)
                        {
                            EditorGUILayout.IntSlider(_startOffset, 0, _animEvent.GetMaxStartOffset());
                        }

                        //			if( _animEvent.IsBlending() )
                        {
                            EditorGUILayout.IntSlider(_blendLength, 0, _animEvent.Length);
                        }
                        --EditorGUI.indentLevel;
                        if (EditorGUI.EndChangeCheck())
                        {
                            rebuildTrack = true;
                        }
                    }
                }
                else
                {
                    EditorGUI.BeginChangeCheck();
                    EditorGUILayout.IntSlider(_startOffset, 0, _animEvent.GetMaxStartOffset());
                    if (EditorGUI.EndChangeCheck())
                    {
                        rebuildTrack = true;
                    }
                }
            }

            serializedObject.ApplyModifiedProperties();

            if (rebuildTrack)
            {
                FAnimationTrackInspector.RebuildStateMachine((FAnimationTrack)_animEvent.Track);
            }
        }
Beispiel #12
0
        public static AnimationClip CreateAnimationClip(FPlayAnimationEvent animEvent)
        {
            FAnimationTrack track = (FAnimationTrack)animEvent.Track;

            string animName = ((FAnimationTrack)animEvent.Track).LayerName + "_" + animEvent.GetId().ToString();

            AnimationClip clip = AnimatorController.AllocateAnimatorClip(animName);

            clip.frameRate = animEvent.Sequence.FrameRate;

            Transform ownerTransform = animEvent.Owner;

            Vector3 pos = ownerTransform.localPosition;

            Vector3 rot = ownerTransform.localRotation.eulerAngles;             //ownerTransform.localEulerAngles;

            Keyframe[] xPosKeys = new Keyframe[] { new Keyframe(0, pos.x), new Keyframe(animEvent.LengthTime, pos.x) };
            Keyframe[] yPosKeys = new Keyframe[] { new Keyframe(0, pos.y), new Keyframe(animEvent.LengthTime, pos.y) };
            Keyframe[] zPosKeys = new Keyframe[] { new Keyframe(0, pos.z), new Keyframe(animEvent.LengthTime, pos.z) };

            Keyframe[] xRotKeys = new Keyframe[] { new Keyframe(0, rot.x), new Keyframe(animEvent.LengthTime, rot.x) };
            Keyframe[] yRotKeys = new Keyframe[] { new Keyframe(0, rot.y), new Keyframe(animEvent.LengthTime, rot.y) };
            Keyframe[] zRotKeys = new Keyframe[] { new Keyframe(0, rot.z), new Keyframe(animEvent.LengthTime, rot.z) };

            // set the tangent mode
            int tangentMode = 10;             // 10 is unity auto tangent mode

            for (int i = 0; i != xPosKeys.Length; ++i)
            {
                xPosKeys[i].tangentMode = tangentMode;
                yPosKeys[i].tangentMode = tangentMode;
                zPosKeys[i].tangentMode = tangentMode;

                xRotKeys[i].tangentMode = tangentMode;
                yRotKeys[i].tangentMode = tangentMode;
                zRotKeys[i].tangentMode = tangentMode;
            }

            AnimationCurve xPos = new AnimationCurve(xPosKeys);
            AnimationCurve yPos = new AnimationCurve(yPosKeys);
            AnimationCurve zPos = new AnimationCurve(zPosKeys);

            AnimationCurve xRot = new AnimationCurve(xRotKeys);
            AnimationCurve yRot = new AnimationCurve(yRotKeys);
            AnimationCurve zRot = new AnimationCurve(zRotKeys);

            AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(string.Empty, typeof(Transform), "m_LocalPosition.x"), xPos);
            AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(string.Empty, typeof(Transform), "m_LocalPosition.y"), yPos);
            AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(string.Empty, typeof(Transform), "m_LocalPosition.z"), zPos);

            // workaround unity bug of spilling errors if you just set localEulerAngles
            Quaternion quat = ownerTransform.localRotation;

            Keyframe[]     xQuatKeys = new Keyframe[] { new Keyframe(0, quat.x), new Keyframe(animEvent.LengthTime, quat.x) };
            Keyframe[]     yQuatKeys = new Keyframe[] { new Keyframe(0, quat.y), new Keyframe(animEvent.LengthTime, quat.y) };
            Keyframe[]     zQuatKeys = new Keyframe[] { new Keyframe(0, quat.z), new Keyframe(animEvent.LengthTime, quat.z) };
            Keyframe[]     wQuatKeys = new Keyframe[] { new Keyframe(0, quat.w), new Keyframe(animEvent.LengthTime, quat.w) };
            AnimationCurve xQuat     = new AnimationCurve(xQuatKeys);
            AnimationCurve yQuat     = new AnimationCurve(yQuatKeys);
            AnimationCurve zQuat     = new AnimationCurve(zQuatKeys);
            AnimationCurve wQuat     = new AnimationCurve(wQuatKeys);

            AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(string.Empty, typeof(Transform), "m_LocalRotation.x"), xQuat);
            AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(string.Empty, typeof(Transform), "m_LocalRotation.y"), yQuat);
            AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(string.Empty, typeof(Transform), "m_LocalRotation.z"), zQuat);
            AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(string.Empty, typeof(Transform), "m_LocalRotation.w"), wQuat);

            AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(string.Empty, typeof(Transform), "localEulerAngles.x"), xRot);
            AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(string.Empty, typeof(Transform), "localEulerAngles.y"), yRot);
            AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.FloatCurve(string.Empty, typeof(Transform), "localEulerAngles.z"), zRot);

            clip.EnsureQuaternionContinuity();

            AssetDatabase.AddObjectToAsset(clip, track.AnimatorController);

            AnimationClipSettings clipSettings = AnimationUtility.GetAnimationClipSettings(clip);

            clipSettings.loopTime = false;
            AnimationUtility.SetAnimationClipSettings(clip, clipSettings);

            AssetDatabase.SaveAssets();

            FAnimationEventInspector.SetAnimationClip(animEvent, clip);

            return(clip);
        }
Beispiel #13
0
        public static void RebuildStateMachine(FAnimationTrack track)
        {
            if (track.AnimatorController == null || track.LayerId == -1)
            {
                return;
            }

            bool isPreviewing = track.HasCache;

            if (isPreviewing)
            {
                track.ClearCache();
            }

            Animator animator = track.Owner.GetComponent <Animator>();

            animator.runtimeAnimatorController = null;

            AnimatorController controller = (AnimatorController)track.AnimatorController;

            track.UpdateEventIds();

            AnimatorControllerLayer layer = FindLayer(controller, track.LayerName);

            if (layer == null)
            {
                controller.AddLayer(track.LayerName);
                layer = FindLayer(controller, track.LayerName);
            }

            AnimatorStateMachine fluxSM = FindStateMachine(layer.stateMachine, FLUX_STATE_MACHINE_NAME);

            if (fluxSM == null)
            {
                fluxSM = layer.stateMachine.AddStateMachine(FLUX_STATE_MACHINE_NAME);

                ChildAnimatorStateMachine[] stateMachines = layer.stateMachine.stateMachines;

                for (int i = 0; i != stateMachines.Length; ++i)
                {
                    if (stateMachines[i].stateMachine == fluxSM)
                    {
                        stateMachines[i].position = layer.stateMachine.entryPosition + new Vector3(300, 0, 0);
                        break;
                    }
                }
                layer.stateMachine.stateMachines = stateMachines;
            }

            List <FEvent> events = track.Events;

            if (fluxSM.states.Length > events.Count)
            {
                for (int i = events.Count; i < fluxSM.states.Length; ++i)
                {
                    fluxSM.RemoveState(fluxSM.states[i].state);
                }
            }
            else if (fluxSM.states.Length < events.Count)
            {
                for (int i = fluxSM.states.Length; i < events.Count; ++i)
                {
                    fluxSM.AddState(i.ToString());
                }
            }

            AnimatorState lastState = null;
            Vector3       pos       = fluxSM.entryPosition + new Vector3(300, 0, 0);

            Vector3 statePosDelta = new Vector3(0, 80, 0);

            ChildAnimatorState[] states = fluxSM.states;

            for (int i = 0; i != events.Count; ++i)
            {
                FPlayAnimationEvent animEvent = (FPlayAnimationEvent)events[i];

                if (animEvent._animationClip == null) // dump events without animations
                {
                    continue;
                }

                states[i].position = pos;

                AnimatorState state = states[i].state;

                state.name = i.ToString();

                pos += statePosDelta;

                state.motion = animEvent._animationClip;

                animEvent._stateHash = Animator.StringToHash(state.name);

                if (lastState)
                {
                    AnimatorStateTransition[] lastStateTransitions = lastState.transitions;

                    if (animEvent.IsBlending())
                    {
                        AnimatorStateTransition transition = null;

                        for (int j = lastStateTransitions.Length - 1; j > 0; --j)
                        {
                            lastState.RemoveTransition(lastStateTransitions[j]);
                        }

                        transition = lastStateTransitions.Length == 1 ? lastStateTransitions[0] : lastState.AddTransition(state);

                        transition.offset = (animEvent._startOffset / animEvent._animationClip.frameRate) / animEvent._animationClip.length;

                        FPlayAnimationEvent prevAnimEvent = (FPlayAnimationEvent)events[i - 1];

                        int offsetFrame = Mathf.RoundToInt(transition.offset * animEvent._animationClip.length * animEvent._animationClip.frameRate);
                        animEvent._startOffset = Mathf.Clamp(offsetFrame, 0, animEvent.GetMaxStartOffset());
                        transition.offset      = (animEvent._startOffset / animEvent._animationClip.frameRate) / animEvent._animationClip.length;
                        EditorUtility.SetDirty(animEvent);

                        float blendSeconds = animEvent._blendLength / prevAnimEvent._animationClip.frameRate;
                        transition.duration = blendSeconds;

                        transition.hasExitTime = true;
                        float p = blendSeconds / prevAnimEvent._animationClip.length;
                        transition.exitTime = p > 1f ? 1f : 1f - Mathf.Clamp01(blendSeconds / prevAnimEvent._animationClip.length);

                        for (int j = transition.conditions.Length - 1; j >= 0; --j)
                        {
                            transition.RemoveCondition(transition.conditions[j]);
                        }

                        //						AnimatorCondition condition = transition.conditions[0];
                        //						condition.threshold = 0;
                    }
                    else // animations not blending, needs hack for animation previewing
                    {
                        for (int j = lastStateTransitions.Length - 1; j >= 0; --j)
                        {
                            lastState.RemoveTransition(lastStateTransitions[j]);
                        }
                    }
                }

                lastState = state;
            }

            fluxSM.states = states;

            if (fluxSM.states.Length > 0)
            {
                layer.stateMachine.defaultState = fluxSM.states[0].state;
            }

            animator.runtimeAnimatorController = controller;

            if (isPreviewing)
            {
                track.CreateCache();
            }
        }
 public static void RebuildStateMachine(FAnimationTrack track)
 {
     /*
      *          if( track.GetAnimatorController() == null )
      *                  return;
      *
      *          UnityEditor.Animations.AnimatorController controller = (UnityEditor.Animations.AnimatorController)track.GetAnimatorController();
      *
      *          track.UpdateEventIds();
      *
      *          //if( controller.FindParameter( ADVANCE_TRIGGER ) < 0 )
      *  controller.AddParameter(ADVANCE_TRIGGER, UnityEngine.AnimatorControllerParameterType.Trigger);
      *
      * UnityEditor.Animations.AnimatorControllerLayer baseLayer = controller.layers[0];
      *
      *          UnityEditor.Animations.AnimatorStateMachine baseSM = baseLayer.stateMachine;
      *
      *          List<FEvent> events = track.GetEvents();
      *
      * if (baseSM.states.Length > events.Count)
      *          {
      *  for (int i = events.Count; i < baseSM.states.Length; ++i)
      *                          baseSM.RemoveState( baseSM.states[i].state );
      *          }
      * else if (baseSM.states.Length < events.Count)
      *          {
      *  for (int i = baseSM.states.Length; i < events.Count; ++i)
      *                          baseSM.AddState( i.ToString() );
      *          }
      *
      *          UnityEditor.Animations.AnimatorState lastState = null;
      *          Vector3 pos = baseSM.anyStatePosition + new Vector3( 300, 0, 0 );
      *
      *          Vector3 statePosDelta = new Vector3(0, 80, 0 );
      *
      *          for( int i = 0; i != events.Count; ++i )
      *          {
      *                  FPlayAnimationEvent animEvent = (FPlayAnimationEvent)events[i];
      *
      *                  if( animEvent._animationClip == null ) // dump events without animations
      *                          continue;
      *
      *  UnityEditor.Animations.ChildAnimatorState child = baseSM.states[i];
      *  UnityEditor.Animations.ChildAnimatorStateMachine childMachine = baseSM.stateMachines[i];
      *  UnityEditor.Animations.AnimatorState state = child.state;
      *                  state.name = i.ToString();
      *
      *  child.position = pos;
      *
      *                  pos += statePosDelta;
      *
      *                  state.SetAnimationClip( animEvent._animationClip );
      *
      *                  animEvent._stateHash = Animator.StringToHash( state.name );
      *
      *                  if( lastState )
      *                  {
      *      UnityEditor.Animations.AnimatorTransition[] transitionsLastState = baseSM.GetStateMachineTransitions(childMachine.stateMachine);
      *
      *                          UnityEditor.Animations.AnimatorTransition transition = null;
      *
      *                          if( transitionsLastState.Length == 1 )
      *                          {
      *                                  if( transitionsLastState[0].destinationState != state )
      *                                  {
      *                                          baseSM.RemoveTransition( transitionsLastState[0] );
      *                                          transition = baseSM.AddTransition( lastState, state );
      *                                          transition.offset = (animEvent._startOffset / animEvent._animationClip.frameRate) / animEvent._animationClip.length;
      *                                  }
      *                                  else
      *                                          transition = transitionsLastState[0];
      *                          }
      *                          else
      *                          {
      *                                  if( transitionsLastState.Length > 0 )
      *                                  {
      *                                          for( int j = 0; j != transitionsLastState.Length; ++j )
      *                                                  baseSM.RemoveTransition( transitionsLastState[j] );
      *                                  }
      *                                  transition = baseSM.AddTransition( lastState, state );
      *                                  transition.offset = (animEvent._startOffset / animEvent._animationClip.frameRate) / animEvent._animationClip.length;
      *                          }
      *
      *                          if( animEvent.IsBlending() )
      *                          {
      *                                  FPlayAnimationEvent prevAnimEvent = (FPlayAnimationEvent)events[i-1];
      *
      *                                  animEvent._startOffset = Mathf.Clamp( Mathf.RoundToInt(transition.offset * animEvent._animationClip.length * animEvent._animationClip.frameRate), 0, animEvent._animationClip.isLooping ? animEvent.Length : Mathf.RoundToInt( animEvent._animationClip.length * animEvent._animationClip.frameRate ) - animEvent.Length );
      *                                  transition.offset = (animEvent._startOffset / animEvent._animationClip.frameRate) / animEvent._animationClip.length;
      *                                  EditorUtility.SetDirty( animEvent );
      *
      *                                  transition.duration = (animEvent._blendLength / prevAnimEvent._animationClip.frameRate) / prevAnimEvent._animationClip.length;
      *
      *                                  AnimatorCondition condition = transition.GetCondition(0);
      *
      *                                  condition.mode = TransitionConditionMode.ExitTime;
      *                                  condition.exitTime = (prevAnimEvent.Length + prevAnimEvent._startOffset) / (prevAnimEvent._animationClip.length*prevAnimEvent._animationClip.frameRate);
      *                                  condition.threshold = 0;
      *                          }
      *                          else // animations not blending, needs hack for animation previewing
      *                          {
      *                                  animEvent._startOffset = Mathf.RoundToInt(transition.offset * animEvent._animationClip.length * animEvent._animationClip.frameRate);
      *                                  EditorUtility.SetDirty( animEvent );
      *
      *                                  transition.duration = 0;
      *
      *                                  AnimatorCondition condition = transition.GetCondition(0);
      *
      *                                  condition.parameter = ADVANCE_TRIGGER;
      *                                  condition.mode = TransitionConditionMode.If;
      *
      *                                  condition.threshold = 0;
      *                          }
      *                  }
      *
      *                  lastState = state;
      *          }
      * */
 }