unity3d 实现帧序列精灵动画

<pre name="code" class="csharp">using UnityEngine;
using System.Collections;
//现只适用 一行一列精灵图
public class SpriteAni : MonoBehaviour {

	public float timer = 0; //切换下一帧 计时器
	public float frameNumber = 30;//每秒多少帧
	public int frameCounts = 3; //总帧

	public int currentFrameIndex = 0; //当前帧
	public float frameIndexOffsetX = 0.33333f; //== Tilling x  1/frameCounts动画总数
	public float frameIndexOffsetY = 0.0f; //== Tilling y

	private float currentFrameOffsetX = 0.0f;
	private float currentFrameOffsetY = 0.0f;
	// Use this for initialization
	void Start () {
	}
	// Update is called once per frame
	void Update () {
		timer += Time.deltaTime;
		if(timer >= 1.0f/frameNumber)
		{
			currentFrameIndex ++;
			timer -= 1.0f/frameNumber;
			currentFrameIndex = currentFrameIndex%frameCounts;
			currentFrameOffsetX = frameIndexOffsetX*currentFrameIndex;
			currentFrameOffsetY = frameIndexOffsetY*currentFrameIndex;
			this.renderer.material.SetTextureOffset("_MainTex", new Vector2(currentFrameOffsetX,currentFrameOffsetY));
		}
	
	}
}


 

                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity DOTS(Data-Oriented Technology Stack)中,序列帧动画可以使用ECS(Entity Component System)和Burst Compiler来提高游戏性能。以下是基本步骤: 1. 创建一个Animation组件,用于存储动画的信息。 ``` [Serializable] public struct Animation : IComponentData { public int frameCount; public float frameTime; public float elapsedTime; } ``` 2. 创建一个AnimationSystem系统,用于更新动画的信息。 ``` public class AnimationSystem : SystemBase { protected override void OnUpdate() { float deltaTime = Time.DeltaTime; Entities.ForEach((ref Animation animation) => { animation.elapsedTime += deltaTime; int frameIndex = (int)(animation.elapsedTime / animation.frameTime) % animation.frameCount; // 更新动画 }).ScheduleParallel(); } } ``` 3. 创建一个RenderMesh组件,用于显示动画。 ``` public struct RenderMesh : IComponentData { public Mesh mesh; public Material material; public int subMesh; } ``` 4. 创建一个RenderSystem系统,用于显示动画。 ``` public class RenderSystem : SystemBase { private EndSimulationEntityCommandBufferSystem _entityCommandBufferSystem; protected override void OnCreate() { _entityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>(); } protected override void OnUpdate() { EntityCommandBuffer commandBuffer = _entityCommandBufferSystem.CreateCommandBuffer(); Entities.ForEach((Entity entity, ref RenderMesh renderMesh, in Animation animation) => { // 更新RenderMesh的Mesh和Material commandBuffer.SetSharedComponent(entity, new RenderMesh { mesh = ..., // 根据动画更新Mesh material = renderMesh.material, subMesh = renderMesh.subMesh }); }).ScheduleParallel(); } } ``` 5. 在场景中创建一个实体,并添加Animation和RenderMesh组件。 ``` Entity entity = EntityManager.CreateEntity(); EntityManager.AddComponentData(entity, new Animation { frameCount = ..., // 动画数 frameTime = ..., // 动画时间间隔 elapsedTime = 0f }); EntityManager.AddSharedComponentData(entity, new RenderMesh { mesh = ..., // 初始Mesh material = ..., // 初始Material subMesh = 0 }); ``` 这样,序列帧动画就可以在Unity DOTS中实现了。注意,这只是一个基本示例,具体实现可能会因游戏需求而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值