From 4851a1880eef9f385dd9db644ea7e544f95da4fc Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Thu, 17 Aug 2023 17:48:48 +0900 Subject: [PATCH] fix: inactive ParticleSystems are removed from the list on refresh --- Scripts/Editor/UIParticleEditor.cs | 5 +++-- Scripts/UIParticle.cs | 4 ++-- Scripts/UIParticleAttractor.cs | 2 +- Scripts/Utils.cs | 35 ++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/Scripts/Editor/UIParticleEditor.cs b/Scripts/Editor/UIParticleEditor.cs index e10430b..8c00b6d 100644 --- a/Scripts/Editor/UIParticleEditor.cs +++ b/Scripts/Editor/UIParticleEditor.cs @@ -12,6 +12,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; +using Coffee.UIParticleExtensions; using UnityEditor; using UnityEditor.UI; using UnityEditorInternal; @@ -127,7 +128,7 @@ namespace Coffee.UIExtensions { var uiParticles = Selection.gameObjects.Select(x => x.GetComponent()) .Where(x => x) - .Select(x => x.GetComponentInParent()) + .Select(x => x.GetComponentInParent(true)) .Where(x => x && x.canvas) .Concat(Selection.gameObjects.Select(x => x.GetComponent()) .Where(x => x && x.canvas)) @@ -315,7 +316,7 @@ namespace Coffee.UIExtensions } // Does the shader support UI masks? - if (current.maskable && current.GetComponentInParent()) + if (current.maskable && current.GetComponentInParent(false)) { foreach (var mat in current.materials) { diff --git a/Scripts/UIParticle.cs b/Scripts/UIParticle.cs index b522214..796cbd0 100644 --- a/Scripts/UIParticle.cs +++ b/Scripts/UIParticle.cs @@ -394,8 +394,8 @@ namespace Coffee.UIExtensions private void RefreshParticles(GameObject root) { if (!root) return; - root.GetComponentsInChildren(particles); - particles.RemoveAll(x => x.GetComponentInParent() != this); + root.GetComponentsInChildren(true, particles); + particles.RemoveAll(x => x.GetComponentInParent(true) != this); for (var i = 0; i < particles.Count; i++) { diff --git a/Scripts/UIParticleAttractor.cs b/Scripts/UIParticleAttractor.cs index 59e85a9..60d6aab 100644 --- a/Scripts/UIParticleAttractor.cs +++ b/Scripts/UIParticleAttractor.cs @@ -248,7 +248,7 @@ namespace Coffee.UIExtensions return; } - _uiParticle = m_ParticleSystem.GetComponentInParent(); + _uiParticle = m_ParticleSystem.GetComponentInParent(true); if (_uiParticle && !_uiParticle.particles.Contains(m_ParticleSystem)) { _uiParticle = null; diff --git a/Scripts/Utils.cs b/Scripts/Utils.cs index dda1b19..7736a94 100644 --- a/Scripts/Utils.cs +++ b/Scripts/Utils.cs @@ -244,5 +244,40 @@ namespace Coffee.UIParticleExtensions Object.Destroy(obj); } } + +#if !UNITY_2020_3_OR_NEWER + public static T GetComponentInParent(this Component self, bool includeInactive) where T : Component + { + if (!self) return null; + if (!includeInactive) return self.GetComponentInParent(); + + var current = self.transform; + while (current) + { + var component = current.GetComponent(); + if (component) return component; + current = current.parent; + } + + return null; + } + + public static T GetComponentInChildren(this Component self, bool includeInactive) where T : Component + { + if (!self) return null; + if (!includeInactive) return self.GetComponentInChildren(); + + var component = self.GetComponent(); + if (component) return component; + + foreach (Transform child in self.transform) + { + component = child.GetComponentInChildren(true); + if (component) return component; + } + + return null; + } +#endif } }