【UNITY3D 遊戲開發之四】有關實現2D幀序列幀播放相關—Animating Tiledtexture
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):
(Leo Nogueira) Adding a simple image with multiple rows for testing purposes and a modified version of the C# Script:
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多相關崗位!更多內推資訊請掃描以下二維碼關注查閱。
相關文章
- Unity3D中實現幀同步 - Part 2Unity3D
- Flash8教程3:幀和關鍵幀
- iOS 關鍵幀動畫iOS動畫
- 序列幀&一鏡到底H5開發實現分析H5
- Flash8教程3:幀和關鍵幀(作者司徒子瑄10歲)
- UIImageView 序列幀動畫的實現以及記憶體的優化UIView動畫記憶體優化
- HarmonyOS:幀率和丟幀分析實踐
- CSS製作陰陽(偽元素/關鍵幀)CSS
- Unity3D 2D實戰遊戲開發Unity3D遊戲開發
- OpenGL 之 幀緩衝 使用實踐
- WebSocket原理與實踐(四)--生成資料幀Web
- css3實現逐幀動畫CSSS3動畫
- css3 實現逐幀動畫CSSS3動畫
- WiFi基礎(五):802.11幀結構與WiFi控制幀、管理幀、資料幀WiFi
- 今日談:通道複用、PPP幀、ARP協議的有關知識協議
- CSS動畫屬性關鍵幀keyframes全解析CSS動畫
- html5--6-55 動畫效果-關鍵幀動畫HTML動畫
- 【逐幀分析】《黑神話:悟空》gameplay相關的技術和調整細節整理GAM
- Unity3D相關面試題Unity3D面試題
- requestAnimationFrame實現一幀的函式節流requestAnimationFrame函式
- 「譯」 MotionLayout 介紹 (Part IV) 深入理解關鍵幀
- Android開發之幀動畫Android動畫
- ucgui 字型相關實現GUI
- wasm + ffmpeg實現前端擷取視訊幀功能ASM前端
- Unity幀動畫Unity動畫
- Android播放聊天語音訊息幀動畫問題Android音訊動畫
- CSS3動畫實戰之多關鍵幀實現無限迴圈動效的時間間隔CSSS3動畫
- CSMA/CD 以太幀最小幀長計算問題
- CSS3動畫之逐幀動畫CSSS3動畫
- 騰訊遊戲效能實戰案例分享之幀率陡變遊戲
- pyav 拆幀速度和執行緒數目的關係測試執行緒
- 幀動畫的多種實現方式與效能對比動畫
- View動畫、幀動畫View動畫
- 幀中繼(FR)中繼
- Flutter實戰之非同步相關Flutter非同步
- 一起動手實現一個js幀動畫庫JS動畫
- OpenCV&Qt學習之四——OpenCV 實現人臉檢測與相關知識整理OpenCVQT
- 字串相關函式的實現字串函式