Hi, I’m having some trouble creating new animation clips via script. I have A TON of npcs and skin parts, most of them are just spritesheets organized exactly the same way and already editted. Thing is… making the animator component is pretty boring and very tedious. Since they all behave the same way, I thought about creating a script to automate this work.
The animator is very simple, but I don’t know what I’m supposed to do. I got stuck when I tried to add keyframes to change the sprite in the script.
This is the script I was making to automatically create the skin parts of my player(I was supposed to input the spritesheet and it generates the animator and animation clips). I’ve managed to creat the animator and the clips, but now I have no idea how to create the keyframes. Would love some help!
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Sirenix.OdinInspector;
public class SkinAnimatorGenerator : MonoBehaviour
{
public string mainPath;
public string name;
public Texture2D MaleHead;
public Texture2D MaleBody;
public Texture2D MaleLegs;
public Texture2D FemaleHead;
public Texture2D FemaleBody;
public Texture2D FemaleLegs;
private string maleFolderPath;
private string femaleFolderPath;
[Button("Create Skin Animators")]
private void CreateSkinAnimator()
{
var maleFolder = AssetDatabase.CreateFolder(mainPath, $"M{name}");
var femaleFolder = AssetDatabase.CreateFolder(mainPath, $"F{name}");
maleFolderPath = AssetDatabase.GUIDToAssetPath(maleFolder);
femaleFolderPath = AssetDatabase.GUIDToAssetPath(femaleFolder);
AssetDatabase.CreateFolder(maleFolderPath, "Head");
AssetDatabase.CreateFolder(maleFolderPath, "Body");
AssetDatabase.CreateFolder(maleFolderPath, "Legs");
CreateSkinPartAnimator(MaleHead, $"{maleFolderPath}/Head");
CreateSkinPartAnimator(MaleBody, $"{maleFolderPath}/Body");
CreateSkinPartAnimator(MaleLegs, $"{maleFolderPath}/Legs");
AssetDatabase.CreateFolder(femaleFolderPath, "Head");
AssetDatabase.CreateFolder(femaleFolderPath, "Body");
AssetDatabase.CreateFolder(femaleFolderPath, "Legs");
CreateSkinPartAnimator(FemaleHead, $"{femaleFolderPath}/Head");
CreateSkinPartAnimator(FemaleBody, $"{femaleFolderPath}/Body");
CreateSkinPartAnimator(FemaleLegs, $"{femaleFolderPath}/Legs");
}
private void CreateSkinPartAnimator(Texture2D texture2D, string path)
{
var controller = UnityEditor.Animations.AnimatorController.
CreateAnimatorControllerAtPath($"{path}/{texture2D.name}.controller");
var rootSM = controller.layers[0].stateMachine;
//Create parameters
controller.AddParameter("Walking", AnimatorControllerParameterType.Bool);
controller.AddParameter("Dashing", AnimatorControllerParameterType.Bool);
//Create all states
var idleDown = rootSM.AddState("IdleDown" );
var idleUp = rootSM.AddState("IdleUp" );
var idleLeft = rootSM.AddState("IdleLeft" );
var idleRight = rootSM.AddState("IdleRight");
var walkDown = rootSM.AddState("WalkDown" );
var walkUp = rootSM.AddState("WalkUp" );
var walkLeft = rootSM.AddState("WalkLeft" );
var walkRight = rootSM.AddState("WalkRight");
var dashDown = rootSM.AddState("DashDown" );
var dashUp = rootSM.AddState("DashUp" );
var dashLeft = rootSM.AddState("DashLeft" );
var dashRight = rootSM.AddState("DashRight");
//Create motions
AnimationClip idleDownAnim = new AnimationClip();
AnimationClip idleUpAnim = new AnimationClip();
AnimationClip idleLeftAnim = new AnimationClip();
AnimationClip idleRightAnim = new AnimationClip();
AnimationClip walkDownAnim = new AnimationClip();
AnimationClip walkUpAnim = new AnimationClip();
AnimationClip walkLeftAnim = new AnimationClip();
AnimationClip walkRightAnim = new AnimationClip();
AnimationClip dashDownAnim = new AnimationClip();
AnimationClip dashUpAnim = new AnimationClip();
AnimationClip dashLeftAnim = new AnimationClip();
AnimationClip dashRightAnim = new AnimationClip();
AssetDatabase.CreateAsset(idleDownAnim, $"{path}/{texture2D.name}_IdleDown.anim");
AssetDatabase.CreateAsset(idleUpAnim, $"{path}/{texture2D.name}_IdleUp.anim");
AssetDatabase.CreateAsset(idleLeftAnim, $"{path}/{texture2D.name}_IdleLeft.anim");
AssetDatabase.CreateAsset(idleRightAnim, $"{path}/{texture2D.name}_IdleRight.anim");
AssetDatabase.CreateAsset(walkDownAnim, $"{path}/{texture2D.name}_WalkDown.anim");
AssetDatabase.CreateAsset(walkUpAnim, $"{path}/{texture2D.name}_WalkUp.anim");
AssetDatabase.CreateAsset(walkLeftAnim, $"{path}/{texture2D.name}_WalkLeft.anim");
AssetDatabase.CreateAsset(walkRightAnim, $"{path}/{texture2D.name}_WalkRight.anim");
AssetDatabase.CreateAsset(dashDownAnim, $"{path}/{texture2D.name}_DashDown.anim");
AssetDatabase.CreateAsset(dashUpAnim, $"{path}/{texture2D.name}_DashUp.anim");
AssetDatabase.CreateAsset(dashLeftAnim, $"{path}/{texture2D.name}_DashLeft.anim");
AssetDatabase.CreateAsset(dashRightAnim, $"{path}/{texture2D.name}_DashRight.anim");
//Assign motions
idleDown.motion = idleDownAnim;
idleUp.motion = idleUpAnim;
idleLeft.motion = idleLeftAnim;
idleRight.motion = idleRightAnim;
walkDown.motion = walkDownAnim;
walkUp.motion = walkUpAnim;
walkLeft.motion = walkLeftAnim;
walkRight.motion = walkRightAnim;
dashDown.motion = dashDownAnim;
dashUp.motion = dashUpAnim;
dashLeft.motion = dashLeftAnim;
dashRight.motion = dashRightAnim;
//Create conditions
//Dash To Idle
var DDownToIdle = dashDown.AddTransition(idleDown, false);
DDownToIdle.hasExitTime = false;
DDownToIdle.AddCondition
(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0, "Dashing");
var DUpToIdle = dashUp.AddTransition(idleUp, false);
DUpToIdle.hasExitTime = false;
DUpToIdle.AddCondition
(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0, "Dashing");
var DLeftToIdle = dashLeft.AddTransition(idleLeft, false);
DLeftToIdle.hasExitTime = false;
DLeftToIdle.AddCondition
(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0, "Dashing");
var DRightToIdle = dashRight.AddTransition(idleRight, false);
DRightToIdle.hasExitTime = false;
DRightToIdle.AddCondition
(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0, "Dashing");
//Walk To Idle
var WDownToIdle = walkDown.AddTransition(idleDown, false);
WDownToIdle.hasExitTime = false;
WDownToIdle.AddCondition
(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0, "Walking");
var WUpToIdle = walkUp.AddTransition(idleUp, false);
WUpToIdle.hasExitTime = false;
WUpToIdle.AddCondition
(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0, "Walking");
var WLeftToIdle = walkLeft.AddTransition(idleLeft, false);
WLeftToIdle.hasExitTime = false;
WLeftToIdle.AddCondition
(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0, "Walking");
var WRightToIdle = walkRight.AddTransition(idleRight, false);
WRightToIdle.hasExitTime = false;
WRightToIdle.AddCondition
(UnityEditor.Animations.AnimatorConditionMode.IfNot, 0, "Walking");
}
}
#endif