From 3377af2d2a62dfe790e92d76b91f04b43c8ae763 Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Tue, 18 Feb 2025 07:57:09 +0900 Subject: [PATCH] fix: fix potential issues --- .../Extensions/ComponentExtensions.cs | 2 +- .../Internal/Extensions/GraphicExtensions.cs | 15 ++++++++++++++- .../src/Runtime/Internal/Utilities/Misc.cs | 11 +++++++++++ Packages/src/Runtime/SoftMask.cs | 15 ++------------- Packages/src/Runtime/SoftMaskable.cs | 19 +++++++++++-------- .../src/Runtime/Utilities/SoftMaskUtils.cs | 1 + 6 files changed, 40 insertions(+), 23 deletions(-) diff --git a/Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs b/Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs index 0ec7a6b..ef58a55 100644 --- a/Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs +++ b/Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs @@ -204,7 +204,7 @@ internal static void ConvertTo(this Object context) where T : MonoBehaviour target.enabled = false; // Find MonoScript of the specified component. - foreach (var script in Resources.FindObjectsOfTypeAll()) + foreach (var script in MonoImporter.GetAllRuntimeMonoScripts()) { if (script.GetClass() != typeof(T)) { diff --git a/Packages/src/Runtime/Internal/Extensions/GraphicExtensions.cs b/Packages/src/Runtime/Internal/Extensions/GraphicExtensions.cs index 416ee64..7a11455 100644 --- a/Packages/src/Runtime/Internal/Extensions/GraphicExtensions.cs +++ b/Packages/src/Runtime/Internal/Extensions/GraphicExtensions.cs @@ -15,7 +15,20 @@ internal static class GraphicExtensions private static readonly Bounds s_ScreenBounds = new Bounds(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(1, 1, 1)); /// - /// Check if a Graphic component is currently in the screen view. + /// Get material for rendering. + /// + public static Material GetMaterialForRendering(this Graphic self) + { + if (!self || !self.isActiveAndEnabled) return null; + + var cr = self.canvasRenderer; + if (!cr || cr.materialCount == 0) return null; + + return cr.GetMaterial(); + } + + /// + /// Get materials for rendering. /// public static void GetMaterialsForRendering(this Graphic self, List result) { diff --git a/Packages/src/Runtime/Internal/Utilities/Misc.cs b/Packages/src/Runtime/Internal/Utilities/Misc.cs index cdb1927..f66b3db 100644 --- a/Packages/src/Runtime/Internal/Utilities/Misc.cs +++ b/Packages/src/Runtime/Internal/Utilities/Misc.cs @@ -77,6 +77,17 @@ public static T[] GetAllComponentsInPrefabStage() where T : Component public static bool isBatchOrBuilding => Application.isBatchMode || BuildPipeline.isBuildingPlayer; #endif + + [Conditional("UNITY_EDITOR")] + public static void QueuePlayerLoopUpdate() + { +#if UNITY_EDITOR + if (!EditorApplication.isPlaying) + { + EditorApplication.QueuePlayerLoopUpdate(); + } +#endif + } } #if !UNITY_2021_2_OR_NEWER diff --git a/Packages/src/Runtime/SoftMask.cs b/Packages/src/Runtime/SoftMask.cs index 2a44312..b65ffcb 100644 --- a/Packages/src/Runtime/SoftMask.cs +++ b/Packages/src/Runtime/SoftMask.cs @@ -652,13 +652,8 @@ private void OnCanvasViewChanged() { _hasResolutionChanged = true; SetDirtyAndNotify(); - + Misc.QueuePlayerLoopUpdate(); #if UNITY_EDITOR - if (!Application.isPlaying) - { - EditorApplication.QueuePlayerLoopUpdate(); - } - EditorApplication.delayCall += () => { if (!this || !isActiveAndEnabled) return; @@ -833,13 +828,7 @@ private void RenderSoftMaskBuffer() Profiler.EndSample(); } -#if UNITY_EDITOR - if (!Application.isPlaying) - { - EditorApplication.QueuePlayerLoopUpdate(); - } -#endif - + Misc.QueuePlayerLoopUpdate(); onRenderSoftMaskBuffer?.Invoke(this); } diff --git a/Packages/src/Runtime/SoftMaskable.cs b/Packages/src/Runtime/SoftMaskable.cs index d0ecdbc..bb7ca82 100755 --- a/Packages/src/Runtime/SoftMaskable.cs +++ b/Packages/src/Runtime/SoftMaskable.cs @@ -1,5 +1,6 @@ using System; using Coffee.UISoftMaskInternal; +using UnityEditor; using UnityEngine; using UnityEngine.Profiling; using UnityEngine.UI; @@ -249,7 +250,7 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial) threshold = s.softnessRange.average; subtract = true; } - else if (_softMask) + else { threshold = _softMask.softnessRange.average; } @@ -257,7 +258,6 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial) localId = (uint)(Mathf.Clamp01(threshold) * (1 << 8) + (subtract ? 1 << 9 : 0) + (localId << 10)); #endif - var hash = new Hash128( (uint)baseMaterial.GetInstanceID(), (uint)_softMask.softMaskBuffer.GetInstanceID(), @@ -307,6 +307,7 @@ public void SetMaterialDirty() { if (!isActiveAndEnabled || !_graphic) return; _graphic.SetMaterialDirty(); + Misc.QueuePlayerLoopUpdate(); } public void SetMaterialDirtyForChildren() @@ -345,7 +346,9 @@ private void OnValidate() private void UpdateSceneViewMatrix() { if (!_graphic || !_graphic.canvas || !_maskableMaterial) return; - if (FrameCache.TryGet(_maskableMaterial, nameof(UpdateSceneViewMatrix), out bool _)) + + var mat = _graphic.GetMaterialForRendering(); + if (!mat || FrameCache.TryGet(mat, nameof(UpdateSceneViewMatrix), out bool _)) { return; } @@ -386,8 +389,8 @@ private void UpdateSceneViewMatrix() // Set view and projection matrices. Profiler.BeginSample("(SM4UI)[SoftMaskable] (Editor) UpdateSceneViewMatrix > Set matrices"); - _maskableMaterial.SetMatrix(s_GameVp, gameVp); - _maskableMaterial.SetMatrix(s_GameTvp, gameTvp); + mat.SetMatrix(s_GameVp, gameVp); + mat.SetMatrix(s_GameTvp, gameTvp); Profiler.EndSample(); // Calc Right eye matrices. @@ -404,11 +407,11 @@ private void UpdateSceneViewMatrix() Profiler.EndSample(); } - _maskableMaterial.SetMatrix(s_GameVp2, gameVp); - _maskableMaterial.SetMatrix(s_GameTvp2, gameVp); + mat.SetMatrix(s_GameVp2, gameVp); + mat.SetMatrix(s_GameTvp2, gameVp); } - FrameCache.Set(_maskableMaterial, nameof(UpdateSceneViewMatrix), true); + FrameCache.Set(mat, nameof(UpdateSceneViewMatrix), true); } #endif } diff --git a/Packages/src/Runtime/Utilities/SoftMaskUtils.cs b/Packages/src/Runtime/Utilities/SoftMaskUtils.cs index 174e45d..5c15fd9 100644 --- a/Packages/src/Runtime/Utilities/SoftMaskUtils.cs +++ b/Packages/src/Runtime/Utilities/SoftMaskUtils.cs @@ -218,6 +218,7 @@ public static Material CreateSoftMaskable( Profiler.EndSample(); Profiler.BeginSample("(SM4UI)[SoftMaskableMaterial] Create > Set Properties"); + mat.CopyPropertiesFromMaterial(baseMat); mat.SetTexture(s_SoftMaskTex, softMaskBuffer); mat.SetInt(s_SoftMaskableStereo, isStereo ? 1 : 0); mat.SetVector(s_SoftMaskColor, new Vector4(