forked from Blightbuster/The-Forest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimateTiledTexture.cs
222 lines (162 loc) · 4.43 KB
/
AnimateTiledTexture.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimateTiledTexture : MonoBehaviour
{
public void RegisterCallback(AnimateTiledTexture.VoidEvent cbFunction)
{
if (this._enableEvents)
{
this._voidEventCallbackList.Add(cbFunction);
}
else
{
Debug.LogWarning("AnimateTiledTexture: You are attempting to register a callback but the events of this object are not enabled!");
}
}
public void UnRegisterCallback(AnimateTiledTexture.VoidEvent cbFunction)
{
if (this._enableEvents)
{
this._voidEventCallbackList.Remove(cbFunction);
}
else
{
Debug.LogWarning("AnimateTiledTexture: You are attempting to un-register a callback but the events of this object are not enabled!");
}
}
public void Play()
{
if (this._isPlaying)
{
base.StopCoroutine("updateTiling");
this._isPlaying = false;
}
base.GetComponent<Renderer>().enabled = true;
this._index = this._columns;
base.StartCoroutine(this.updateTiling());
}
public void ChangeMaterial(Material newMaterial, bool newInstance = false)
{
if (newInstance)
{
if (this._hasMaterialInstance)
{
UnityEngine.Object.Destroy(base.GetComponent<Renderer>().sharedMaterial);
}
this._materialInstance = new Material(newMaterial);
base.GetComponent<Renderer>().sharedMaterial = this._materialInstance;
this._hasMaterialInstance = true;
}
else
{
base.GetComponent<Renderer>().sharedMaterial = newMaterial;
}
this.CalcTextureSize();
base.GetComponent<Renderer>().sharedMaterial.SetTextureScale("_MainTex", this._textureSize);
}
private void Awake()
{
if (this._enableEvents)
{
this._voidEventCallbackList = new List<AnimateTiledTexture.VoidEvent>();
}
this.ChangeMaterial(base.GetComponent<Renderer>().sharedMaterial, this._newMaterialInstance);
}
private void OnDestroy()
{
if (this._hasMaterialInstance)
{
UnityEngine.Object.Destroy(base.GetComponent<Renderer>().sharedMaterial);
this._hasMaterialInstance = false;
}
}
private void HandleCallbacks(List<AnimateTiledTexture.VoidEvent> cbList)
{
for (int i = 0; i < cbList.Count; i++)
{
cbList[i]();
}
}
private void OnEnable()
{
this.CalcTextureSize();
if (this._playOnEnable)
{
this.Play();
}
}
private void CalcTextureSize()
{
this._textureSize = new Vector2(1f / (float)this._columns, 1f / (float)this._rows);
this._textureSize.x = this._textureSize.x / this._scale.x;
this._textureSize.y = this._textureSize.y / this._scale.y;
this._textureSize -= this._buffer;
}
private IEnumerator updateTiling()
{
this._isPlaying = true;
int checkAgainst = this._rows * this._columns;
for (;;)
{
if (this._index >= checkAgainst)
{
this._index = 0;
if (this._playOnce)
{
if (checkAgainst == this._columns)
{
break;
}
checkAgainst = this._columns;
}
}
this.ApplyOffset();
this._index++;
yield return new WaitForSeconds(1f / this._framesPerSecond);
}
if (this._enableEvents)
{
this.HandleCallbacks(this._voidEventCallbackList);
}
if (this._disableUponCompletion)
{
base.gameObject.GetComponent<Renderer>().enabled = false;
}
this._isPlaying = false;
yield break;
yield break;
}
private void ApplyOffset()
{
Vector2 value = new Vector2((float)this._index / (float)this._columns - (float)(this._index / this._columns), 1f - (float)(this._index / this._columns) / (float)this._rows);
if (value.y == 1f)
{
value.y = 0f;
}
value.x += (1f / (float)this._columns - this._textureSize.x) / 2f;
value.y += (1f / (float)this._rows - this._textureSize.y) / 2f;
value.x += this._offset.x;
value.y += this._offset.y;
base.GetComponent<Renderer>().sharedMaterial.SetTextureOffset("_MainTex", value);
}
public int _columns = 2;
public int _rows = 2;
public Vector2 _scale = new Vector3(1f, 1f);
public Vector2 _offset = Vector2.zero;
public Vector2 _buffer = Vector2.zero;
public float _framesPerSecond = 10f;
public bool _playOnce;
public bool _disableUponCompletion;
public bool _enableEvents;
public bool _playOnEnable = true;
public bool _newMaterialInstance;
private int _index;
private Vector2 _textureSize = Vector2.zero;
private Material _materialInstance;
private bool _hasMaterialInstance;
private bool _isPlaying;
private List<AnimateTiledTexture.VoidEvent> _voidEventCallbackList;
public delegate void VoidEvent();
}