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.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 @@ internal class UIParticleEditor : GraphicEditor /// /// 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 @@ private void DestroyUIParticle(UIParticle p, bool ignoreCurrent = false) { 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.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 @@ public enum PositionMode "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; + public RectTransform rectTransform => transform as RectTransform; + + public Canvas canvas + { + get + { + if (_canvas) return _canvas; + + var tr = transform; + while (tr && !_canvas) + { + if (tr.TryGetComponent(out _canvas)) return _canvas; + tr = tr.parent; + } + + return null; + } + } + /// - /// Should this graphic be considered a target for ray-casting? + /// Does this graphic allow masking. /// - public override bool raycastTarget + public bool maskable { - get => false; - set { } + get => m_Maskable; + set + { + if (value == m_Maskable) return; + m_Maskable = value; + UpdateRendererMaterial(); + } } /// @@ -307,15 +335,15 @@ public Vector3 scale3D 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 @@ protected override void OnEnable() RefreshParticles(); } - base.OnEnable(); + UpdateRendererMaterial(); } /// @@ -342,9 +370,15 @@ protected override void OnDisable() _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 @@ protected override void OnDidApplyAnimationProperties() { } + /// + /// 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 @@ internal void ResetGroupId() : 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++)