diff --git a/Assets/Demo/Performance Demo/Scripts/UIParticle_PerformanceDemo.cs b/Assets/Demo/Performance Demo/Scripts/UIParticle_PerformanceDemo.cs index ab0003b..ce4027a 100644 --- a/Assets/Demo/Performance Demo/Scripts/UIParticle_PerformanceDemo.cs +++ b/Assets/Demo/Performance Demo/Scripts/UIParticle_PerformanceDemo.cs @@ -1,3 +1,4 @@ +using Coffee.UIParticleInternal; using UnityEngine; using UnityEngine.Serialization; @@ -46,11 +47,7 @@ public void UIParticle_Enable(bool flag) if (!flag) { -#if UNITY_2023_1_OR_NEWER - foreach (var ps in FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None)) -#else - foreach (var ps in FindObjectsOfType()) -#endif + foreach (var ps in Misc.FindObjectsOfType()) { ps.Play(false); } @@ -79,11 +76,7 @@ public void UIParticle_RandomGroup(bool flag) public void ParticleSystem_SetScale(float scale) { -#if UNITY_2023_1_OR_NEWER - foreach (var ps in FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None)) -#else - foreach (var ps in FindObjectsOfType()) -#endif + foreach (var ps in Misc.FindObjectsOfType()) { ps.transform.localScale = new Vector3(scale, scale, scale); } diff --git a/Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs b/Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs index b45f016..d65be22 100644 --- a/Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs +++ b/Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs @@ -134,6 +134,35 @@ public static void AddComponentOnChildren(this Component self, HideFlags hide Profiler.EndSample(); } + /// + /// Add a component of a specific type to the children of a GameObject. + /// + public static void AddComponentOnChildren(this Component self, bool includeSelf) + where T : Component + { + if (self == null) return; + + Profiler.BeginSample("(COF)[ComponentExt] AddComponentOnChildren > Self"); + if (includeSelf && !self.TryGetComponent(out _)) + { + self.gameObject.AddComponent(); + } + + Profiler.EndSample(); + + Profiler.BeginSample("(COF)[ComponentExt] AddComponentOnChildren > Child"); + var childCount = self.transform.childCount; + for (var i = 0; i < childCount; i++) + { + var child = self.transform.GetChild(i); + if (child.TryGetComponent(out _)) continue; + + child.gameObject.AddComponent(); + } + + Profiler.EndSample(); + } + #if !UNITY_2021_2_OR_NEWER && !UNITY_2020_3_45 && !UNITY_2020_3_46 && !UNITY_2020_3_47 && !UNITY_2020_3_48 public static T GetComponentInParent(this Component self, bool includeInactive) where T : Component { diff --git a/Packages/src/Runtime/Internal/Extensions/Misc.cs b/Packages/src/Runtime/Internal/Utilities/Misc.cs similarity index 78% rename from Packages/src/Runtime/Internal/Extensions/Misc.cs rename to Packages/src/Runtime/Internal/Utilities/Misc.cs index 66d0b4e..fb1a210 100644 --- a/Packages/src/Runtime/Internal/Extensions/Misc.cs +++ b/Packages/src/Runtime/Internal/Utilities/Misc.cs @@ -6,6 +6,15 @@ namespace Coffee.UIParticleInternal { internal static class Misc { + public static T[] FindObjectsOfType() where T : Object + { +#if UNITY_2023_1_OR_NEWER + return Object.FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None); +#else + return Object.FindObjectsOfType(); +#endif + } + public static void Destroy(Object obj) { if (!obj) return; diff --git a/Packages/src/Runtime/Internal/Extensions/Misc.cs.meta b/Packages/src/Runtime/Internal/Utilities/Misc.cs.meta similarity index 83% rename from Packages/src/Runtime/Internal/Extensions/Misc.cs.meta rename to Packages/src/Runtime/Internal/Utilities/Misc.cs.meta index 5668392..e9f550c 100644 --- a/Packages/src/Runtime/Internal/Extensions/Misc.cs.meta +++ b/Packages/src/Runtime/Internal/Utilities/Misc.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 39ed6a6b0a72e482488bd298b2ae762e +guid: 182319ecc315e4858b119764af0fbcb0 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Packages/src/Runtime/Internal/Utilities/UIExtraCallbacks.cs b/Packages/src/Runtime/Internal/Utilities/UIExtraCallbacks.cs index be9e7ee..6ad33ed 100755 --- a/Packages/src/Runtime/Internal/Utilities/UIExtraCallbacks.cs +++ b/Packages/src/Runtime/Internal/Utilities/UIExtraCallbacks.cs @@ -14,6 +14,8 @@ internal static class UIExtraCallbacks private static readonly FastAction s_AfterCanvasRebuildAction = new FastAction(); private static readonly FastAction s_LateAfterCanvasRebuildAction = new FastAction(); private static readonly FastAction s_BeforeCanvasRebuildAction = new FastAction(); + private static readonly FastAction s_OnScreenSizeChangedAction = new FastAction(); + private static Vector2Int s_LastScreenSize; static UIExtraCallbacks() { @@ -48,6 +50,15 @@ public static event Action onAfterCanvasRebuild remove => s_AfterCanvasRebuildAction.Remove(value); } + /// + /// Event that occurs when the screen size changes. + /// + public static event Action onScreenSizeChanged + { + add => s_OnScreenSizeChangedAction.Add(value); + remove => s_OnScreenSizeChangedAction.Remove(value); + } + /// /// Initializes the UIExtraCallbacks to ensure proper event handling. /// @@ -77,6 +88,17 @@ private static void InitializeOnLoad() /// private static void OnBeforeCanvasRebuild() { + var screenSize = new Vector2Int(Screen.width, Screen.height); + if (s_LastScreenSize != screenSize) + { + if (s_LastScreenSize != default) + { + s_OnScreenSizeChangedAction.Invoke(); + } + + s_LastScreenSize = screenSize; + } + s_BeforeCanvasRebuildAction.Invoke(); InitializeAfterCanvasRebuild(); } diff --git a/Packages/src/Runtime/UIParticle.cs b/Packages/src/Runtime/UIParticle.cs index 40133d6..5ad390d 100644 --- a/Packages/src/Runtime/UIParticle.cs +++ b/Packages/src/Runtime/UIParticle.cs @@ -9,6 +9,8 @@ using Random = UnityEngine.Random; [assembly: InternalsVisibleTo("Coffee.UIParticle.Editor")] +[assembly: InternalsVisibleTo("Coffee.UIParticle.PerformanceDemo")] +[assembly: InternalsVisibleTo("Coffee.UIParticle.Demo")] namespace Coffee.UIExtensions { diff --git a/Packages/src/Samples~/Demo/Scripts/UIParticle_Demo.cs b/Packages/src/Samples~/Demo/Scripts/UIParticle_Demo.cs index 266eda3..68e8993 100644 --- a/Packages/src/Samples~/Demo/Scripts/UIParticle_Demo.cs +++ b/Packages/src/Samples~/Demo/Scripts/UIParticle_Demo.cs @@ -1,3 +1,4 @@ +using Coffee.UIParticleInternal; using UnityEngine; using UnityEngine.Serialization; using UnityEngine.UI; @@ -51,11 +52,7 @@ public void FullScreen() public void EnableAnimations(bool flag) { -#if UNITY_2023_1_OR_NEWER - foreach (var animator in FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None)) -#else - foreach (var animator in FindObjectsOfType()) -#endif + foreach (var animator in Misc.FindObjectsOfType()) { animator.enabled = flag; } @@ -83,11 +80,7 @@ public void UIParticle_RandomGroup(bool flag) public void UIParticle_Scale(float scale) { -#if UNITY_2023_1_OR_NEWER - foreach (var uip in FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None)) -#else - foreach (var uip in FindObjectsOfType()) -#endif + foreach (var uip in Misc.FindObjectsOfType()) { uip.scale = scale; } @@ -95,11 +88,7 @@ public void UIParticle_Scale(float scale) public void ParticleSystem_WorldSpaseSimulation(bool flag) { -#if UNITY_2023_1_OR_NEWER - foreach (var p in FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None)) -#else - foreach (var p in FindObjectsOfType()) -#endif + foreach (var p in Misc.FindObjectsOfType()) { var main = p.main; main.simulationSpace = flag @@ -135,11 +124,7 @@ public void ParticleSystem_Emit(ParticleSystem ps) public void ParticleSystem_SetScale(float scale) { -#if UNITY_2023_1_OR_NEWER - foreach (var ps in FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None)) -#else - foreach (var ps in FindObjectsOfType()) -#endif + foreach (var ps in Misc.FindObjectsOfType()) { ps.transform.localScale = new Vector3(scale, scale, scale); }