【UNITY3D 遊戲開發之四】有關實現2D幀序列幀播放相關—Animating Tiledtexture

ourpush發表於2016-10-16

Himi 嘗試使用了此作者《CSharp – SpritSheet.cs》程式碼段,發現其中有一個演算法估計是作者大意寫錯了。這樣改了就矩形也都支援了。

<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// split into horizontal and vertical index</span></span>
<span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> uIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">%</span> _uvTieX<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
<span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> vIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> <span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;"><span style="border: 0px; margin: 0px; padding: 0px; color: rgb(255, 0, 0);">_uvTieY</span></span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;

應改為:
</span>
<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// split into horizontal and vertical index</span></span>
<span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> uIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">%</span> _uvTieX<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
<span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> vIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> <span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;"><span style="border: 0px; margin: 0px; padding: 0px; color: rgb(255, 0, 0);">_uvTieX</span></span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;"> </span>

 

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

<span class="kw2" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">var</span></span> uvAnimationTileX <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">24</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span> <span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">//Here you can place the number of columns of your sheet. </span></span>
                           <span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">//The above sheet has 24</span></span>
 
<span class="kw2" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">var</span></span> uvAnimationTileY <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span> <span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">//Here you can place the number of rows of your sheet. </span></span>
                          <span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">//The above sheet has 1</span></span>
<span class="kw2" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">var</span></span> framesPerSecond <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">10.0</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
<span class="kw2" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">function</span></span> Update <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
 
	<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// Calculate index</span></span>
	<span class="kw2" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">var</span></span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">:</span> int <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> Time.<span class="me1" style="border: 0px; margin: 0px; padding: 0px;">time</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span> framesPerSecond<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
	<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// repeat when exhausting all frames</span></span>
	index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">%</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>uvAnimationTileX <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span> uvAnimationTileY<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
	<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// Size of every tile</span></span>
	<span class="kw2" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">var</span></span> size <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> Vector2 <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1.0</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> uvAnimationTileX<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">,</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1.0</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> uvAnimationTileY<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
	<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// split into horizontal and vertical index</span></span>
	<span class="kw2" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">var</span></span> uIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">%</span> uvAnimationTileX<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
	<span class="kw2" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">var</span></span> vIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> uvAnimationTileX<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
	<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// build offset</span></span>
	<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// v coordinate is the bottom of the image in opengl so we need to invert.</span></span>
	<span class="kw2" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">var</span></span> offset <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> Vector2 <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>uIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span> size.<span class="me1" style="border: 0px; margin: 0px; padding: 0px;">x</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">,</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1.0</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">-</span> size.<span class="me1" style="border: 0px; margin: 0px; padding: 0px;">y</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">-</span> vIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span> size.<span class="me1" style="border: 0px; margin: 0px; padding: 0px;">y</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
	renderer.<span class="me1" style="border: 0px; margin: 0px; padding: 0px;">material</span>.<span class="me1" style="border: 0px; margin: 0px; padding: 0px;">SetTextureOffset</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="st0" style="border: 0px; margin: 0px; padding: 0px;">"_MainTex"</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">,</span> offset<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
	renderer.<span class="me1" style="border: 0px; margin: 0px; padding: 0px;">material</span>.<span class="me1" style="border: 0px; margin: 0px; padding: 0px;">SetTextureScale</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="st0" style="border: 0px; margin: 0px; padding: 0px;">"_MainTex"</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">,</span> size<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>

CSharp – SpritSheet.cs

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

<span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">public</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">class</span></span> SpriteSheet <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">:</span> MonoBehaviour 
<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
	<span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">public</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> _uvTieX <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
	<span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">public</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> _uvTieY <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
	<span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">public</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> _fps <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">10</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
	<span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">private</span></span> Vector2 _size<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
	<span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">private</span></span> Renderer _myRenderer<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
	<span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">private</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> _lastIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">-</span><span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
	<span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">void</span></span> Start <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span> 
	<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
		_size <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="kw3" style="border: 0px; margin: 0px; padding: 0px;">new</span> Vector2 <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>1<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span>0f <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> _uvTieX , 1<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span>0f <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> _uvTieY<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
		_myRenderer <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> renderer<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
		<span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">if</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>_myRenderer <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">==</span> <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">null</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>
			enabled <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">false</span></span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
	<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>
	<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// Update is called once per frame</span></span>
	<span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">void</span></span> Update<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>
	<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
		<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// Calculate index</span></span>
		<span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>Time<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">timeSinceLevelLoad</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span> _fps<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">%</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>_uvTieX <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span> _uvTieY<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
    	<span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">if</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">!=</span> _lastIndex<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>
		<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
			<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// split into horizontal and vertical index</span></span>
			<span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> uIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">%</span> _uvTieX<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
			<span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> vIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> _uvTieY<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
			<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// build offset</span></span>
			<span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">// v coordinate is the bottom of the image in opengl so we need to invert.</span></span>
			Vector2 offset <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="kw3" style="border: 0px; margin: 0px; padding: 0px;">new</span> Vector2 <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>uIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span> _size<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">x</span>, 1<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span>0f <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">-</span> _size<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">y</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">-</span> vIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span> _size<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">y</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
			_myRenderer<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">material</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">SetTextureOffset</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="st0" style="border: 0px; margin: 0px; padding: 0px;">"_MainTex"</span>, offset<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
			_myRenderer<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">material</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">SetTextureScale</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="st0" style="border: 0px; margin: 0px; padding: 0px;">"_MainTex"</span>, _size<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
			_lastIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> index<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
		<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>
	<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>
<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>

CSharp – SpritSheetNG.cs

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

<span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">public</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">class</span></span> SpriteSheetNG <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">:</span> MonoBehaviour
<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>	
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">private</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">float</span></span> iX<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span><span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">0</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">private</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">float</span></span> iY<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span><span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">public</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> _uvTieX <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">public</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> _uvTieY <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">public</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> _fps <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">10</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">private</span></span> Vector2 _size<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">private</span></span> Renderer _myRenderer<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">private</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> _lastIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">-</span><span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
    <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">void</span></span> Start <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>
    <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
        _size <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="kw3" style="border: 0px; margin: 0px; padding: 0px;">new</span> Vector2 <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>1<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span>0f <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> _uvTieX ,
                             1<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span>0f <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> _uvTieY<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
        _myRenderer <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> renderer<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
        <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">if</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>_myRenderer <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">==</span> <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">null</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span> enabled <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">false</span></span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
        _myRenderer<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">material</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">SetTextureScale</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="st0" style="border: 0px; margin: 0px; padding: 0px;">"_MainTex"</span>, _size<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
    <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>
 
 
 
    <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">void</span></span> Update<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>
    <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
        <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>Time<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">timeSinceLevelLoad</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span> _fps<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">%</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>_uvTieX <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span> _uvTieY<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
        <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">if</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">!=</span> _lastIndex<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>
        <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
            Vector2 offset <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="kw3" style="border: 0px; margin: 0px; padding: 0px;">new</span> Vector2<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>iX<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span>_size<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">x</span>,
                                         <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">-</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>_size<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">y</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span>iY<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
            iX<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">++;</span>
            <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">if</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>iX <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> _uvTieX <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">==</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>
            <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
                <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">if</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>_uvTieY<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">!=</span><span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>    iY<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">++;</span>
                iX<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span><span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">0</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
                <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">if</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>iY <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> _uvTieY <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">==</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>
                <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
                    iY<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span><span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
                <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>
            <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>
 
            _myRenderer<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">material</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">SetTextureOffset</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="st0" style="border: 0px; margin: 0px; padding: 0px;">"_MainTex"</span>, offset<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
 
            _lastIndex <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> index<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
        <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>
    <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>
<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>

CSharp – AnimateTiledTexture

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

<span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">using</span></span> <span class="co3" style="border: 0px; margin: 0px; padding: 0px;">UnityEngine</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
<span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">using</span></span> <span class="co3" style="border: 0px; margin: 0px; padding: 0px;">System.Collections</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
<span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">class</span></span> AnimateTiledTexture <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">:</span> MonoBehaviour
<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">public</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> columns <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">2</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">public</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> rows <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">2</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">public</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">float</span></span> framesPerSecond <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> 10f<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
    <span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">//the current frame to display</span></span>
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">private</span></span> <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">int</span></span> index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">0</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
    <span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">void</span></span> Start<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>
    <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
        StartCoroutine<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>updateTiling<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
        <span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">//set the tile size of the texture (in UV units), based on the rows and columns</span></span>
        Vector2 size <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="kw3" style="border: 0px; margin: 0px; padding: 0px;">new</span> Vector2<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>1f <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> columns, 1f <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> rows<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
        renderer<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">sharedMaterial</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">SetTextureScale</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="st0" style="border: 0px; margin: 0px; padding: 0px;">"_MainTex"</span>, size<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
    <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>
 
    <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">private</span></span> IEnumerator updateTiling<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>
    <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
        <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">while</span></span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">true</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>
        <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">{</span>
            <span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">//move to the next index</span></span>
            index<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">++;</span>
            <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">if</span></span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">>=</span> rows <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">*</span> columns<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>
                index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="nu0" style="border: 0px; margin: 0px; padding: 0px;">0</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
            <span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">//split into x and y indexes</span></span>
            Vector2 offset <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">=</span> <span class="kw3" style="border: 0px; margin: 0px; padding: 0px;">new</span> Vector2<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">float</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> columns <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">-</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> columns<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>, <span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">//x index</span></span>
                                          <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>index <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> columns<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span> <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="kw4" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">float</span></span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span>rows<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>          <span class="co1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-weight: bold;">//y index</span></span>
 
            renderer<span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">sharedMaterial</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">.</span><span class="me1" style="border: 0px; margin: 0px; padding: 0px;">SetTextureOffset</span><span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="st0" style="border: 0px; margin: 0px; padding: 0px;">"_MainTex"</span>, offset<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
 
            <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">yield</span></span> <span class="kw1" style="border: 0px; margin: 0px; padding: 0px;"><span style="border: 0px; margin: 0px; padding: 0px; font-size: 12.4342px;">return</span></span> <span class="kw3" style="border: 0px; margin: 0px; padding: 0px;">new</span> WaitForSeconds<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">(</span>1f <span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">/</span> framesPerSecond<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="sy0" style="border: 0px; margin: 0px; padding: 0px;">;</span>
        <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>
 
    <span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>
<span class="br0" style="border: 0px; margin: 0px; padding: 0px;">}</span>


轉載自【黑米GameDev街區】 原文連結: http://www.himigame.com/unity3d-game/1592.html 

【關於我們】

每天名企社招內推(微信眾號ourpush),專注於國內各大網際網路公司社會招聘內推。每天更新最新網際網路名企(包括但不限於網易遊戲、BAT、網易網際網路、小米、京東、樂視、攜程等名企)內推資訊,有技術崗、有產品崗、有運營崗、有設計崗、有互動崗、有銷售崗,更有其他N多相關崗位!更多內推資訊請掃描以下二維碼關注查閱。





相關文章