public static void SetAnimationClip(FPlayAnimationEvent animEvent, AnimationClip animClip) { FAnimationEventInspector editor = (FAnimationEventInspector)CreateEditor(animEvent, typeof(FAnimationEventInspector)); editor.SetAnimationClip(animClip); DestroyImmediate(editor); FAnimationTrackInspector.RebuildStateMachine((FAnimationTrack)animEvent.GetTrack()); }
public static void Resize(FEvent evt, FrameRange newFrameRange) { if (evt.FrameRange == newFrameRange || newFrameRange.Start > newFrameRange.End) { return; } if (newFrameRange.Length < evt.GetMinLength() || newFrameRange.Length > evt.GetMaxLength()) { Debug.LogError(string.Format("Trying to resize an Event to [{0},{1}] (length: {2}) which isn't a valid length, should be between [{3},{4}]", newFrameRange.Start, newFrameRange.End, newFrameRange.Length, evt.GetMinLength(), evt.GetMaxLength()), evt); return; } bool changedLength = evt.Length != newFrameRange.Length; if (!evt.GetTrack().CanAdd(evt, newFrameRange)) { return; } Undo.RecordObject(evt, changedLength ? "Resize Event" : "Move Event"); evt.Start = newFrameRange.Start; evt.End = newFrameRange.End; if (changedLength) { if (evt is FPlayAnimationEvent) { FPlayAnimationEvent animEvt = (FPlayAnimationEvent)evt; if (animEvt.IsAnimationEditable()) { FAnimationEventInspector.ScaleAnimationClip(animEvt._animationClip, animEvt.FrameRange); } } else if (evt is FTimescaleEvent) { ResizeAnimationCurve((FTimescaleEvent)evt, newFrameRange); } } EditorUtility.SetDirty(evt); if (FSequenceEditorWindow.instance != null) { FSequenceEditorWindow.instance.Repaint(); } }
public override void Render(int id, Rect rect, int headerWidth, FrameRange viewRange, float pixelsPerFrame) { base.Render(id, rect, headerWidth, viewRange, pixelsPerFrame); switch (Event.current.type) { case EventType.DragUpdated: if (rect.Contains(Event.current.mousePosition)) { int numAnimationsDragged = FAnimationEventInspector.NumAnimationsDragAndDrop(_track.Sequence.FrameRate); int frame = SequenceEditor.GetFrameForX(Event.current.mousePosition.x); DragAndDrop.visualMode = numAnimationsDragged > 0 && _track.CanAddAt(frame) ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Rejected; Event.current.Use(); } break; case EventType.DragPerform: if (rect.Contains(Event.current.mousePosition)) { AnimationClip animClip = FAnimationEventInspector.GetAnimationClipDragAndDrop(_track.Sequence.FrameRate); if (animClip && Mathf.Approximately(animClip.frameRate, _track.Sequence.FrameRate)) { int frame = SequenceEditor.GetFrameForX(Event.current.mousePosition.x); int maxLength; if (_track.CanAddAt(frame, out maxLength)) { FPlayAnimationEvent animEvt = FEvent.Create <FPlayAnimationEvent>(new FrameRange(frame, frame + Mathf.Min(maxLength, Mathf.RoundToInt(animClip.length * animClip.frameRate)))); _track.Add(animEvt); FAnimationEventInspector.SetAnimationClip(animEvt, animClip); DragAndDrop.AcceptDrag(); } } Event.current.Use(); } break; } }
public static AnimationClip CreateAnimationClip(FPlayAnimationEvent animEvent) { string filePath = EditorUtility.SaveFilePanelInProject("Create Animation...", animEvent.Owner.name, "anim", "Choose path..."); if (string.IsNullOrEmpty(filePath)) { return(null); } AnimationClip clip = UnityEditor.Animations.AnimatorController.AllocateAnimatorClip(System.IO.Path.GetFileNameWithoutExtension(filePath)); clip.frameRate = animEvent.Sequence.FrameRate; Transform ownerTransform = animEvent.Owner; Vector3 pos = ownerTransform.localPosition; Quaternion rot = ownerTransform.localRotation; 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) }; Keyframe[] wRotKeys = new Keyframe[] { new Keyframe(0, rot.w), new Keyframe(animEvent.LengthTime, rot.w) }; // 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; wRotKeys[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); AnimationCurve wRot = new AnimationCurve(wRotKeys); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.PPtrCurve(string.Empty, typeof(Transform), "m_LocalPosition.x"), xPos); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.PPtrCurve(string.Empty, typeof(Transform), "m_LocalPosition.y"), yPos); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.PPtrCurve(string.Empty, typeof(Transform), "m_LocalPosition.z"), zPos); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.PPtrCurve(string.Empty, typeof(Transform), "m_LocalRotation.x"), xRot); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.PPtrCurve(string.Empty, typeof(Transform), "m_LocalRotation.y"), yRot); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.PPtrCurve(string.Empty, typeof(Transform), "m_LocalRotation.z"), zRot); AnimationUtility.SetEditorCurve(clip, EditorCurveBinding.PPtrCurve(string.Empty, typeof(Transform), "m_LocalRotation.w"), wRot); clip.EnsureQuaternionContinuity(); AssetDatabase.CreateAsset(clip, filePath); FAnimationEventInspector.SetAnimationClip(animEvent, clip); return(clip); }
protected override void RenderEvent(FrameRange viewRange, FrameRange validKeyframeRange) { if (_animEvtSO == null) { _animEvtSO = new SerializedObject(_animEvt); _blendLength = _animEvtSO.FindProperty("_blendLength"); _startOffset = _animEvtSO.FindProperty("_startOffset"); } UpdateEventFromController(); _animEvtSO.Update(); FAnimationTrackEditor animTrackEditor = (FAnimationTrackEditor)_trackEditor; Rect transitionOffsetRect = _eventRect; int startOffsetHandleId = EditorGUIUtility.GetControlID(FocusType.Passive); int transitionHandleId = EditorGUIUtility.GetControlID(FocusType.Passive); bool isBlending = _animEvt.IsBlending(); bool isAnimEditable = _animEvt.IsAnimationEditable(); if (isBlending) { transitionOffsetRect.xMin = SequenceEditor.GetXForFrame(_animEvt.Start + _animEvt._blendLength) - 3; transitionOffsetRect.width = 6; transitionOffsetRect.yMin = transitionOffsetRect.yMax - 8; } switch (Event.current.type) { case EventType.MouseDown: if (EditorGUIUtility.hotControl == 0 && Event.current.alt && !isAnimEditable) { if (isBlending && transitionOffsetRect.Contains(Event.current.mousePosition)) { EditorGUIUtility.hotControl = transitionHandleId; if (Selection.activeObject != _transitionToState) { Selection.activeObject = _transitionToState; } Event.current.Use(); } else if (_eventRect.Contains(Event.current.mousePosition)) { _mouseDown = SequenceEditor.GetFrameForX(Event.current.mousePosition.x) - _animEvt.Start; EditorGUIUtility.hotControl = startOffsetHandleId; Event.current.Use(); } } break; case EventType.MouseUp: if (EditorGUIUtility.hotControl == transitionHandleId || EditorGUIUtility.hotControl == startOffsetHandleId) { EditorGUIUtility.hotControl = 0; Event.current.Use(); } break; case EventType.MouseDrag: if (EditorGUIUtility.hotControl == transitionHandleId) { int mouseDragPos = Mathf.Clamp(SequenceEditor.GetFrameForX(Event.current.mousePosition.x) - _animEvt.Start, 0, _animEvt.Length); if (_blendLength.intValue != mouseDragPos) { _blendLength.intValue = mouseDragPos; FPlayAnimationEvent prevAnimEvt = (FPlayAnimationEvent)animTrackEditor._track.GetEvent(_animEvt.GetId() - 1); if (_transitionDuration != null) { _transitionDuration.floatValue = (_blendLength.intValue / prevAnimEvt._animationClip.frameRate) / prevAnimEvt._animationClip.length; } Undo.RecordObject(this, "Animation Blending"); } Event.current.Use(); } else if (EditorGUIUtility.hotControl == startOffsetHandleId) { int mouseDragPos = Mathf.Clamp(SequenceEditor.GetFrameForX(Event.current.mousePosition.x) - _animEvt.Start, 0, _animEvt.Length); int delta = _mouseDown - mouseDragPos; _mouseDown = mouseDragPos; _startOffset.intValue = Mathf.Clamp(_startOffset.intValue + delta, 0, _animEvt._animationClip.isLooping ? _animEvt.Length : Mathf.RoundToInt(_animEvt._animationClip.length * _animEvt._animationClip.frameRate) - _animEvt.Length); if (_transitionOffset != null) { _transitionOffset.floatValue = (_startOffset.intValue / _animEvt._animationClip.frameRate) / _animEvt._animationClip.length; } Undo.RecordObject(this, "Animation Offset"); Event.current.Use(); } break; } _animEvtSO.ApplyModifiedProperties(); if (_transitionSO != null) { _transitionSO.ApplyModifiedProperties(); } switch (Event.current.type) { case EventType.DragUpdated: if (_eventRect.Contains(Event.current.mousePosition)) { int numAnimationsDragged = FAnimationEventInspector.NumAnimationsDragAndDrop(_evt.Sequence.FrameRate); DragAndDrop.visualMode = numAnimationsDragged > 0 ? DragAndDropVisualMode.Link : DragAndDropVisualMode.Rejected; Event.current.Use(); } break; case EventType.DragPerform: if (_eventRect.Contains(Event.current.mousePosition)) { AnimationClip animationClipDragged = FAnimationEventInspector.GetAnimationClipDragAndDrop(_evt.Sequence.FrameRate); if (animationClipDragged) { // animTrackEditor.ClearPreview(); int animFrameLength = Mathf.RoundToInt(animationClipDragged.length * animationClipDragged.frameRate); FAnimationEventInspector.SetAnimationClip(_animEvt, animationClipDragged); FUtility.Resize(_animEvt, new FrameRange(_animEvt.Start, _animEvt.Start + animFrameLength)); DragAndDrop.AcceptDrag(); Event.current.Use(); } else { Event.current.Use(); } } break; } FrameRange currentRange = _evt.FrameRange; base.RenderEvent(viewRange, validKeyframeRange); if (isAnimEditable && currentRange.Length != _evt.FrameRange.Length) { FAnimationEventInspector.ScaleAnimationClip(_animEvt._animationClip, _evt.FrameRange); } if (Event.current.type == EventType.Repaint) { if (isBlending && !isAnimEditable && viewRange.Contains(_animEvt.Start + _animEvt._blendLength)) { GUISkin skin = FUtility.GetFluxSkin(); GUIStyle transitionOffsetStyle = skin.GetStyle("BlendOffset"); Texture2D t = FUtility.GetFluxTexture("EventBlend.png"); Rect r = new Rect(_eventRect.xMin, _eventRect.yMin + 1, transitionOffsetRect.center.x - _eventRect.xMin, _eventRect.height - 2); GUI.color = new Color(1f, 1f, 1f, 0.3f); GUI.DrawTexture(r, t); if (Event.current.alt) { GUI.color = Color.white; } transitionOffsetStyle.Draw(transitionOffsetRect, false, false, false, false); } // GUI.color = Color.red; if (EditorGUIUtility.hotControl == transitionHandleId) { Rect transitionOffsetTextRect = transitionOffsetRect; transitionOffsetTextRect.y -= 16; transitionOffsetTextRect.height = 20; transitionOffsetTextRect.width += 50; GUI.Label(transitionOffsetTextRect, _animEvt._blendLength.ToString(), EditorStyles.label); } if (EditorGUIUtility.hotControl == startOffsetHandleId) { Rect startOffsetTextRect = _eventRect; GUI.Label(startOffsetTextRect, _animEvt._startOffset.ToString(), EditorStyles.label); } } }