Question by
unity_CDjuzjSnutGHiA · Oct 21, 2019 at 07:19 AM ·
animationanimatorscript.
How generate animation with script code
Hello my doubt is the following, I want to make a character editor in 2d and for that I need to dynamically generate the animations, my problem is that the generated animation doesn't playing. It seems that we need to add something when generating the animation but I not find that, I created an animation with the editor and both seem to be the same and this second one playing correctly. This is the code, someone has any ideas. please and thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
using System;
using System.Linq;
public class bodyPartConfig : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
try
{
Debug.Log("Init body part");
Sprite[] sprites = Resources.LoadAll<Sprite>("Sprites/character");
//Sprite example = sprites.Single(s => s.name == "example");
bool create;
create = fnCreateAnimationClip(sprites);
if (create)
{
AnimationClip anim = Resources.Load<AnimationClip>("anim");
Debug.Log(anim);
if (anim != null)
{
AnimatorController controller = fnCreateController(anim);
if (controller)
{
Debug.Log(Resources.Load<AnimatorController>("character"));
Animator animator = GetComponent<Animator>();
//animator.runtimeAnimatorController = controller;
animator.runtimeAnimatorController = Resources.Load<AnimatorController>("character");
}
else
{
Debug.LogError("Failed init animator controller");
}
}
else
{
Debug.LogError("Animation not found");
}
}
else
{
Debug.Log("Sin clip");
}
}
catch (Exception e)
{
Debug.LogError(e);
}
}
bool fnCreateAnimationClip(Sprite[] sprites)
{
try
{
AnimationClip animClip = new AnimationClip();
animClip.frameRate = 60;
EditorCurveBinding spriteBinding = new EditorCurveBinding();
spriteBinding.type = typeof(SpriteRenderer);
spriteBinding.path = "";
spriteBinding.propertyName = "m_Sprite";
ObjectReferenceKeyframe[] spriteKeyFrames = new ObjectReferenceKeyframe[sprites.Length];
for (int i = 0; i < (sprites.Length); i++)
{
float aux = ((i * 1.0f) / (sprites.Length));
spriteKeyFrames[i] = new ObjectReferenceKeyframe();
spriteKeyFrames[i].time = aux;
spriteKeyFrames[i].value = sprites[i];
}
AnimationClipSettings animClipSett = new AnimationClipSettings();
animClipSett.loopTime = true;
AnimationUtility.SetObjectReferenceCurve(animClip, spriteBinding, spriteKeyFrames);
AnimationUtility.SetAnimationClipSettings(animClip, animClipSett);
AssetDatabase.CreateAsset(animClip, "assets/Resources/anim.anim");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
return true;
}
catch(Exception e)
{
Debug.LogError(e);
return false;
}
}
AnimatorController fnCreateController(AnimationClip clip)
{
try
{
var controller = AnimatorController.CreateAnimatorControllerAtPath("assets/Resources/character.controller");
// Add StateMachines
var rootStateMachine = controller.layers[0].stateMachine;
//var stateMachineA = rootStateMachine.AddStateMachine("player");
// Add States
controller.AddMotion(clip);
return controller;
}
catch (Exception e)
{
Debug.LogError(e);
return null ;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612213712im_/https://answers.unity.com/themes/thub/images/avi.jpg)