2020-08-20 03:42:16 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Coffee.UIExtensions
|
|
|
|
{
|
2020-10-01 15:15:57 +08:00
|
|
|
internal static class UIParticleUpdater
|
2020-08-20 03:42:16 +08:00
|
|
|
{
|
|
|
|
static readonly List<UIParticle> s_ActiveParticles = new List<UIParticle>();
|
2022-06-15 09:24:31 +08:00
|
|
|
static readonly List<UIParticleAttractor> s_ActiveAttractors = new List<UIParticleAttractor>();
|
2022-06-11 22:10:17 +08:00
|
|
|
static readonly HashSet<int> s_UpdatedGroupIds = new HashSet<int>();
|
2021-06-03 02:03:16 +08:00
|
|
|
private static int frameCount = 0;
|
2020-08-28 13:38:13 +08:00
|
|
|
|
2022-06-11 21:32:14 +08:00
|
|
|
public static int uiParticleCount
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return s_ActiveParticles.Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 03:42:16 +08:00
|
|
|
public static void Register(UIParticle particle)
|
|
|
|
{
|
|
|
|
if (!particle) return;
|
|
|
|
s_ActiveParticles.Add(particle);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Unregister(UIParticle particle)
|
|
|
|
{
|
|
|
|
if (!particle) return;
|
|
|
|
s_ActiveParticles.Remove(particle);
|
|
|
|
}
|
|
|
|
|
2022-06-15 09:24:31 +08:00
|
|
|
public static void Register(UIParticleAttractor attractor)
|
|
|
|
{
|
|
|
|
if (!attractor) return;
|
|
|
|
s_ActiveAttractors.Add(attractor);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Unregister(UIParticleAttractor attractor)
|
|
|
|
{
|
|
|
|
if (!attractor) return;
|
|
|
|
s_ActiveAttractors.Remove(attractor);
|
|
|
|
}
|
|
|
|
|
2020-08-20 03:42:16 +08:00
|
|
|
#if UNITY_EDITOR
|
|
|
|
[UnityEditor.InitializeOnLoadMethod]
|
|
|
|
#endif
|
|
|
|
[RuntimeInitializeOnLoadMethod]
|
|
|
|
private static void InitializeOnLoad()
|
|
|
|
{
|
|
|
|
Canvas.willRenderCanvases -= Refresh;
|
|
|
|
Canvas.willRenderCanvases += Refresh;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void Refresh()
|
|
|
|
{
|
2021-06-03 02:03:16 +08:00
|
|
|
// Do not allow it to be called in the same frame.
|
|
|
|
if (frameCount == Time.frameCount) return;
|
|
|
|
frameCount = Time.frameCount;
|
|
|
|
|
2022-06-11 22:10:17 +08:00
|
|
|
// Simulate -> Primary
|
2020-08-20 03:42:16 +08:00
|
|
|
for (var i = 0; i < s_ActiveParticles.Count; i++)
|
|
|
|
{
|
2022-06-11 21:32:14 +08:00
|
|
|
var uip = s_ActiveParticles[i];
|
2022-06-11 22:10:17 +08:00
|
|
|
if (!uip.isPrimary || s_UpdatedGroupIds.Contains(uip.groupId)) continue;
|
|
|
|
|
|
|
|
s_UpdatedGroupIds.Add(uip.groupId);
|
|
|
|
uip.UpdateTransformScale();
|
|
|
|
uip.UpdateRenderers();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simulate -> Others
|
|
|
|
for (var i = 0; i < s_ActiveParticles.Count; i++)
|
|
|
|
{
|
|
|
|
var uip = s_ActiveParticles[i];
|
|
|
|
uip.UpdateTransformScale();
|
|
|
|
|
|
|
|
if (!uip.useMeshSharing)
|
2020-08-20 03:42:16 +08:00
|
|
|
{
|
2022-06-11 21:32:14 +08:00
|
|
|
uip.UpdateRenderers();
|
2020-08-20 03:42:16 +08:00
|
|
|
}
|
2022-06-11 22:10:17 +08:00
|
|
|
else if (!s_UpdatedGroupIds.Contains(uip.groupId))
|
2020-08-20 03:42:16 +08:00
|
|
|
{
|
2022-06-11 22:10:17 +08:00
|
|
|
s_UpdatedGroupIds.Add(uip.groupId);
|
|
|
|
uip.UpdateRenderers();
|
2020-08-20 03:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
2022-06-11 22:10:17 +08:00
|
|
|
|
|
|
|
s_UpdatedGroupIds.Clear();
|
2022-06-15 09:24:31 +08:00
|
|
|
|
|
|
|
// Attract
|
|
|
|
for(var i = 0;i< s_ActiveAttractors.Count;i++)
|
|
|
|
{
|
|
|
|
s_ActiveAttractors[i].Attract();
|
|
|
|
}
|
2022-06-16 21:43:23 +08:00
|
|
|
|
|
|
|
// UpdateParticleCount.
|
|
|
|
for (var i = 0; i < s_ActiveParticles.Count; i++)
|
|
|
|
{
|
|
|
|
s_ActiveParticles[i].UpdateParticleCount();
|
|
|
|
}
|
2022-06-11 22:10:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void GetGroupedRenderers(int groupId, int index, List<UIParticleRenderer> results)
|
|
|
|
{
|
|
|
|
results.Clear();
|
|
|
|
for (var i = 0; i < s_ActiveParticles.Count; i++)
|
|
|
|
{
|
|
|
|
var uip = s_ActiveParticles[i];
|
|
|
|
if (uip.useMeshSharing && uip.groupId == groupId)
|
|
|
|
{
|
|
|
|
results.Add(uip.GetRenderer(index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static UIParticle GetPrimary(int groupId)
|
|
|
|
{
|
|
|
|
UIParticle primary = null;
|
|
|
|
for (var i = 0; i < s_ActiveParticles.Count; i++)
|
|
|
|
{
|
|
|
|
var uip = s_ActiveParticles[i];
|
|
|
|
if (!uip.useMeshSharing || uip.groupId != groupId) continue;
|
|
|
|
if (uip.isPrimary) return uip;
|
|
|
|
if (!primary && uip.canSimulate) primary = uip;
|
|
|
|
}
|
|
|
|
return primary;
|
2020-08-20 03:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|