【UNITY3D 游戏开发之四】有关实现2D帧序列帧播放相关—Animating Tiledtexture

简介:

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

 [hide

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):Torchanimation 135.png

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

TilesTestPNG.png

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






本文转自 xiaominghimi 51CTO博客,原文链接:http://blog.51cto.com/xiaominghimi/1638103,如需转载请自行联系原作者

目录
相关文章
|
3月前
|
vr&ar 开发工具 图形学
Unity引擎:收费模式和服务升级,为游戏开发带来更多可能性
Unity引擎:收费模式和服务升级,为游戏开发带来更多可能性
|
4月前
|
C# 图形学
【Unity】2D游戏-愤怒的小鸟教学实战(附源码和实现步骤 超详细)
【Unity】2D游戏-愤怒的小鸟教学实战(附源码和实现步骤 超详细)
139 1
|
6月前
|
vr&ar 开发工具 图形学
Unity引擎:收费模式和服务升级,为游戏开发带来更多可能性
Unity引擎:收费模式和服务升级,为游戏开发带来更多可能性
|
10月前
|
存储 图形学 Android开发
【游戏开发】使用unity创建2D游戏
Unity是一种非常强大的游戏引擎,可以帮助你创建各种类型的游戏,包括2D游戏。在本文中,我们将教你如何使用Unity创建2D游戏。首先,你需要确保你已经下载并安装了最新版本的Unity。如果你还没有安装Unity,可以从官方网站下载免费的Unity Hub来管理你的Unity版本。
388 0
|
10月前
|
图形学
【游戏开发】unity透明特效的制作方法
Unity是一种强大的游戏开发引擎,它支持许多不同的特效和图形效果。其中一种常用的特效是透明特效,它可以使游戏中的材质变得半透明或完全透明。在本文中,我们将介绍如何使用Unity创建透明特效。
516 0
|
10月前
|
JavaScript 前端开发 vr&ar
【游戏开发】unity入门教程
Unity是一款非常受欢迎的游戏引擎,它可以用于开发2D和3D游戏以及虚拟现实和增强现实应用程序。如果你是一个刚开始接触Unity的开发者,这篇教程将为你提供一些有用的指导。
220 0
|
10月前
|
前端开发 定位技术 图形学
【Ruby 2D】【unity learn】抬头显示血条
【Ruby 2D】【unity learn】抬头显示血条
|
10月前
|
机器人 图形学 Ruby
【unity learn】【Ruby 2D】角色发射飞弹
【unity learn】【Ruby 2D】角色发射飞弹
|
10月前
|
机器人 图形学 Ruby
【Ruby 2D】【unity learn】控制敌人随机运动以及动画控制
【Ruby 2D】【unity learn】控制敌人随机运动以及动画控制
|
机器人 图形学 Ruby
【Unity开发实战】—— 2D项目1 - Ruby‘s Adventure 游戏中动画制作(4-1)
【Unity开发实战】—— 2D项目1 - Ruby‘s Adventure 游戏中动画制作(4-1)
202 0
【Unity开发实战】—— 2D项目1 - Ruby‘s Adventure 游戏中动画制作(4-1)