-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathUIParticleSystem.cs
More file actions
94 lines (68 loc) · 3.07 KB
/
UIParticleSystem.cs
File metadata and controls
94 lines (68 loc) · 3.07 KB
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
namespace UnityEngine.UI.Windows {
public class UIParticleSystem : MonoBehaviour {
public WindowObject windowObject;
public int sortingOrder;
public string sortingLayerName;
public ParticleSystemRenderer particleSystemRenderer;
public CanvasGroup alphaCanvasGroup;
public ParticleSystem[] particleSystems;
private float prevAlpha;
public void OnValidate() {
if (this.particleSystemRenderer == null) this.particleSystemRenderer = this.GetComponent<ParticleSystemRenderer>();
this.particleSystems = this.GetComponentsInChildren<ParticleSystem>(true);
}
public void OnEnable() {
if (this.windowObject.GetState() >= ObjectState.Showing) {
this.ApplyOrder(this.windowObject);
} else {
WindowSystem.GetEvents().RegisterOnce(this.windowObject, WindowEvent.OnShowBegin, this.ApplyOrder);
}
}
private void ApplyOrder(WindowObject obj) {
this.particleSystemRenderer.sortingOrder = this.windowObject.GetWindow().GetCanvasOrder() + this.sortingOrder;
if (string.IsNullOrEmpty(this.sortingLayerName) == false) this.particleSystemRenderer.sortingLayerName = this.sortingLayerName;
}
private void Update() {
if (this.windowObject.GetState() is ObjectState.Showing or ObjectState.Shown or ObjectState.Hiding && this.alphaCanvasGroup != null) {
var currentAlpha = this.alphaCanvasGroup.alpha;
if (currentAlpha != this.prevAlpha) {
this.prevAlpha = currentAlpha;
this.ApplyAlpha(currentAlpha);
}
}
}
private static readonly ParticleSystem.Particle[] particles = new ParticleSystem.Particle[1000];
private void ApplyAlpha(float alpha) {
foreach (var system in this.particleSystems) {
var systemMain = system.main;
int count = system.GetParticles(particles);
for (int i = 0; i < count; i++) {
ref var particle = ref particles[i];
{
var color = particle.startColor;
color.a = (byte)(alpha * 255);
particle.startColor = color;
}
}
system.SetParticles(particles, count);
var startColor = systemMain.startColor;
{
var color = startColor.color;
color.a = alpha;
startColor.color = color;
}
{
var color = startColor.colorMin;
color.a = alpha;
startColor.colorMin = color;
}
{
var color = startColor.colorMax;
color.a = alpha;
startColor.colorMax = color;
}
systemMain.startColor = startColor;
}
}
}
}