博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【UNITY3D 游戏开发之四】有关实现2D帧序列帧播放相关—Animating Tiledtexture
阅读量:6958 次
发布时间:2019-06-27

本文共 5553 字,大约阅读时间需要 18 分钟。

本站文章均为原创,转载务必在明显处注明:(作者新浪微博:) 
转载自 原文链接: 
 

 本博客最新动态!及时将最新博文通知您!


                 

 

Himi 尝试使用了此作者《CSharp – SpritSheet.cs》代码段,发现其中有一个算法估计是作者大意写错了。这样改了就矩形也都支持了。

// split into horizontal and vertical indexint uIndex = index % _uvTieX;int vIndex = index / _uvTieY;应改为:
// split into horizontal and vertical indexint uIndex = index % _uvTieX;int vIndex = index / _uvTieX;
 

 Author: Joachim Ante

Contents

 [] 

Description

This script animates a texture containing tiles of an animation. You can give it a framerate to determine the speed of the animation and set how many tiles on x, y there are.

Usage

Attach this script to the object that has a material with the tiled texture. To avoid distortion, the proportions of the object must be the same as the proportions of each tile (eg 1:2 for the sheet below).

Here is an example of how to lay out a texture for it (Thanks to BigBrainz for providing it):

(Leo Nogueira) Adding a simple image with multiple rows for testing purposes and a modified version of the C# Script:

JavaScript – AnimatedTextureUV.js

var uvAnimationTileX = 24; //Here you can place the number of columns of your sheet.                            //The above sheet has 24 var uvAnimationTileY = 1; //Here you can place the number of rows of your sheet.                           //The above sheet has 1var framesPerSecond = 10.0; function Update () { 	// Calculate index	var index : int = Time.time * framesPerSecond;	// repeat when exhausting all frames	index = index % (uvAnimationTileX * uvAnimationTileY); 	// Size of every tile	var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY); 	// split into horizontal and vertical index	var uIndex = index % uvAnimationTileX;	var vIndex = index / uvAnimationTileX; 	// build offset	// v coordinate is the bottom of the image in opengl so we need to invert.	var offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y); 	renderer.material.SetTextureOffset ("_MainTex", offset);	renderer.material.SetTextureScale ("_MainTex", size);}

CSharp – SpritSheet.cs

This is just a CSharp version of the AnimatedTextureUV.js above.

public class SpriteSheet : MonoBehaviour {	public int _uvTieX = 1;	public int _uvTieY = 1;	public int _fps = 10; 	private Vector2 _size;	private Renderer _myRenderer;	private int _lastIndex = -1; 	void Start () 	{		_size = new Vector2 (1.0f / _uvTieX , 1.0f / _uvTieY);		_myRenderer = renderer;		if(_myRenderer == null)			enabled = false;	}	// Update is called once per frame	void Update()	{		// Calculate index		int index = (int)(Time.timeSinceLevelLoad * _fps) % (_uvTieX * _uvTieY);    	if(index != _lastIndex)		{			// split into horizontal and vertical index			int uIndex = index % _uvTieX;			int vIndex = index / _uvTieY; 			// build offset			// v coordinate is the bottom of the image in opengl so we need to invert.			Vector2 offset = new Vector2 (uIndex * _size.x, 1.0f - _size.y - vIndex * _size.y); 			_myRenderer.material.SetTextureOffset ("_MainTex", offset);			_myRenderer.material.SetTextureScale ("_MainTex", _size); 			_lastIndex = index;		}	}}

CSharp – SpritSheetNG.cs

The CSharp version of the script was not working with multiple rows so i made some changes.

public class SpriteSheetNG : MonoBehaviour{	    private float iX=0;    private float iY=1;    public int _uvTieX = 1;    public int _uvTieY = 1;    public int _fps = 10;    private Vector2 _size;    private Renderer _myRenderer;    private int _lastIndex = -1;     void Start ()    {        _size = new Vector2 (1.0f / _uvTieX ,                             1.0f / _uvTieY);         _myRenderer = renderer;         if(_myRenderer == null) enabled = false;         _myRenderer.material.SetTextureScale ("_MainTex", _size);    }       void Update()    {        int index = (int)(Time.timeSinceLevelLoad * _fps) % (_uvTieX * _uvTieY);         if(index != _lastIndex)        {            Vector2 offset = new Vector2(iX*_size.x,                                         1-(_size.y*iY));            iX++;            if(iX / _uvTieX == 1)            {                if(_uvTieY!=1)    iY++;                iX=0;                if(iY / _uvTieY == 1)                {                    iY=1;                }            }             _myRenderer.material.SetTextureOffset ("_MainTex", offset);              _lastIndex = index;        }    }}

CSharp – AnimateTiledTexture

A version using coroutines. Slightly faster since it doesn’t update every frame and only sets the texture scale once.

using UnityEngine;using System.Collections; class AnimateTiledTexture : MonoBehaviour{    public int columns = 2;    public int rows = 2;    public float framesPerSecond = 10f;     //the current frame to display    private int index = 0;     void Start()    {        StartCoroutine(updateTiling());         //set the tile size of the texture (in UV units), based on the rows and columns        Vector2 size = new Vector2(1f / columns, 1f / rows);        renderer.sharedMaterial.SetTextureScale("_MainTex", size);    }     private IEnumerator updateTiling()    {        while (true)        {            //move to the next index            index++;            if (index >= rows * columns)                index = 0;             //split into x and y indexes            Vector2 offset = new Vector2((float)index / columns - (index / columns), //x index                                          (index / columns) / (float)rows);          //y index             renderer.sharedMaterial.SetTextureOffset("_MainTex", offset);             yield return new WaitForSeconds(1f / framesPerSecond);        }     }}

你可能感兴趣的文章
西数企业级市场大救星:充氦硬盘
查看>>
中国人工智能学会通讯——人工智能在各医学亚专科的发展现状及趋势 1.5 人工智能在各医学亚专科应用的局限性...
查看>>
《VMware Virtual SAN权威指南》一1.4 Virtual SAN简介
查看>>
ClassFlow推出全新课堂活动轨迹功能
查看>>
高性能计算在电网技术中的应用
查看>>
亿级下ApsaraDB HBase Phoenix秒级内RT在大搜车实践
查看>>
EMC高管:戴尔Nutanix一体机与EMC超融合产品之间不存在竞争
查看>>
十分钟看懂时序数据库II——预处理
查看>>
O2O探秘:实体店背后的科学
查看>>
《构建实时机器学习系统》一3.2 Pandas 的安装
查看>>
创新驱动转型发展 江波龙科技携手IBM构建物联网云数据服务系统
查看>>
揭秘eBay四大系统 从行为数据中寻找价值
查看>>
Google 404页面暗藏漏洞,可泄漏服务器内部信息
查看>>
排除网络故障的十大真理
查看>>
辉瑞健康药物部联手易传媒 打造大数据管理平台
查看>>
CIO们从云中学到的那些经验教训
查看>>
混合云和多云管理不再难:基础架构即代码来帮忙
查看>>
大数据能否解决城市所面临的环境问题
查看>>
数据库安全需要遵循的8项最佳实践
查看>>
关于HTTP推送的一些问题
查看>>