Skip to content

Commit

Permalink
refactor: update coffee.internal
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Nov 20, 2024
1 parent 3544c59 commit 253fb52
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 31 deletions.
13 changes: 3 additions & 10 deletions Assets/Demo/Performance Demo/Scripts/UIParticle_PerformanceDemo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Coffee.UIParticleInternal;
using UnityEngine;
using UnityEngine.Serialization;

Expand Down Expand Up @@ -46,11 +47,7 @@ public void UIParticle_Enable(bool flag)

if (!flag)
{
#if UNITY_2023_1_OR_NEWER
foreach (var ps in FindObjectsByType<ParticleSystem>(FindObjectsInactive.Include, FindObjectsSortMode.None))
#else
foreach (var ps in FindObjectsOfType<ParticleSystem>())
#endif
foreach (var ps in Misc.FindObjectsOfType<ParticleSystem>())
{
ps.Play(false);
}
Expand Down Expand Up @@ -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<ParticleSystem>(FindObjectsInactive.Include, FindObjectsSortMode.None))
#else
foreach (var ps in FindObjectsOfType<ParticleSystem>())
#endif
foreach (var ps in Misc.FindObjectsOfType<ParticleSystem>())
{
ps.transform.localScale = new Vector3(scale, scale, scale);
}
Expand Down
29 changes: 29 additions & 0 deletions Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,35 @@ public static void AddComponentOnChildren<T>(this Component self, HideFlags hide
Profiler.EndSample();
}

/// <summary>
/// Add a component of a specific type to the children of a GameObject.
/// </summary>
public static void AddComponentOnChildren<T>(this Component self, bool includeSelf)
where T : Component
{
if (self == null) return;

Profiler.BeginSample("(COF)[ComponentExt] AddComponentOnChildren > Self");
if (includeSelf && !self.TryGetComponent<T>(out _))
{
self.gameObject.AddComponent<T>();
}

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<T>(out _)) continue;

child.gameObject.AddComponent<T>();
}

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<T>(this Component self, bool includeInactive) where T : Component
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ namespace Coffee.UIParticleInternal
{
internal static class Misc
{
public static T[] FindObjectsOfType<T>() where T : Object
{
#if UNITY_2023_1_OR_NEWER
return Object.FindObjectsByType<T>(FindObjectsInactive.Include, FindObjectsSortMode.None);
#else
return Object.FindObjectsOfType<T>();
#endif
}

public static void Destroy(Object obj)
{
if (!obj) return;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Packages/src/Runtime/Internal/Utilities/UIExtraCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -48,6 +50,15 @@ public static event Action onAfterCanvasRebuild
remove => s_AfterCanvasRebuildAction.Remove(value);
}

/// <summary>
/// Event that occurs when the screen size changes.
/// </summary>
public static event Action onScreenSizeChanged
{
add => s_OnScreenSizeChangedAction.Add(value);
remove => s_OnScreenSizeChangedAction.Remove(value);
}

/// <summary>
/// Initializes the UIExtraCallbacks to ensure proper event handling.
/// </summary>
Expand Down Expand Up @@ -77,6 +88,17 @@ private static void InitializeOnLoad()
/// </summary>
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();
}
Expand Down
2 changes: 2 additions & 0 deletions Packages/src/Runtime/UIParticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
25 changes: 5 additions & 20 deletions Packages/src/Samples~/Demo/Scripts/UIParticle_Demo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Coffee.UIParticleInternal;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
Expand Down Expand Up @@ -51,11 +52,7 @@ public void FullScreen()

public void EnableAnimations(bool flag)
{
#if UNITY_2023_1_OR_NEWER
foreach (var animator in FindObjectsByType<Animator>(FindObjectsInactive.Include, FindObjectsSortMode.None))
#else
foreach (var animator in FindObjectsOfType<Animator>())
#endif
foreach (var animator in Misc.FindObjectsOfType<Animator>())
{
animator.enabled = flag;
}
Expand Down Expand Up @@ -83,23 +80,15 @@ public void UIParticle_RandomGroup(bool flag)

public void UIParticle_Scale(float scale)
{
#if UNITY_2023_1_OR_NEWER
foreach (var uip in FindObjectsByType<UIParticle>(FindObjectsInactive.Include, FindObjectsSortMode.None))
#else
foreach (var uip in FindObjectsOfType<UIParticle>())
#endif
foreach (var uip in Misc.FindObjectsOfType<UIParticle>())
{
uip.scale = scale;
}
}

public void ParticleSystem_WorldSpaseSimulation(bool flag)
{
#if UNITY_2023_1_OR_NEWER
foreach (var p in FindObjectsByType<ParticleSystem>(FindObjectsInactive.Include, FindObjectsSortMode.None))
#else
foreach (var p in FindObjectsOfType<ParticleSystem>())
#endif
foreach (var p in Misc.FindObjectsOfType<ParticleSystem>())
{
var main = p.main;
main.simulationSpace = flag
Expand Down Expand Up @@ -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<ParticleSystem>(FindObjectsInactive.Include, FindObjectsSortMode.None))
#else
foreach (var ps in FindObjectsOfType<ParticleSystem>())
#endif
foreach (var ps in Misc.FindObjectsOfType<ParticleSystem>())
{
ps.transform.localScale = new Vector3(scale, scale, scale);
}
Expand Down

0 comments on commit 253fb52

Please sign in to comment.