From f08809772a7775cc50a1bdf3344d001a98b396cc Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Tue, 18 Jun 2024 17:13:35 +0900 Subject: [PATCH] feat: restore `Transform.localScale` when setting `autoScalingMode` to something other than `Transform` # Conflicts: # Packages/src/Editor/UIParticleEditor.cs # Packages/src/Runtime/UIParticle.cs --- Editor/UIParticleEditor.cs | 18 ------------ Runtime/UIParticle.cs | 60 ++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 46 deletions(-) diff --git a/Editor/UIParticleEditor.cs b/Editor/UIParticleEditor.cs index 2430958..648fcb3 100644 --- a/Editor/UIParticleEditor.cs +++ b/Editor/UIParticleEditor.cs @@ -401,25 +401,7 @@ namespace Coffee.UIExtensions private static void DrawAutoScaling(SerializedProperty prop, IEnumerable uiParticles) { - var isTransformMode = prop.intValue == (int)UIParticle.AutoScalingMode.Transform; - EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(prop); - if (!EditorGUI.EndChangeCheck() || !isTransformMode) return; - - // on changed true->false, reset scale. - ResetScale(uiParticles); - } - - private static void ResetScale(IEnumerable uiParticles) - { - EditorApplication.delayCall += () => - { - foreach (var uip in uiParticles) - { - if (!uip) continue; - uip.transform.localScale = Vector3.one; - } - }; } private void DestroyUIParticle(UIParticle p, bool ignoreCurrent = false) diff --git a/Runtime/UIParticle.cs b/Runtime/UIParticle.cs index 5a16df4..f690ba2 100644 --- a/Runtime/UIParticle.cs +++ b/Runtime/UIParticle.cs @@ -111,6 +111,8 @@ namespace Coffee.UIExtensions private int _groupId; private Camera _orthographicCamera; private DrivenRectTransformTracker _tracker; + private Vector3 _storedScale; + private bool _isScaleStored; public RectTransform rectTransform => transform as RectTransform; @@ -232,7 +234,12 @@ namespace Coffee.UIExtensions { if (m_AutoScalingMode == value) return; m_AutoScalingMode = value; - UpdateTracker(); + + if (autoScalingMode != AutoScalingMode.Transform && _isScaleStored) + { + transform.localScale = _storedScale; + _isScaleStored = false; + } } } @@ -308,8 +315,8 @@ namespace Coffee.UIExtensions protected override void OnEnable() { + _isScaleStored = false; ResetGroupId(); - UpdateTracker(); UIParticleUpdater.Register(this); // @@ -330,7 +337,13 @@ namespace Coffee.UIExtensions /// protected override void OnDisable() { - UpdateTracker(); + _tracker.Clear(); + if (autoScalingMode == AutoScalingMode.Transform && _isScaleStored) + { + transform.localScale = _storedScale; + } + + _isScaleStored = false; UIParticleUpdater.Unregister(this); _renderers.ForEach(r => r.Reset()); _canvas = null; @@ -359,15 +372,6 @@ namespace Coffee.UIExtensions _canvas = null; } -#if UNITY_EDITOR - protected override void OnValidate() - { - base.OnValidate(); - UpdateTracker(); - UpdateRendererMaterial(); - } -#endif - void ISerializationCallbackReceiver.OnBeforeSerialize() { } @@ -583,12 +587,26 @@ namespace Coffee.UIExtensions internal void UpdateTransformScale() { + _tracker.Clear(); canvasScale = canvas.rootCanvas.transform.localScale.Inverse(); parentScale = transform.parent.lossyScale; - if (autoScalingMode != AutoScalingMode.Transform) return; + if (autoScalingMode != AutoScalingMode.Transform) + { + if (_isScaleStored) + { + transform.localScale = _storedScale; + } + _isScaleStored = false; + return; + } + + var currentScale = transform.localScale; + _storedScale = currentScale; + _isScaleStored = true; + _tracker.Add(this, rectTransform, DrivenTransformProperties.Scale); var newScale = parentScale.Inverse(); - if (transform.localScale != newScale) + if (currentScale != newScale) { transform.localScale = newScale; } @@ -704,19 +722,5 @@ namespace Coffee.UIExtensions return _orthographicCamera; } - - private void UpdateTracker() - { -#pragma warning disable CS0618 // Type or member is obsolete - if (!enabled || autoScalingMode != AutoScalingMode.Transform) -#pragma warning restore CS0618 // Type or member is obsolete - { - _tracker.Clear(); - } - else - { - _tracker.Add(this, rectTransform, DrivenTransformProperties.Scale); - } - } } }