【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多相關崗位!更多內推資訊請掃描以下二維碼關注查閱。
相關文章
- 序列幀&一鏡到底H5開發實現分析H5
- iOS 關鍵幀動畫iOS動畫
- 騰訊遊戲效能實戰案例分享之幀率陡變遊戲
- 幀同步遊戲的設計遊戲
- windows10遊戲幀數怎麼提高_windows10如何提高遊戲fps幀數Windows遊戲
- win10系統遊戲鎖幀怎麼回事 win10遊戲出現鎖幀的解決步驟Win10遊戲
- HarmonyOS:幀率和丟幀分析實踐
- UIImageView 序列幀動畫的實現以及記憶體的優化UIView動畫記憶體優化
- OpenGL 之 幀緩衝 使用實踐
- Win10系統玩lol遊戲幀數只有60幀的解決方法Win10遊戲
- WebSocket原理與實踐(四)--生成資料幀Web
- Swift 遊戲開發之「能否關個燈」(〇)Swift遊戲開發
- FPS遊戲制勝法寶,“硬”實力,“幀”能贏!遊戲
- CSS製作陰陽(偽元素/關鍵幀)CSS
- 如何使用 pyav 抽取 I-frame (關鍵幀)
- 今日談:通道複用、PPP幀、ARP協議的有關知識協議
- WiFi基礎(五):802.11幀結構與WiFi控制幀、管理幀、資料幀WiFi
- win10遊戲幀率上不去怎麼辦 win10玩遊戲幀率上不去解決方法Win10遊戲
- win10玩遊戲卡幀怎麼解決_win10玩遊戲掉幀嚴重處理方法Win10遊戲
- PerfDog可以助力高幀率遊戲生態更全面發展遊戲
- Swift 遊戲開發之「能否關個燈」(一)Swift遊戲開發
- Unity3D相關面試題Unity3D面試題
- CSS動畫屬性關鍵幀keyframes全解析CSS動畫
- css3實現逐幀動畫CSSS3動畫
- 手機玩遊戲卡頓發熱掉幀讓體驗大打折扣 選對芯是關鍵遊戲
- 混合開發之phonegap開發相關知識解析
- 【逐幀分析】《黑神話:悟空》gameplay相關的技術和調整細節整理GAM
- [Unity3D] 2D畫素遊戲(一) Hello Unity!Unity3D遊戲
- 「譯」 MotionLayout 介紹 (Part IV) 深入理解關鍵幀
- 敏捷開發相關敏捷
- requestAnimationFrame實現一幀的函式節流requestAnimationFrame函式
- FFmpeg開發筆記(三十)解析H.264碼流中的SPS幀和PPS幀筆記
- 網路遊戲同步方式(幀同步和狀態同步)遊戲
- Unity3D開發入門教程(四)——用Lua實現元件Unity3D元件
- 關於四川哪裡有開發票
- 【Unity3D開發小遊戲】《戰棋小遊戲》Unity開發教程Unity3D遊戲
- wasm + ffmpeg實現前端擷取視訊幀功能ASM前端
- 一鍵提高遊戲幀數到底是什麼魔法?遊戲
- 每幀都是AI實時生成的,全球首款AI遊戲問世了!AI遊戲