Unity 利用代码动态控制帧动画,解决在UI中动画器不生效的问题

创建以下脚本组件,建议绑定到UI所在Canvas

using UnityEngine;
using UnityEngine.UI;
public class UIFrameAnimation : MonoBehaviour
{
public Image image; // 绑定 UI Image
public Sprite[] frames; // 帧动画的图片数组
public float frameRate = 0.1f; // 每帧的时间
private int currentFrame;
private float timer;
void Update()
{
if (frames.Length == 0) return;
timer += Time.deltaTime;
if (timer >= frameRate)
{
timer = 0;
currentFrame = (currentFrame + 1) % frames.Length;
image.sprite = frames[currentFrame];
}
}
}
发布于   xiins  阅读(50)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示