public static bool AddClipToAnimatorComponent(Animator animator, AnimationClip newClip) { UnityEditor.Animations.AnimatorController animatorController = UnityEditor.Animations.AnimatorController.GetEffectiveAnimatorController(animator); if ((UnityEngine.Object)animatorController == (UnityEngine.Object)null) { UnityEditor.Animations.AnimatorController controllerForClip = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerForClip(newClip, animator.gameObject); UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, controllerForClip); return((UnityEngine.Object)controllerForClip != (UnityEngine.Object)null); } ChildAnimatorState state = animatorController.layers[0].stateMachine.FindState(newClip.name); if (state.Equals((object)new ChildAnimatorState())) { animatorController.AddMotion((Motion)newClip); } else if ((bool)((UnityEngine.Object)state.state) && (UnityEngine.Object)state.state.motion == (UnityEngine.Object)null) { state.state.motion = (Motion)newClip; } else if ((bool)((UnityEngine.Object)state.state) && (UnityEngine.Object)state.state.motion != (UnityEngine.Object)newClip) { animatorController.AddMotion((Motion)newClip); } return(true); }
public static bool AddClipToAnimatorComponent(Animator animator, AnimationClip newClip) { UnityEditor.Animations.AnimatorController effectiveAnimatorController = UnityEditor.Animations.AnimatorController.GetEffectiveAnimatorController(animator); if (effectiveAnimatorController == null) { effectiveAnimatorController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerForClip(newClip, animator.gameObject); UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, effectiveAnimatorController); if (effectiveAnimatorController != null) { return(true); } } else { ChildAnimatorState state = effectiveAnimatorController.layers[0].stateMachine.FindState(newClip.name); if (state.Equals(new ChildAnimatorState())) { effectiveAnimatorController.AddMotion(newClip); } else if ((state.state != null) && (state.state.motion == null)) { state.state.motion = newClip; } else if ((state.state != null) && (state.state.motion != newClip)) { effectiveAnimatorController.AddMotion(newClip); } return(true); } return(false); }
public static bool AddClipToAnimatorComponent(Animator animator, AnimationClip newClip) { UnityEditor.Animations.AnimatorController animatorController = UnityEditor.Animations.AnimatorController.GetEffectiveAnimatorController(animator); if (animatorController == null) { animatorController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerForClip(newClip, animator.gameObject); UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, animatorController); return(animatorController != null); } ChildAnimatorState childAnimatorState = animatorController.layers[0].stateMachine.FindState(newClip.name); if (childAnimatorState.Equals(default(ChildAnimatorState))) { animatorController.AddMotion(newClip); } else if (childAnimatorState.state && childAnimatorState.state.motion == null) { childAnimatorState.state.motion = newClip; } else if (childAnimatorState.state && childAnimatorState.state.motion != newClip) { animatorController.AddMotion(newClip); } return(true); }
public static AnimatorController CreateAnimatorControllerAtPathWithClip(string path, AnimationClip clip) { AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath(path); animatorController.AddMotion(clip); return(animatorController); }
public static GameObject InstantiateWithAnimation(GameObject go, AnimationClip animClip) //Used after the user has selected a GameObject and an AnimationClip to edit; instantiates the GameObject into the scene and sets it up appropriately. { ForceLoop(animClip); GameObject newGo = GameObject.Instantiate(go); if (newGo.GetComponent <Animator> () != null) { //We don't like Animator for our purposes; ergo, we get rid of it //TODO: Maybe add support for Animator?? //Object.Destroy(newGo.GetComponent<Animator>()); } /*if (newGo.GetComponent<Animation> () != null) { * newGo.GetComponent<Animation> ().clip = animClip; * } else { * newGo.AddComponent<Animation> (); * newGo.GetComponent<Animation> ().clip = animClip; * } * * newGo.GetComponent<Animation> ().AddClip (animClip, "default"); */ if (newGo.GetComponent <Animator> () != null) { UnityEditor.Animations.AnimatorController ac = new UnityEditor.Animations.AnimatorController(); ac.AddLayer("default"); ac.AddMotion(animClip); newGo.GetComponent <Animator> ().runtimeAnimatorController = ac; } return(newGo); //TODO: May have to tell an Editor class somewhere that our current object being edited is this newly instantiated GameObject }
static void DoCreateClip() { GameObject gameObject = Selection.activeGameObject; Animator animator = gameObject.GetComponent <Animator>(); if (animator == null) { return; } UnityEditor.Animations.AnimatorController controller = animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController; if (controller == null) { return; } // Go forward with presenting user a save clip dialog string message = string.Format("Create a new animation for the game object '{0}':", gameObject.name); string newClipPath = EditorUtility.SaveFilePanelInProject("Create New Animation", "New Animation", "anim", message); // If user canceled or save path is invalid, we can't create a clip if (newClipPath == "") { return; } // At this point we know that we can create a clip AnimationClip newClip = new AnimationClip(); AssetDatabase.CreateAsset(newClip, newClipPath); controller.AddMotion(newClip); }
/// <summary> /// <para>Creates an AnimatorController at the given path, and automatically create an AnimatorLayer with an AnimatorStateMachine that will add a State with the AnimationClip in it.</para> /// </summary> /// <param name="path">The path where the AnimatorController will be created.</param> /// <param name="clip">The default clip that will be played by the AnimatorController.</param> public static AnimatorController CreateAnimatorControllerAtPathWithClip(string path, AnimationClip clip) { AnimatorController controllerAtPath = AnimatorController.CreateAnimatorControllerAtPath(path); controllerAtPath.AddMotion((Motion)clip); return(controllerAtPath); }
public static void addMotionToControllerByPath(string path, UnityEditor.Animations.AnimatorController ac) { AnimationClip fist = (AnimationClip)AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip)); Motion fistmotion = (Motion)fist as Motion; ac.AddMotion(fistmotion); }
static AnimationClip GenerateTriggerableTransition(string name, UnityEditor.Animations.AnimatorController controller) { var clip = UnityEditor.Animations.AnimatorController.AllocateAnimatorClip(name); AssetDatabase.AddObjectToAsset(clip, controller); var state = controller.AddMotion(clip); controller.AddParameter(name, AnimatorControllerParameterType.Trigger); var stateMachine = controller.layers[0].stateMachine; var transition = stateMachine.AddAnyStateTransition(state); transition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, name); // var state = UnityEditor.Animations.AnimatorController.AddAnimationClipToController(controller, clip); //controller.AddParameter(name, AnimatorControllerParameterType.Trigger); //var stateMachine = controller.GetLayer(0).stateMachine; //var transition = stateMachine.AddAnyStateTransition(state); //var condition = transition.GetCondition(0); //condition.mode = TransitionConditionMode.If; //condition.parameter = name; //#endif return(clip); }
public static void addMotionToController(string name, UnityEditor.Animations.AnimatorController ac) { GameObject av = getVRCSceneAvatar(); AnimatorOverrideController controller = av.GetComponent <VRC_AvatarDescriptor>().CustomStandingAnims; AnimationClip anclip = controller[name]; Motion fistmotion = (Motion)anclip as Motion; ac.AddMotion(fistmotion); }
private static AnimationClip GenerateTriggerableTransition(string name, UnityEditor.Animations.AnimatorController controller) { // Create the clip var clip = UnityEditor.Animations.AnimatorController.AllocateAnimatorClip(name); AssetDatabase.AddObjectToAsset(clip, controller); // Create a state in the animatior controller for this clip var state = controller.AddMotion(clip); // Add a transition property controller.AddParameter(name, AnimatorControllerParameterType.Trigger); // Add an any state transition var stateMachine = controller.layers[0].stateMachine; var transition = stateMachine.AddAnyStateTransition(state); transition.AddCondition(AnimatorConditionMode.If, 0, name); return(clip); }
// GUI Layout for 2D Animation creation function void OnGUI() { if (selectedObject != null) { //Display the object's name that the animations will be created from EditorGUILayout.LabelField("Animations for " + selectedObject.name); Object[] sprites = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(selectedObject)); int spriteCount = sprites.Length; //Display the number of sprites in the sheet EditorGUILayout.LabelField("Number of Sprites: " + spriteCount); //create a space EditorGUILayout.Separator(); //Get the name for the animation controller controllerName = EditorGUILayout.TextField("Controller Name", controllerName); //Determine how many animations there will be numAnimations = EditorGUILayout.IntField("How many animations?", numAnimations); //Loop through each theoretical animation for (int i = 0; i < numAnimations; i++) { //Determine a name for the animation animationNames [i] = EditorGUILayout.TextField("Animation Name", animationNames [i]); //Start a section where the items will be displayed horizontally instead of vertically. EditorGUILayout.BeginHorizontal(); //Determine the start frame for the animation startFrames [i] = EditorGUILayout.IntField("Start Frame", startFrames [i]); //Determine the end frame for the animation endFrames [i] = EditorGUILayout.IntField("End Frame", endFrames [i]); //End the section where the previous items are displayed horizontally instead of vertically EditorGUILayout.EndHorizontal(); //Start a section where the following items will be displayed horizontally instead of vertically. EditorGUILayout.BeginHorizontal(); //Determine the frame rate for the animation clipFrameRate [i] = EditorGUILayout.FloatField("Frame Rate", clipFrameRate [i]); //Determine the space between each keyframe clipTimeBetween [i] = EditorGUILayout.FloatField("Frame Spacing", clipTimeBetween [i]); //End the section where the previous items are displayed horizontally instead of vertically EditorGUILayout.EndHorizontal(); //Start a section where the following items will be displayed horizontally instead of vertically. EditorGUILayout.BeginHorizontal(); //Create a checkbox to determine if this animation should loop loop [i] = EditorGUILayout.Toggle("Loop", loop [i]); //Create a checkbox to determine if this animation should ping-pong pingPong [i] = EditorGUILayout.Toggle("Ping Pong", pingPong [i]); //End the section where the previous items are displayed horizontally instead of vertically EditorGUILayout.EndHorizontal(); //Create a space EditorGUILayout.Separator(); } //Create a button with the label "Create" if (GUILayout.Button("Create")) { //if the button has been pressed //create an animator controller UnityEditor.Animations.AnimatorController controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath("Assets/" + controllerName + ".controller"); for (int j = 0; j < numAnimations; j++) { //if the end frame of an iteration is greater than the total number of frames, a prompt is generated to see //if the user wishes to change it to the last available frame. Otherwise the iteration is skipped if (endFrames[j] > spriteCount) { if (EditorUtility.DisplayDialog("Caution", "End Frame of animation" + j + " exceeds last frame available.\n" + "Change to last frame?", "Yes", "No")) { endFrames[j] = spriteCount; } else { continue; } } //create animation clip AnimationClip tempClip = CreateClip(selectedObject, animationNames [j], startFrames [j], endFrames [j], clipFrameRate [j], clipTimeBetween [j], pingPong [j]); //determine if the clip should loop if (loop [j]) { //set the loop on the clip to true AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(tempClip); settings.loopTime = true; settings.loopBlend = true; AnimationUtility.SetAnimationClipSettings(tempClip, settings); } controller.AddMotion(tempClip); } CreatePrefab(controller); } } }
//Defines Gui Features void OnGUI() { if (selectedObject != null) { EditorGUILayout.LabelField("Animations for " + selectedObject.name); EditorGUILayout.Separator(); controllerName = EditorGUILayout.TextField("Controller Name", controllerName); numAnimations = EditorGUILayout.IntField("How many animations", numAnimations); for (int i = 0; i < numAnimations; i++) { //Allows the user to name the animation animationNames[i] = EditorGUILayout.TextField("Animation Name", animationNames[i]); //Animation length modifiers for GUI EditorGUILayout.BeginHorizontal(); startFrames[i] = EditorGUILayout.IntField("Start Frame", startFrames[i]); endFrames[i] = EditorGUILayout.IntField("End Frame", endFrames[i]); EditorGUILayout.EndHorizontal(); //Frame Rate modifiers for GUI EditorGUILayout.BeginHorizontal(); clipFrameRate[i] = EditorGUILayout.FloatField("Frame Rate", clipFrameRate[i]); clipTimeBetween[i] = EditorGUILayout.FloatField("Frame Spacing", clipTimeBetween[i]); EditorGUILayout.EndHorizontal(); //Animation behaviour modifiers for GUI EditorGUILayout.BeginHorizontal(); loop[i] = EditorGUILayout.Toggle("Loop", loop[i]); pingPong[i] = EditorGUILayout.Toggle("Ping Pong", pingPong[i]); EditorGUILayout.EndHorizontal(); EditorGUILayout.Separator(); } //if the user presses the create button if (GUILayout.Button("Create")) { UnityEditor.Animations.AnimatorController controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath( ("Assets/" + controllerName + ".controller")); for (int i = 0; i < numAnimations; i++) { //apply changes to the Animation AnimationClip tempClip = CreateClip(selectedObject, animationNames[i], startFrames[i], endFrames[i], clipFrameRate[i], clipTimeBetween[i], pingPong[i]); //Sets up loop behaviour if (loop[i]) { AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(tempClip); settings.loopTime = true; settings.loopBlend = true; AnimationUtility.SetAnimationClipSettings(tempClip, settings); } controller.AddMotion(tempClip); } //Creates a prefab of the given Ojbect PrefabUtility.InstantiatePrefab(selectedObject); Instantiate(selectedObject); //Refreshs the AssetDatabase AssetDatabase.Refresh(); } } }
void OnGUI() { if (selectedObject != null) { //Display the objects name that the animations will be created from EditorGUILayout.LabelField("Animations for " + selectedObject.name); //Create a space EditorGUILayout.Separator(); //Get the name for the animation controller controllerName = EditorGUILayout.TextField("Controller Name", controllerName); //Determine how many animations there will be numAnimations = EditorGUILayout.IntField("How many animations?", numAnimations); //Loop through each theoretical animation for (int i = 0; i < numAnimations; i++) { //Determine a name for the animation animationNames[i] = EditorGUILayout.TextField("Animation Name", animationNames[i]); //Start a section where the following items will be displayed horizontally instead of vertically EditorGUILayout.BeginHorizontal(); //Determine the start frame for the animation startFrames[i] = EditorGUILayout.IntField("Start Frame", startFrames[i]); //Determine the end frame for the animation endFrames[i] = EditorGUILayout.IntField("End Frame", endFrames[i]); //End the section where the previous items are displayed horitontally instead of vertically EditorGUILayout.EndHorizontal(); //Start a section where the following items will be displayed horizontally instead of vertically EditorGUILayout.BeginHorizontal(); //Determine the frame rate for the animation clipFrameRate[i] = EditorGUILayout.FloatField("Frame Rate", clipFrameRate[i]); //Determine the space between each keyframe clipTimeBetween[i] = EditorGUILayout.FloatField("Frame Spacing", clipTimeBetween[i]); //End the section where the previous items are displayed horitontally instead of vertically EditorGUILayout.EndHorizontal(); //Start a section where the following items will be displayed horizontally instead of vertically EditorGUILayout.BeginHorizontal(); //Create a checkbox to determine if this animation should loop loop[i] = EditorGUILayout.Toggle("Loop", loop[i]); //Create a checkbox to determine if this animation should pingpong pingPong[i] = EditorGUILayout.Toggle("Ping Pong", pingPong[i]); //End the section where the previous items are displayed horitontally instead of vertically EditorGUILayout.EndHorizontal(); //Create a space EditorGUILayout.Separator(); } //Create a button with the label "Create" if (GUILayout.Button("Create")) { //Create an animator controller UnityEditor.Animations.AnimatorController controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(("Assets/" + controllerName + ".controller")); for (int i = 0; i < numAnimations; i++) { //Create animation clip AnimationClip tempClip = CreateClip(selectedObject, animationNames[i], startFrames[i], endFrames[i], clipFrameRate[i], clipTimeBetween[i], pingPong[i]); //Detemine if the clip should loop if (loop[i]) { //If so, capture the settings of the clip AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(tempClip); //Set the looping to true settings.loopTime = true; settings.loopBlend = true; //Apply the settings to the clip AnimationUtility.SetAnimationClipSettings(tempClip, settings); } //Add the clip to the Animator Controller controller.AddMotion(tempClip); } } } }
void OnGUI() { if (selectedObject != null) { //Display the name of the object the animations will be created from EditorGUILayout.LabelField("Animations for " + selectedObject.name); //Create a space EditorGUILayout.Separator(); //Get the name of the Animation Controller controllerName = EditorGUILayout.TextField("Controller Name", controllerName); //Determine how many animations there will be numAnimations = EditorGUILayout.IntField("How many animations?", numAnimations); //Loop through all the theoretical animations for (int i = 0; i < numAnimations; i++) { //Get the name of the animation animationNames[i] = EditorGUILayout.TextField("Animation Name", animationNames[i]); //Start a section where the elements will be listed horizontally EditorGUILayout.BeginHorizontal(); //Determine the start frame of the animation startFrames[i] = EditorGUILayout.IntField("Start Frame", startFrames[i]); //Determine the end frame of the animation endFrames[i] = EditorGUILayout.IntField("End Frame", endFrames[i]); //Change to the next row of elements listed horizontally EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); //Create a checkbox to see if the animation should loop loop[i] = EditorGUILayout.Toggle("Loop", loop[i]); //Create a checkbox to see if the animation should pingpong pingPong[i] = EditorGUILayout.Toggle("Ping Pong", pingPong[i]); //End the horizontal display section EditorGUILayout.EndHorizontal(); //Create a space EditorGUILayout.Separator(); } //Create a button labeled "Create" if (GUILayout.Button("Create")) { //If the button is pressed... UnityEditor.Animations.AnimatorController controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(("Assets/" + controllerName + ".controller")); for (int i = 0; i < numAnimations; i++) { AnimationClip tempClip = new AnimationClip(); tempClip = CreateClip(selectedObject, animationNames[i], startFrames[i], endFrames[i], clipFrameRate[i], clipTimeBetween[i], pingPong[i]); if (tempClip == null) { //Play error sound and abort animation creation System.Media.SystemSounds.Exclamation.Play(); i = numAnimations; } else { if (loop[i]) { AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(tempClip); settings.loopTime = true; settings.loopBlend = true; AnimationUtility.SetAnimationClipSettings(tempClip, settings); } controller.AddMotion(tempClip); } } } } }
// Adds components to the window GUI void OnGUI() { if (selectedObject != null) { //Display the objects name that the animations will be created from EditorGUILayout.LabelField("Animations for " + selectedObject.name); //Create a space EditorGUILayout.Separator(); //Get the name for the animation controller controllerName = EditorGUILayout.TextField("Animator Controller Name", controllerName); //Determine how many animations there will be numAnimations = EditorGUILayout.IntField("How many animations?", numAnimations); //Loop through each theoretical animation for (int i = 0; i < numAnimations; i++) { //Determine a name for the animation animationNames[i] = EditorGUILayout.TextField("Animation " + (i + 1) + " Name:", animationNames[i]); //Start a section where the following items will be displayed horizontally instead of vertically EditorGUILayout.BeginHorizontal(); //Determine the start frame for the animation startFrames[i] = EditorGUILayout.IntField("Start Frame: ", startFrames[i]); //Determine the end frame for the animation endFrames[i] = EditorGUILayout.IntField("End Frame: ", endFrames[i]); //End the section where the previous items are displayed horitontally instead of vertically EditorGUILayout.EndHorizontal(); //Start a section where the following items will be displayed horizontally instead of vertically EditorGUILayout.BeginHorizontal(); //Create a checkbox to determine if this animation should loop loop[i] = EditorGUILayout.Toggle("Loop", loop[i]); //Create a checkbox to determine if this animation should pingpong pingPong[i] = EditorGUILayout.Toggle("Ping Pong", pingPong[i]); //End the section where the previous items are displayed horitontally instead of vertically EditorGUILayout.EndHorizontal(); //Create a space EditorGUILayout.Separator(); } //Create a button with the label "Create" if (GUILayout.Button("Create")) { //Create an animator controller UnityEditor.Animations.AnimatorController controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(("Assets/" + controllerName + ".controller")); for (int i = 0; i < numAnimations; i++) { //Create animation clip AnimationClip tempClip = CreateClip(selectedObject, animationNames[i], startFrames[i], endFrames[i], pingPong[i]); //Detemine if the clip should loop if (loop[i]) { //If so, capture the settings of the clip AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(tempClip); //Set the looping to true settings.loopTime = true; settings.loopBlend = true; //Apply the settings to the clip AnimationUtility.SetAnimationClipSettings(tempClip, settings); } //Add the clip to the Animator Controller controller.AddMotion(tempClip); // Create an instance of the sprite at (0, 0, 0) GameObject spriteInstance = new GameObject(); // Add a sprite renderer to the spriteInstance spriteInstance.AddComponent <SpriteRenderer>(); // Add an animator component to the instance spriteInstance.AddComponent <Animator>(); // Set the controller of the animator component to the created controller spriteInstance.GetComponent <Animator>().runtimeAnimatorController = controller; // Set the name of the instance to the name of the animation spriteInstance.name = animationNames[i]; // Select the instance for the CreatePrefab method Selection.activeGameObject = spriteInstance; // Create the prefab. ScriptCreatePrefab.CreatePrefab(); } } } }