转载自【黑米GameDev街区】 原文链接: http://www.himigame.com/unity3d-game/1592.html
Himi 尝试使用了此作者《CSharp – SpritSheet.cs》代码段,发现其中有一个算法估计是作者大意写错了。这样改了就矩形也都支持了。
1
2
3
4
5
6
|
<
span
class
=
"co1"
>
<
em
>
// split into horizontal and vertical index</em></span>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
uIndex
<
span
class
=
"sy0"
>=
<
/
span
>
index
<
span
class
=
"sy0"
>
%
<
/
span
>
_uvTieX
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
vIndex
<
span
class
=
"sy0"
>=
<
/
span
>
index
<
span
class
=
"sy0"
>
/
<
/
span
>
<
strong
>
<
span
style
=
"color: #ff0000;"
>
_uvTieY
<
/
span
>
<
/
strong
>
<
span
class
=
"sy0"
>
;
应改为:
<
/
span
>
|
1
2
3
|
<
span
class
=
"co1"
>
<
em
>
// split into horizontal and vertical index</em></span>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
uIndex
<
span
class
=
"sy0"
>=
<
/
span
>
index
<
span
class
=
"sy0"
>
%
<
/
span
>
_uvTieX
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
vIndex
<
span
class
=
"sy0"
>=
<
/
span
>
index
<
span
class
=
"sy0"
>
/
<
/
span
>
<
strong
>
<
span
style
=
"color: #ff0000;"
>
_uvTieX
<
/
span
>
<
/
strong
>
<
span
class
=
"sy0"
>
;
<
/
span
>
|
1
|
<
span
class
=
"sy0"
>
<
/
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<
span
class
=
"kw2"
>
<
strong
>
var
<
/
strong
>
<
/
span
>
uvAnimationTileX
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
24
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
//Here you can place the number of columns of your sheet. </em></span>
<
span
class
=
"co1"
>
<
em
>
//The above sheet has 24</em></span>
<
span
class
=
"kw2"
>
<
strong
>
var
<
/
strong
>
<
/
span
>
uvAnimationTileY
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
//Here you can place the number of rows of your sheet. </em></span>
<
span
class
=
"co1"
>
<
em
>
//The above sheet has 1</em></span>
<
span
class
=
"kw2"
>
<
strong
>
var
<
/
strong
>
<
/
span
>
framesPerSecond
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
10.0
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw2"
>
<
strong
>
function
<
/
strong
>
<
/
span
>
Update
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
{
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
// Calculate index</em></span>
<
span
class
=
"kw2"
>
<
strong
>
var
<
/
strong
>
<
/
span
>
index
<
span
class
=
"sy0"
>
:
<
/
span
>
int
<
span
class
=
"sy0"
>=
<
/
span
>
Time
.
<
span
class
=
"me1"
>
time
<
/
span
>
<
span
class
=
"sy0"
>
*
<
/
span
>
framesPerSecond
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
// repeat when exhausting all frames</em></span>
index
<
span
class
=
"sy0"
>=
<
/
span
>
index
<
span
class
=
"sy0"
>
%
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
uvAnimationTileX
<
span
class
=
"sy0"
>
*
<
/
span
>
uvAnimationTileY
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
// Size of every tile</em></span>
<
span
class
=
"kw2"
>
<
strong
>
var
<
/
strong
>
<
/
span
>
size
<
span
class
=
"sy0"
>=
<
/
span
>
Vector2
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"nu0"
>
1.0
<
/
span
>
<
span
class
=
"sy0"
>
/
<
/
span
>
uvAnimationTileX
<
span
class
=
"sy0"
>
,
<
/
span
>
<
span
class
=
"nu0"
>
1.0
<
/
span
>
<
span
class
=
"sy0"
>
/
<
/
span
>
uvAnimationTileY
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
// split into horizontal and vertical index</em></span>
<
span
class
=
"kw2"
>
<
strong
>
var
<
/
strong
>
<
/
span
>
uIndex
<
span
class
=
"sy0"
>=
<
/
span
>
index
<
span
class
=
"sy0"
>
%
<
/
span
>
uvAnimationTileX
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw2"
>
<
strong
>
var
<
/
strong
>
<
/
span
>
vIndex
<
span
class
=
"sy0"
>=
<
/
span
>
index
<
span
class
=
"sy0"
>
/
<
/
span
>
uvAnimationTileX
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
// build offset</em></span>
<
span
class
=
"co1"
>
<
em
>
// v coordinate is the bottom of the image in opengl so we need to invert.</em></span>
<
span
class
=
"kw2"
>
<
strong
>
var
<
/
strong
>
<
/
span
>
offset
<
span
class
=
"sy0"
>=
<
/
span
>
Vector2
<
span
class
=
"br0"
>
(
<
/
span
>
uIndex
<
span
class
=
"sy0"
>
*
<
/
span
>
size
.
<
span
class
=
"me1"
>
x
<
/
span
>
<
span
class
=
"sy0"
>
,
<
/
span
>
<
span
class
=
"nu0"
>
1.0
<
/
span
>
<
span
class
=
"sy0"
>
-
<
/
span
>
size
.
<
span
class
=
"me1"
>
y
<
/
span
>
<
span
class
=
"sy0"
>
-
<
/
span
>
vIndex
<
span
class
=
"sy0"
>
*
<
/
span
>
size
.
<
span
class
=
"me1"
>
y
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
renderer
.
<
span
class
=
"me1"
>
material
<
/
span
>
.
<
span
class
=
"me1"
>
SetTextureOffset
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"st0"
>
"_MainTex"
<
/
span
>
<
span
class
=
"sy0"
>
,
<
/
span
>
offset
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
renderer
.
<
span
class
=
"me1"
>
material
<
/
span
>
.
<
span
class
=
"me1"
>
SetTextureScale
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"st0"
>
"_MainTex"
<
/
span
>
<
span
class
=
"sy0"
>
,
<
/
span
>
size
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
|
CSharp – SpritSheet.cs
This is just a CSharp version of the AnimatedTextureUV.js above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<
span
class
=
"kw1"
>
<
strong
>
public
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
class
<
/
strong
>
<
/
span
>
SpriteSheet
<
span
class
=
"sy0"
>
:
<
/
span
>
MonoBehaviour
<
span
class
=
"br0"
>
{
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
public
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
_uvTieX
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
public
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
_uvTieY
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
public
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
_fps
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
10
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
private
<
/
strong
>
<
/
span
>
Vector2
_size
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
private
<
/
strong
>
<
/
span
>
Renderer
_myRenderer
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
private
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
_lastIndex
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"sy0"
>
-
<
/
span
>
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
void
<
/
strong
>
<
/
span
>
Start
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
{
<
/
span
>
_size
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"kw3"
>
new
<
/
span
>
Vector2
<
span
class
=
"br0"
>
(
<
/
span
>
1
<
span
class
=
"sy0"
>
.
<
/
span
>
0f
<
span
class
=
"sy0"
>
/
<
/
span
>
_uvTieX
,
1
<
span
class
=
"sy0"
>
.
<
/
span
>
0f
<
span
class
=
"sy0"
>
/
<
/
span
>
_uvTieY
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
_myRenderer
<
span
class
=
"sy0"
>=
<
/
span
>
renderer
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
if
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
_myRenderer
<
span
class
=
"sy0"
>=
=
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
null
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
enabled
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
false
<
/
strong
>
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
// Update is called once per frame</em></span>
<
span
class
=
"kw4"
>
<
strong
>
void
<
/
strong
>
<
/
span
>
Update
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
{
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
// Calculate index</em></span>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
index
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
Time
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
timeSinceLevelLoad
<
/
span
>
<
span
class
=
"sy0"
>
*
<
/
span
>
_fps
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
%
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
_uvTieX
<
span
class
=
"sy0"
>
*
<
/
span
>
_uvTieY
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
if
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
index
<
span
class
=
"sy0"
>
!=
<
/
span
>
_lastIndex
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
{
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
// split into horizontal and vertical index</em></span>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
uIndex
<
span
class
=
"sy0"
>=
<
/
span
>
index
<
span
class
=
"sy0"
>
%
<
/
span
>
_uvTieX
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
vIndex
<
span
class
=
"sy0"
>=
<
/
span
>
index
<
span
class
=
"sy0"
>
/
<
/
span
>
_uvTieY
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
// build offset</em></span>
<
span
class
=
"co1"
>
<
em
>
// v coordinate is the bottom of the image in opengl so we need to invert.</em></span>
Vector2
offset
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"kw3"
>
new
<
/
span
>
Vector2
<
span
class
=
"br0"
>
(
<
/
span
>
uIndex
<
span
class
=
"sy0"
>
*
<
/
span
>
_size
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
x
<
/
span
>
,
1
<
span
class
=
"sy0"
>
.
<
/
span
>
0f
<
span
class
=
"sy0"
>
-
<
/
span
>
_size
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
y
<
/
span
>
<
span
class
=
"sy0"
>
-
<
/
span
>
vIndex
<
span
class
=
"sy0"
>
*
<
/
span
>
_size
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
y
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
_myRenderer
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
material
<
/
span
>
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
SetTextureOffset
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"st0"
>
"_MainTex"
<
/
span
>
,
offset
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
_myRenderer
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
material
<
/
span
>
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
SetTextureScale
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"st0"
>
"_MainTex"
<
/
span
>
,
_size
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
_lastIndex
<
span
class
=
"sy0"
>=
<
/
span
>
index
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
|
CSharp – SpritSheetNG.cs
The CSharp version of the script was not working with multiple rows so i made some changes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<
span
class
=
"kw1"
>
<
strong
>
public
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
class
<
/
strong
>
<
/
span
>
SpriteSheetNG
<
span
class
=
"sy0"
>
:
<
/
span
>
MonoBehaviour
<
span
class
=
"br0"
>
{
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
private
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
float
<
/
strong
>
<
/
span
>
iX
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
0
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
private
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
float
<
/
strong
>
<
/
span
>
iY
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
public
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
_uvTieX
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
public
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
_uvTieY
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
public
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
_fps
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
10
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
private
<
/
strong
>
<
/
span
>
Vector2
_size
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
private
<
/
strong
>
<
/
span
>
Renderer
_myRenderer
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
private
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
_lastIndex
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"sy0"
>
-
<
/
span
>
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
void
<
/
strong
>
<
/
span
>
Start
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
{
<
/
span
>
_size
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"kw3"
>
new
<
/
span
>
Vector2
<
span
class
=
"br0"
>
(
<
/
span
>
1
<
span
class
=
"sy0"
>
.
<
/
span
>
0f
<
span
class
=
"sy0"
>
/
<
/
span
>
_uvTieX
,
1
<
span
class
=
"sy0"
>
.
<
/
span
>
0f
<
span
class
=
"sy0"
>
/
<
/
span
>
_uvTieY
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
_myRenderer
<
span
class
=
"sy0"
>=
<
/
span
>
renderer
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
if
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
_myRenderer
<
span
class
=
"sy0"
>=
=
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
null
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
enabled
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
false
<
/
strong
>
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
_myRenderer
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
material
<
/
span
>
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
SetTextureScale
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"st0"
>
"_MainTex"
<
/
span
>
,
_size
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
void
<
/
strong
>
<
/
span
>
Update
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
{
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
index
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
Time
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
timeSinceLevelLoad
<
/
span
>
<
span
class
=
"sy0"
>
*
<
/
span
>
_fps
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
%
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
_uvTieX
<
span
class
=
"sy0"
>
*
<
/
span
>
_uvTieY
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
if
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
index
<
span
class
=
"sy0"
>
!=
<
/
span
>
_lastIndex
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
{
<
/
span
>
Vector2
offset
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"kw3"
>
new
<
/
span
>
Vector2
<
span
class
=
"br0"
>
(
<
/
span
>
iX
<
span
class
=
"sy0"
>
*
<
/
span
>
_size
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
x
<
/
span
>
,
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"sy0"
>
-
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
_size
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
y
<
/
span
>
<
span
class
=
"sy0"
>
*
<
/
span
>
iY
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
iX
<
span
class
=
"sy0"
>
++
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
if
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
iX
<
span
class
=
"sy0"
>
/
<
/
span
>
_uvTieX
<
span
class
=
"sy0"
>=
=
<
/
span
>
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
{
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
if
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
_uvTieY
<
span
class
=
"sy0"
>
!=
<
/
span
>
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
iY
<
span
class
=
"sy0"
>
++
;
<
/
span
>
iX
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
0
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
if
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
iY
<
span
class
=
"sy0"
>
/
<
/
span
>
_uvTieY
<
span
class
=
"sy0"
>=
=
<
/
span
>
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
{
<
/
span
>
iY
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
1
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
_myRenderer
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
material
<
/
span
>
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
SetTextureOffset
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"st0"
>
"_MainTex"
<
/
span
>
,
offset
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
_lastIndex
<
span
class
=
"sy0"
>=
<
/
span
>
index
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
|
CSharp – AnimateTiledTexture
A version using coroutines. Slightly faster since it doesn’t update every frame and only sets the texture scale once.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<
span
class
=
"kw1"
>
<
strong
>
using
<
/
strong
>
<
/
span
>
<
span
class
=
"co3"
>
UnityEngine
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
using
<
/
strong
>
<
/
span
>
<
span
class
=
"co3"
>
System
.
Collections
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
class
<
/
strong
>
<
/
span
>
AnimateTiledTexture
<
span
class
=
"sy0"
>
:
<
/
span
>
MonoBehaviour
<
span
class
=
"br0"
>
{
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
public
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
columns
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
2
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
public
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
rows
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
2
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
public
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
float
<
/
strong
>
<
/
span
>
framesPerSecond
<
span
class
=
"sy0"
>=
<
/
span
>
10f
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
//the current frame to display</em></span>
<
span
class
=
"kw1"
>
<
strong
>
private
<
/
strong
>
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
int
<
/
strong
>
<
/
span
>
index
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
0
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
void
<
/
strong
>
<
/
span
>
Start
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
{
<
/
span
>
StartCoroutine
<
span
class
=
"br0"
>
(
<
/
span
>
updateTiling
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
//set the tile size of the texture (in UV units), based on the rows and columns</em></span>
Vector2
size
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"kw3"
>
new
<
/
span
>
Vector2
<
span
class
=
"br0"
>
(
<
/
span
>
1f
<
span
class
=
"sy0"
>
/
<
/
span
>
columns
,
1f
<
span
class
=
"sy0"
>
/
<
/
span
>
rows
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
renderer
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
sharedMaterial
<
/
span
>
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
SetTextureScale
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"st0"
>
"_MainTex"
<
/
span
>
,
size
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
private
<
/
strong
>
<
/
span
>
IEnumerator
updateTiling
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
{
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
while
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
true
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"br0"
>
{
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
//move to the next index</em></span>
index
<
span
class
=
"sy0"
>
++
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
if
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
index
<
span
class
=
"sy0"
>
&
gt
;
=
<
/
span
>
rows
<
span
class
=
"sy0"
>
*
<
/
span
>
columns
<
span
class
=
"br0"
>
)
<
/
span
>
index
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"nu0"
>
0
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
//split into x and y indexes</em></span>
Vector2
offset
<
span
class
=
"sy0"
>=
<
/
span
>
<
span
class
=
"kw3"
>
new
<
/
span
>
Vector2
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
float
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
index
<
span
class
=
"sy0"
>
/
<
/
span
>
columns
<
span
class
=
"sy0"
>
-
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
index
<
span
class
=
"sy0"
>
/
<
/
span
>
columns
<
span
class
=
"br0"
>
)
<
/
span
>
,
<
span
class
=
"co1"
>
<
em
>
//x index</em></span>
<
span
class
=
"br0"
>
(
<
/
span
>
index
<
span
class
=
"sy0"
>
/
<
/
span
>
columns
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
/
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"kw4"
>
<
strong
>
float
<
/
strong
>
<
/
span
>
<
span
class
=
"br0"
>
)
<
/
span
>
rows
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"co1"
>
<
em
>
//y index</em></span>
renderer
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
sharedMaterial
<
/
span
>
<
span
class
=
"sy0"
>
.
<
/
span
>
<
span
class
=
"me1"
>
SetTextureOffset
<
/
span
>
<
span
class
=
"br0"
>
(
<
/
span
>
<
span
class
=
"st0"
>
"_MainTex"
<
/
span
>
,
offset
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
yield
<
/
strong
>
<
/
span
>
<
span
class
=
"kw1"
>
<
strong
>
return
<
/
strong
>
<
/
span
>
<
span
class
=
"kw3"
>
new
<
/
span
>
WaitForSeconds
<
span
class
=
"br0"
>
(
<
/
span
>
1f
<
span
class
=
"sy0"
>
/
<
/
span
>
framesPerSecond
<
span
class
=
"br0"
>
)
<
/
span
>
<
span
class
=
"sy0"
>
;
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
<
span
class
=
"br0"
>
}
<
/
span
>
|