From 52eb28cdafe7c9009d5a4b76978e002798440e8d Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Mon, 30 Sep 2024 02:45:50 +0900 Subject: [PATCH] feat: UIParticle no longer inherits from MaskableGraphic BREAKING CHANGE: Some members inherited from MaskableGraphic will no longer be available. --- Packages/src/Editor/UIParticleEditor.cs | 15 +++-- Packages/src/Runtime/UIParticle.cs | 79 +++++++++++++++++-------- 2 files changed, 62 insertions(+), 32 deletions(-) diff --git a/Packages/src/Editor/UIParticleEditor.cs b/Packages/src/Editor/UIParticleEditor.cs index 16085db..5bbbdba 100644 --- a/Packages/src/Editor/UIParticleEditor.cs +++ b/Packages/src/Editor/UIParticleEditor.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using UnityEditor; -using UnityEditor.UI; using UnityEditorInternal; using UnityEngine; using UnityEngine.Profiling; @@ -26,7 +25,7 @@ namespace Coffee.UIExtensions { [CustomEditor(typeof(UIParticle))] [CanEditMultipleObjects] - internal class UIParticleEditor : GraphicEditor + internal class UIParticleEditor : Editor { //################################ // Constant or Static Members. @@ -81,10 +80,8 @@ namespace Coffee.UIExtensions /// /// This function is called when the object becomes enabled and active. /// - protected override void OnEnable() + private void OnEnable() { - base.OnEnable(); - _maskable = serializedObject.FindProperty("m_Maskable"); _scale3D = serializedObject.FindProperty("m_Scale3D"); _animatableProperties = serializedObject.FindProperty("m_AnimatableProperties"); @@ -437,9 +434,11 @@ namespace Coffee.UIExtensions { if (!p || (ignoreCurrent && target == p)) return; - var cr = p.canvasRenderer; - DestroyImmediate(p); - DestroyImmediate(cr); + Misc.DestroyImmediate(p); + if (p.TryGetComponent(out var cr)) + { + Misc.DestroyImmediate(cr); + } #if UNITY_2018_3_OR_NEWER var stage = PrefabStageUtility.GetCurrentPrefabStage(); diff --git a/Packages/src/Runtime/UIParticle.cs b/Packages/src/Runtime/UIParticle.cs index 40133d6..b56710d 100644 --- a/Packages/src/Runtime/UIParticle.cs +++ b/Packages/src/Runtime/UIParticle.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using Coffee.UIParticleInternal; using UnityEngine; +using UnityEngine.EventSystems; using UnityEngine.Rendering; using UnityEngine.Serialization; -using UnityEngine.UI; using Random = UnityEngine.Random; [assembly: InternalsVisibleTo("Coffee.UIParticle.Editor")] @@ -18,7 +18,7 @@ namespace Coffee.UIExtensions [ExecuteAlways] [RequireComponent(typeof(RectTransform))] [RequireComponent(typeof(CanvasRenderer))] - public class UIParticle : MaskableGraphic, ISerializationCallbackReceiver + public class UIParticle : UIBehaviour, ISerializationCallbackReceiver { public enum AutoScalingMode { @@ -117,20 +117,48 @@ namespace Coffee.UIExtensions "Change the bake view size.")] private float m_CustomViewSize = 10; + [SerializeField] + private bool m_Maskable = true; + private readonly List _renderers = new List(); private Camera _bakeCamera; + private Canvas _canvas; private int _groupId; private bool _isScaleStored; private Vector3 _storedScale; private DrivenRectTransformTracker _tracker; - /// - /// Should this graphic be considered a target for ray-casting? - /// - public override bool raycastTarget + public RectTransform rectTransform => transform as RectTransform; + + public Canvas canvas { - get => false; - set { } + get + { + if (_canvas) return _canvas; + + var tr = transform; + while (tr && !_canvas) + { + if (tr.TryGetComponent(out _canvas)) return _canvas; + tr = tr.parent; + } + + return null; + } + } + + /// + /// Does this graphic allow masking. + /// + public bool maskable + { + get => m_Maskable; + set + { + if (value == m_Maskable) return; + m_Maskable = value; + UpdateRendererMaterial(); + } } /// @@ -307,15 +335,15 @@ namespace Coffee.UIExtensions public Vector3 parentScale { get; private set; } - public Vector3 canvasScale { get; private set; } + private Vector3 canvasScale { get; set; } protected override void OnEnable() { _isScaleStored = false; ResetGroupId(); UIParticleUpdater.Register(this); - RegisterDirtyMaterialCallback(UpdateRendererMaterial); + // if (0 < particles.Count) { RefreshParticles(particles); @@ -325,7 +353,7 @@ namespace Coffee.UIExtensions RefreshParticles(); } - base.OnEnable(); + UpdateRendererMaterial(); } /// @@ -342,9 +370,15 @@ namespace Coffee.UIExtensions _isScaleStored = false; UIParticleUpdater.Unregister(this); _renderers.ForEach(r => r.Reset()); - UnregisterDirtyMaterialCallback(UpdateRendererMaterial); + _canvas = null; + } - base.OnDisable(); + /// + /// Called when the state of the parent Canvas is changed. + /// + protected override void OnCanvasHierarchyChanged() + { + _canvas = null; } /// @@ -354,6 +388,14 @@ namespace Coffee.UIExtensions { } + /// + /// This function is called when a direct or indirect parent of the transform of the GameObject has changed. + /// + protected override void OnTransformParentChanged() + { + _canvas = null; + } + void ISerializationCallbackReceiver.OnBeforeSerialize() { } @@ -646,17 +688,6 @@ namespace Coffee.UIExtensions : Random.Range(m_GroupId, m_GroupMaxId + 1); } - protected override void UpdateMaterial() - { - } - - /// - /// Call to update the geometry of the Graphic onto the CanvasRenderer. - /// - protected override void UpdateGeometry() - { - } - private void UpdateRendererMaterial() { for (var i = 0; i < _renderers.Count; i++)