From 07332438a579cab227e1c1ef1f55c8b726571e27 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Mon, 11 May 2020 14:48:45 +0900 Subject: [PATCH] fix: Unintentional material destruction --- .../SoftMaskForUGUI/Scripts/SoftMaskable.cs | 91 +++++++------------ 1 file changed, 33 insertions(+), 58 deletions(-) diff --git a/Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs b/Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs index 8c30a99..9c5ad2a 100755 --- a/Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs +++ b/Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs @@ -90,10 +90,13 @@ public Graphic graphic Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial) { _softMask = null; - if (!isActiveAndEnabled) - { - return baseMaterial; - } + + // Unregister the previous material + MaterialCache.Unregister(_effectMaterialHash); + _effectMaterialHash = k_InvalidHash; + + // If this component is disabled, the material is returned as is. + if (!isActiveAndEnabled) return baseMaterial; // Find the nearest parent softmask. var parentTransform = transform.parent; @@ -109,48 +112,39 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial) parentTransform = parentTransform.parent; } - var oldHash = _effectMaterialHash; - var modifiedMaterial = baseMaterial; - if (_softMask) + // If the parents do not have a soft mask component, the material is returned as is. + if (!_softMask) return baseMaterial; + + // Generate soft maskable material. + _effectMaterialHash = new Hash128( + (uint) baseMaterial.GetInstanceID(), + (uint) _softMask.GetInstanceID(), + (uint) m_MaskInteraction, + (uint) (m_UseStencil ? 1 : 0) + ); + + // Generate soft maskable material. + var modifiedMaterial = MaterialCache.Register(baseMaterial, _effectMaterialHash, mat => { - _effectMaterialHash = GetMaterialHash(baseMaterial); - modifiedMaterial = MaterialCache.Register(baseMaterial, _effectMaterialHash, mat => - { - mat.shader = Shader.Find(string.Format("Hidden/{0} (SoftMaskable)", mat.shader.name)); + mat.shader = Shader.Find(string.Format("Hidden/{0} (SoftMaskable)", mat.shader.name)); #if UNITY_EDITOR - mat.EnableKeyword("SOFTMASK_EDITOR"); + mat.EnableKeyword("SOFTMASK_EDITOR"); #endif - mat.SetTexture(s_SoftMaskTexId, _softMask.softMaskBuffer); - mat.SetInt(s_StencilCompId, - m_UseStencil ? (int) CompareFunction.Equal : (int) CompareFunction.Always); - mat.SetVector(s_MaskInteractionId, new Vector4( - (m_MaskInteraction & 0x3), - ((m_MaskInteraction >> 2) & 0x3), - ((m_MaskInteraction >> 4) & 0x3), - ((m_MaskInteraction >> 6) & 0x3) - )); - }); - ReleaseMaterial(ref _maskMaterial); - _maskMaterial = modifiedMaterial; - } + mat.SetTexture(s_SoftMaskTexId, _softMask.softMaskBuffer); + mat.SetInt(s_StencilCompId, + m_UseStencil ? (int) CompareFunction.Equal : (int) CompareFunction.Always); + mat.SetVector(s_MaskInteractionId, new Vector4( + (m_MaskInteraction & 0x3), + ((m_MaskInteraction >> 2) & 0x3), + ((m_MaskInteraction >> 4) & 0x3), + ((m_MaskInteraction >> 6) & 0x3) + )); + }); + _maskMaterial = modifiedMaterial; - MaterialCache.Unregister(oldHash); return modifiedMaterial; } - private Hash128 GetMaterialHash(Material material) - { - if (!isActiveAndEnabled || !material || !material.shader) - return k_InvalidHash; - - return new Hash128( - (uint) material.GetInstanceID(), - (uint) m_MaskInteraction, - (uint) (m_UseStencil ? 1 : 0), - 0 - ); - } - /// /// Given a point and a camera is the raycast valid. /// @@ -225,30 +219,11 @@ private void OnDisable() graphic.SetMaterialDirtyEx(); _softMask = null; - ReleaseMaterial(ref _maskMaterial); MaterialCache.Unregister(_effectMaterialHash); _effectMaterialHash = k_InvalidHash; } - /// - /// Release the material. - /// - static void ReleaseMaterial(ref Material mat) - { - if (!mat) return; - -#if UNITY_EDITOR - if (!Application.isPlaying) - DestroyImmediate(mat); - else -#endif - Destroy(mat); - - mat = null; - } - - #if UNITY_EDITOR /// /// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).