我对unity非常陌生,刚刚开始我的第一个项目.我想知道是否有可能通过脚本而不是手动更改动画中使用的精灵.

伪代码示例

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]);

因此,动画应该是动画like that

推荐答案

我用2个脚本制作了动画.


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.c

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();
        }
    }
}

Csharp相关问答推荐

如何禁用ASP.NET MVP按钮,以便无法使用开发人员控制台重新启用它

获取ASP.NET核心身份认证cookie名称

如何创建ASP.NET Core主机并在同一进程中运行请求

使用特定格式的JsonConvert序列化对象

解析需要HttpClient和字符串的服务

. NET Core DB vs JSON模型设计

在一个模拟上设置一个方法,该模拟具有一个参数,该参数是一个numc函数表达式

C#方法从AJAX调用接收NULL

每个http请求需要60秒,为什么?

C# CompareTo()和Compare()可以返回除-1和1以外的整数吗?

在swagger示例中添加默认数组列表

CA1508:';NULL=>;TRUE;始终为';TRUE';.移除或重构条件(S)以避免死代码

.NET:从XPath定位原始XML文档中的 node

在Docker容器中运行API项目时,无法本地浏览到index.html

在.NET8中如何反序列化为私有字段?

使用可空引用类型时C#接口实现错误

如何从SignalR获取连接客户端的域

Autofac -动态实例化:手动传递构造函数

避免在特定区域中设置Visual Studio代码的自动格式

反序列化我以前使用System.Text.Json序列化的文件时出现异常