2020-08-20 03:42:16 +08:00
|
|
|
using System.Collections.Generic;
|
2023-08-17 08:43:02 +08:00
|
|
|
using UnityEditor;
|
2020-08-20 03:42:16 +08:00
|
|
|
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
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
private static readonly List<UIParticle> s_ActiveParticles = new List<UIParticle>();
|
|
|
|
private static readonly List<UIParticleAttractor> s_ActiveAttractors = new List<UIParticleAttractor>();
|
|
|
|
private static readonly HashSet<int> s_UpdatedGroupIds = new HashSet<int>();
|
|
|
|
private static int s_FrameCount;
|
2020-08-28 13:38:13 +08:00
|
|
|
|
2022-06-11 21:32:14 +08:00
|
|
|
public static int uiParticleCount
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
get { return s_ActiveParticles.Count; }
|
2022-06-11 21:32:14 +08:00
|
|
|
}
|
|
|
|
|
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
|
2023-08-17 08:43:02 +08:00
|
|
|
[InitializeOnLoadMethod]
|
2020-08-20 03:42:16 +08:00
|
|
|
#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.
|
2023-08-17 08:43:02 +08:00
|
|
|
if (s_FrameCount == Time.frameCount) return;
|
|
|
|
s_FrameCount = Time.frameCount;
|
2021-06-03 02:03:16 +08:00
|
|
|
|
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-28 13:26:09 +08:00
|
|
|
if (!uip || !uip.canvas || !uip.isPrimary || s_UpdatedGroupIds.Contains(uip.groupId)) continue;
|
2022-06-11 22:10:17 +08:00
|
|
|
|
|
|
|
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];
|
2022-06-28 13:26:09 +08:00
|
|
|
if (!uip || !uip.canvas) continue;
|
|
|
|
|
2022-06-11 22:10:17 +08:00
|
|
|
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
|
2022-06-17 12:35:19 +08:00
|
|
|
for (var i = 0; i < s_ActiveAttractors.Count; i++)
|
2022-06-15 09:24:31 +08:00
|
|
|
{
|
|
|
|
s_ActiveAttractors[i].Attract();
|
|
|
|
}
|
2022-06-16 21:43:23 +08:00
|
|
|
|
|
|
|
// UpdateParticleCount.
|
|
|
|
for (var i = 0; i < s_ActiveParticles.Count; i++)
|
|
|
|
{
|
2022-06-28 13:26:09 +08:00
|
|
|
var uip = s_ActiveParticles[i];
|
|
|
|
if (!uip || !uip.canvas) continue;
|
|
|
|
|
|
|
|
uip.UpdateParticleCount();
|
2022-06-16 21:43:23 +08:00
|
|
|
}
|
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;
|
|
|
|
}
|
2023-08-17 08:43:02 +08:00
|
|
|
|
2022-06-11 22:10:17 +08:00
|
|
|
return primary;
|
2020-08-20 03:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|