Home > Software design >  Create unity 2d animation via c# script
Create unity 2d animation via c# script

Time:06-12

I am extremely new to unity and just started working on my first project. I was wondering if there is a possibility to change sprites used in an animation via script instead of doing it by hand.

Example of a pseudocode

Sprite[] animSr = new Sprite[2]{SpriteA, SpriteB};
int animSampleRate = 60;
int[] framesIndex = new int[2]{ 0, 60};
Animation anim = new Animation();

//pseudocode
anim.SetSampleRate(animSampleRate);
anim.UpdateFrame(Sprite = animSr[0], FrameInd = framesIndex[0]);
anim.UpdateFrame(Sprite = animSr[1], FrameInd = framesIndex[1]);

so as a result anim should be an Animation like that

CodePudding user response:

I have made animations with 2 scripts. AnimationGerator.cs


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class AnimationGenerator : MonoBehaviour
{
    private static AnimationGenerator a_instance;
    public static AnimationGenerator Instance
    {
        get
        {
            return a_instance;
        }
    }
    public List<Sprite> Sprites;
    private void Start()
    {
        a_instance = this;
    }
    public AnimationClip Generate()
    {
        AnimationClip animClip = new AnimationClip();
        animClip.frameRate = 25;   // FPS
        EditorCurveBinding spriteBinding = new EditorCurveBinding();
        spriteBinding.type = typeof(Image);
        spriteBinding.path = "";
        spriteBinding.propertyName = "m_Sprite";
        ObjectReferenceKeyframe[] spriteKeyFrames = new ObjectReferenceKeyframe[Sprites.Count];
        for (int i = 0; i <Sprites.Count; i  )
        {
            spriteKeyFrames[i] = new ObjectReferenceKeyframe();
            spriteKeyFrames[i].time = ((float)i / 25); 
            spriteKeyFrames[i].value = Sprites[i];
        }
        AnimationUtility.SetObjectReferenceCurve(animClip, spriteBinding, spriteKeyFrames);
        return animClip;
    }

    public void ClearSprites()
    {
        Sprites.Clear();
    }
}

AnimationGeneratorEditor.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public class AnimationGeneratorEditor : EditorWindow
{
    public GameObject obj = null;
    string objNames = "";
    [MenuItem("Animation/AnimationGenerator")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(AnimationGeneratorEditor));
    }
    static void Initialize()
    {
        AnimationGeneratorEditor window = (AnimationGeneratorEditor)EditorWindow.GetWindow(
            typeof(AnimationGeneratorEditor),
            true,
            "Animation Generating Tools"
        );
        window.position = new Rect(0, 0, 250, 150);
    }

    public void OnGUI()
    {
        string path = "";
        objNames = EditorGUI.TextField(new Rect(10, 25, position.width - 20, 20),
            path,
        objNames);
        if (GUI.Button(new Rect(10, 45, position.width - 20, 20), "Generate"))
        {
            AnimationClip animClip = FindObjectOfType<AnimationGenerator>().Generate();
            AssetDatabase.CreateAsset(animClip, "assets/"   objNames   ".anim");
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
        if (GUI.Button(new Rect(10, 68, position.width - 20, 20), "Clear Sprites"))
        {
            FindObjectOfType<AnimationGenerator>().ClearSprites();
        }
    }
}
  • Related