-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextureScrolling.cs
91 lines (80 loc) · 1.61 KB
/
TextureScrolling.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
using UnityEngine;
[RequireComponent(typeof(Renderer))]
public class TextureScrolling : MonoBehaviour
{
public enum UpdateType
{
Update,
LateUpdate,
FixedUpdate
}
public float scrollSpeed = 0.5f;
public bool horizontal = true;
public bool vertical = false;
public UpdateType updateType = UpdateType.Update;
public bool ignoreTimeScale = true;
private Renderer _renderer;
private float scrollUpdate;
private Vector2 currentOffset;
private float usedTime;
void Start()
{
_renderer = GetComponent<Renderer>();
}
void Update()
{
if(updateType == UpdateType.Update)
{
DoScroll();
}
}
void LateUpdate()
{
if(updateType == UpdateType.LateUpdate)
{
DoScroll();
}
}
void FixedUpdate()
{
if(updateType == UpdateType.FixedUpdate)
{
DoScroll();
}
}
public void DoScroll()
{
if(ignoreTimeScale)
{
if(Time.inFixedTimeStep)
{
usedTime = Time.fixedUnscaledTime;
} else
{
usedTime = Time.unscaledTime;
}
} else
{
usedTime = Time.time;
}
scrollUpdate = usedTime * scrollSpeed;
if(horizontal == true && vertical == false)
{
currentOffset = new Vector2(scrollUpdate, 0);
} else if(horizontal == true && vertical == true)
{
currentOffset = new Vector2(scrollUpdate, scrollUpdate);
} else if(horizontal == false && vertical == true)
{
currentOffset = new Vector2(0, scrollUpdate);
} else if(horizontal == false && vertical == false)
{
currentOffset = new Vector2(0, 0);
}
var allMaterials = _renderer.materials;
foreach(var currentMaterial in allMaterials)
{
currentMaterial.mainTextureOffset = currentOffset;
}
}
}