Skip to content

Commit

Permalink
Fixed a major performance regression where if you had a lot of softma…
Browse files Browse the repository at this point in the history
…skable components (even if the parent canvas was in-active) it would eat over 20ms doing a certain editor-only function.
  • Loading branch information
strich authored and mob-sakai committed Aug 22, 2023
1 parent b870d64 commit 29998f4
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Scripts/SoftMaskable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ private void OnEnable()
#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
}

Expand All @@ -269,13 +271,16 @@ private void OnDisable()

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;
Expand All @@ -301,9 +306,12 @@ private void UpdateMaterialForSceneView(Material mat)
Profiler.EndSample();
}

private void LateUpdate()
private void SceneView_beforeSceneGui(UnityEditor.SceneView obj)
{
UpdateMaterialForSceneView(modifiedMaterial);

var parentCanvas = GetComponentInParent<Canvas>(); // 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);
}


Expand Down

0 comments on commit 29998f4

Please sign in to comment.