private void UpdateEventFromController() { bool isBlending = _animEvt.IsBlending(); if (isBlending) { // FPlayAnimationEvent prevAnimEvt = (FPlayAnimationEvent)_animTrack.GetEvents()[_animEvt.GetId() - 1]; if (_transitionToState == null) { _transitionToState = FAnimationTrackInspector.GetTransitionTo(_animEvt); if (_transitionToState == null) { // if( animTrackEditor.PreviewInSceneView ) // animTrackEditor.ClearPreview(); FAnimationTrackInspector.RebuildStateMachine((FAnimationTrack)_trackEditor._track); _transitionToState = FAnimationTrackInspector.GetTransitionTo(_animEvt); } } if (_transitionSO == null) { if (_transitionToState != null) { _transitionSO = new SerializedObject(_transitionToState); // SerializedProperty p = _transitionSO.GetIterator(); // // while( p.Next( true ) ) // Debug.Log (p.propertyPath ); _transitionDuration = _transitionSO.FindProperty("m_TransitionDuration"); _transitionOffset = _transitionSO.FindProperty("m_TransitionOffset"); _transitionConditions = _transitionSO.FindProperty("m_Conditions"); } } else if (_transitionSO.targetObject == null) { _transitionDuration = null; _transitionOffset = null; _transitionConditions = null; _transitionSO = null; } } else { if (_transitionToState != null || _transitionSO != null) { _transitionToState = null; _transitionSO = null; _transitionOffset = null; _transitionDuration = null; _transitionConditions = null; } } if (_transitionSO != null) { _transitionSO.Update(); _animEvtSO.Update(); FPlayAnimationEvent prevAnimEvt = (FPlayAnimationEvent)_animTrack.GetEvents()[_animEvt.GetId() - 1]; AnimationClip prevAnimEvtClip = prevAnimEvt._animationClip; if (prevAnimEvtClip != null) { float exitTimeNormalized = (prevAnimEvt.Length / prevAnimEvtClip.frameRate) / prevAnimEvtClip.length; SerializedProperty exitTime = _transitionConditions.GetArrayElementAtIndex(0).FindPropertyRelative("m_ExitTime"); if (!Mathf.Approximately(exitTimeNormalized, exitTime.floatValue)) { exitTime.floatValue = exitTimeNormalized; } float blendNormalized = (_blendLength.intValue / prevAnimEvtClip.frameRate) / prevAnimEvtClip.length; if (!Mathf.Approximately(blendNormalized, _transitionDuration.floatValue)) { _blendLength.intValue = Mathf.Clamp(Mathf.RoundToInt(_transitionDuration.floatValue * prevAnimEvtClip.length * prevAnimEvtClip.frameRate), 0, _animEvt.Length); _transitionDuration.floatValue = (_blendLength.intValue / prevAnimEvtClip.frameRate) / prevAnimEvtClip.length; _animEvtSO.ApplyModifiedProperties(); } } _transitionSO.ApplyModifiedProperties(); } }
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); }
public override void OnInspectorGUI() { bool rebuildTrack = false; base.OnInspectorGUI(); serializedObject.Update(); if (_animationClip.objectReferenceValue == null) { EditorGUILayout.HelpBox("无动画剪辑Clip", MessageType.Warning); } EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(_animationClip, _animationClipUI); 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.Container.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.Container.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); } } if (_animEvent.IsBlending()) { int id = _animEvent.GetId(); FPlayAnimationEvent preAnimEvt = _animEvent.Track.Events[id - 1] as FPlayAnimationEvent; if (id > 0) { EditorGUI.BeginChangeCheck(); EditorGUILayout.IntSlider(_startOffset, 0, _animEvent.GetMaxStartOffset(), _startOffsetUI); EditorGUILayout.IntSlider(_blendLength, 0, preAnimEvt.Length > _animEvent.Length ? _animEvent.Length : preAnimEvt.Length, _blendLengthUI); if (EditorGUI.EndChangeCheck()) { rebuildTrack = true; } } } else { EditorGUI.BeginChangeCheck(); EditorGUILayout.IntSlider(_startOffset, 0, _animEvent.GetMaxStartOffset(), _startOffsetUI); if (EditorGUI.EndChangeCheck()) { rebuildTrack = true; } } serializedObject.ApplyModifiedProperties(); if (rebuildTrack) { FAnimationTrackInspector.RebuildStateMachine((FAnimationTrack)_animEvent.Track); } }