From 820d6d43642fe4765832021fc959c9c9bc8cba95 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Thu, 19 Jan 2023 23:50:13 +1000 Subject: [PATCH] Fixed a major performance regression where if you had a lot of softmaskable components (even if the parent canvas was in-active) it would eat over 20ms doing a certain editor-only function. --- Scripts/SoftMaskable.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Scripts/SoftMaskable.cs b/Scripts/SoftMaskable.cs index d63a6b0..1133344 100755 --- a/Scripts/SoftMaskable.cs +++ b/Scripts/SoftMaskable.cs @@ -248,6 +248,8 @@ namespace Coffee.UISoftMask #if UNITY_EDITOR s_GameVPId = Shader.PropertyToID("_GameVP"); s_GameTVPId = Shader.PropertyToID("_GameTVP"); + UnityEditor.SceneView.beforeSceneGui -= SceneView_beforeSceneGui; // For safety + UnityEditor.SceneView.beforeSceneGui += SceneView_beforeSceneGui; #endif } @@ -269,13 +271,16 @@ namespace Coffee.UISoftMask MaterialCache.Unregister(_effectMaterialHash); _effectMaterialHash = k_InvalidHash; +#if UNITY_EDITOR + UnityEditor.SceneView.beforeSceneGui -= SceneView_beforeSceneGui; +#endif } #if UNITY_EDITOR private void UpdateMaterialForSceneView(Material mat) { if(!mat || !graphic || !graphic.canvas || !mat.shader || !mat.shader.name.EndsWith(" (SoftMaskable)")) return; - + Debug.Log("UpdateMaterialForSceneView"); // Set view and projection matrices. Profiler.BeginSample("Set view and projection matrices"); var c = graphic.canvas.rootCanvas; @@ -301,9 +306,12 @@ namespace Coffee.UISoftMask Profiler.EndSample(); } - private void LateUpdate() + private void SceneView_beforeSceneGui(UnityEditor.SceneView obj) { - UpdateMaterialForSceneView(modifiedMaterial); + + var parentCanvas = GetComponentInParent(); // Don't think we can cache this in case this go is moved to another parent + if (parentCanvas != null && parentCanvas.enabled) // Only do this expensive call if the UI element is active + UpdateMaterialForSceneView(modifiedMaterial); }