这个脚本动画纹理瓷砖动画。你可以给它一个帧率,以确定动画的速度,并设置有多少砖上的X,Y。
将这个脚本的对象,有一个平铺纹理的材料。为了避免失真,物体的比例必须是每瓦下面的表(如1:2)的比例相同。
下面是一个例子,如何奠定了一个纹理,它(感谢BigBrainz提供):
(利奥诺盖拉)用于测试目的和C#脚本的修改版本多行添加一个简单的图像:
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 1
var 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;
}
} |