/// <summary> /// Saves the binding boonRoot offset - position offset of boonRoot and gameObject holder at animation time=0s /// Saves current root position/rotation /// </summary> /// <param name="clipBindings">Clip bindings.</param> public static void SaveBindingsOffset(EditorClipBinding[] clipBindings) { int len = clipBindings.Length; EditorClipBinding clipBindingCurrent; for (int i=0; i<len; i++) { clipBindingCurrent = clipBindings [i]; SaveBindingOffset (clipBindingCurrent); } }
/// <summary> /// Saves the binding boonRoot offset - position before animation from boonRoot position at time=0s /// Saves current root position/rotation /// </summary> /// <param name="clipBindingCurrent">Clip binding current.</param> public static void SaveBindingOffset(EditorClipBinding clipBindingCurrent) { if (clipBindingCurrent.gameObject != null) { if(clipBindingCurrent.boneTransform!=null){ //save bone position Vector3 positionPrev = clipBindingCurrent.boneTransform.position; //make sample at 0f (sample would probably change bone position according to ani clip) AnimationMode.SampleAnimationClip (clipBindingCurrent.gameObject, clipBindingCurrent.clip, 0f); //calculate difference of bone position orginal - bone postion after clip effect clipBindingCurrent.boneRootPositionOffset = positionPrev - clipBindingCurrent.boneTransform.position; } clipBindingCurrent.positionOriginalRoot = clipBindingCurrent.gameObject.transform.root.position; clipBindingCurrent.rotationOriginalRoot = clipBindingCurrent.gameObject.transform.root.rotation; } }
/// <summary> /// Resamples the animation. /// </summary> /// <param name="clipBindings">Clip bindings.</param> /// <param name="time">Time.</param> public static void SampleClipBindingAt(EditorClipBinding[] clipBindings, float time) { Undo.FlushUndoRecordObjects (); AnimationMode.BeginSampling (); int len = clipBindings.Length; EditorClipBinding clipBindingCurrent; for (int i=0; i<len; i++) { clipBindingCurrent = clipBindings [i]; if (clipBindingCurrent.clip != null) { AnimationMode.SampleAnimationClip (clipBindingCurrent.gameObject, clipBindingCurrent.clip, time); //Correction is needed for root bones as animation doesn't respect current GameObject transform position,rotation //=> shifting current boneTransform position as result of clip animation, to offset of orginal position before animation if (clipBindingCurrent.boneTransform != null) { clipBindingCurrent.boneTransform.transform.position = clipBindingCurrent.boneRootPositionOffset + clipBindingCurrent.boneTransform.transform.position; } } } AnimationMode.EndSampling (); SceneView.RepaintAll (); }
/// <summary> /// Resets the binding Transform property modification. /// </summary> /// <param name="clipBindingCurrent">Clip binding current.</param> public static void ResetBindingTransformPropertyModification(EditorClipBinding clipBindingCurrent) { PrefabType prefabType = PrefabType.None; if (clipBindingCurrent.gameObject != null) { prefabType = PrefabUtility.GetPrefabType (clipBindingCurrent.gameObject); if (prefabType == PrefabType.ModelPrefabInstance || prefabType == PrefabType.PrefabInstance) clipBindingCurrent.gameObject.ResetPropertyModification<Transform> (); //rewind to start position clipBindingCurrent.ResetRoot (); } }
/// <summary> /// Resets the only transform property modifications and restore gameObject transform state as before animation. /// </summary> /// <param name="clipBindings">Clip bindings.</param> public static void ResetBindingsTransformPropertyModification(EditorClipBinding[] clipBindings) { int len = clipBindings.Length; EditorClipBinding clipBindingCurrent; for (int i=0; i<len; i++) { clipBindingCurrent = clipBindings [i]; ResetBindingTransformPropertyModification (clipBindingCurrent); } }
/// <summary> /// Show the specified space, clip, node and position. /// </summary> /// <param name="space">Space.</param> /// <param name="clip">Clip.</param> /// <param name="node">Node.</param> /// <param name="position">Position.</param> public static void Show(GameObject space,AnimationClip clip, SerializedNode node, Rect? position) { MecanimNodeEditorWindow.__spaceGameObject = space;//in this case is character MecanimNodeEditorWindow.__spaceGameObjectAnimationClip = clip; MecanimNodeEditorWindow.__serializedNode = node; /////// ACCESS SERIALIZED DATA ///////// NodePropertyIterator iterator = node.GetIterator (); __isPlaying = false; __isRecording = false; __variableSelected = null; __timeNormalized = 0f; __timeNormalizedUpdate = false; __timeCurrent = 0f; AnimationMode.StopAnimationMode (); Undo.postprocessModifications -= PostprocessAnimationRecordingModifications; SceneView.onSceneGUIDelegate += OnSceneGUI; if (iterator.Find ("clipBindings")) clipBindingsSerialized = iterator.current; if (__mecanimNodeClipBinding == null) __mecanimNodeClipBinding = ScriptableObject.CreateInstance<EditorClipBinding> (); __mecanimNodeClipBinding.gameObject = __spaceGameObject; __mecanimNodeClipBinding.clip = __spaceGameObjectAnimationClip; /////// INIT SERIALIZED NODE PROPERTIES - CURVES, COLORS, VARIABLES ////// if (iterator.Find ("curves")) curvesSerialized = iterator.current; else Debug.LogError ("MecananimNode should have public field 'curves'"); if (iterator.Find ("curvesColors")) curvesColorsSerialized = iterator.current; else Debug.LogError ("MecananimNode should have public field 'curvesColors'"); if (iterator.Find ("variablesBindedToCurves")) variablesBindedToCurvesSerialized = iterator.current; else Debug.LogError ("MecananimNode should have public field 'variablesBindedToCurves'"); curves = (AnimationCurve[])curvesSerialized.value; curveColors = (Color[])curvesColorsSerialized.value; variablesBindedToCurves = (UnityVariable[])variablesBindedToCurvesSerialized.value; AnimationModeUtility.ResetBindingsTransformPropertyModification (clipBindingsSerialized.value as EditorClipBinding[]); AnimationModeUtility.ResetBindingTransformPropertyModification (__mecanimNodeClipBinding); keyframeTimeValues = new float[0]; eventTimeValuesPrev = new float[0]; keyframeTimeValuesSelected = new bool[0]; keyframesDisplayNames = new string[0]; ///////////// create Reordable list of gameObject-animationClip ////////////////// __gameObjectClipList = new ReorderableList (clipBindingsSerialized.value as IList, typeof(EditorClipBinding), true, true, true, true); __gameObjectClipList.drawElementCallback = onDrawElement; __gameObjectClipList.drawHeaderCallback = onDrawHeaderElement; __gameObjectClipList.onRemoveCallback = onRemoveCallback; __gameObjectClipList.onAddCallback = onAddCallback; __gameObjectClipList.onSelectCallback = onSelectCallback; //__gameObjectClipList.elementHeight = 32f; if (MecanimNodeEditorWindow.__window != null)//restore last position = __window.position; MecanimNodeEditorWindow.__window = (MecanimNodeEditorWindow)EditorWindow.GetWindow (typeof(MecanimNodeEditorWindow)); if (position.HasValue) MecanimNodeEditorWindow.__window.position = position.Value; MecanimNodeEditorWindow.__window.Show (); }