From 309ea13afff958d5d680fdd7d4d6d27a14d53cb8 Mon Sep 17 00:00:00 2001 From: wsycarlos Date: Thu, 15 Jan 2026 18:38:05 +0800 Subject: [PATCH] Fix Outline Instance Missing Issue --- .../Runtime/TextMeshProBywayUI.cs | 21 +++++++++ .../TextMeshProBywayUIRuntimeHelper.cs | 45 +++++++++++++------ .../package.json | 2 +- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/Packages/com.bywaystudios.textmeshpro.ext/Runtime/TextMeshProBywayUI.cs b/Packages/com.bywaystudios.textmeshpro.ext/Runtime/TextMeshProBywayUI.cs index baac862..667d1a9 100644 --- a/Packages/com.bywaystudios.textmeshpro.ext/Runtime/TextMeshProBywayUI.cs +++ b/Packages/com.bywaystudios.textmeshpro.ext/Runtime/TextMeshProBywayUI.cs @@ -9,6 +9,27 @@ namespace Byway.TMP public bool m_OutlineUnderlayChanged; + public Material BackupOriginal() + { + Material mat = new Material(fontSharedMaterial); + mat.shaderKeywords = fontSharedMaterial.shaderKeywords; + mat.name += " (Backup)"; + return mat; + } + + public void AssignInstance(Material mat) + { + m_fontMaterial = mat; + + m_sharedMaterial = m_fontMaterial; + + m_padding = GetPaddingForMaterial(); + + m_ShouldRecalculateStencil = true; + SetVerticesDirty(); + SetMaterialDirty(); + } + public void NotifyHelper() { diff --git a/Packages/com.bywaystudios.textmeshpro.ext/Runtime/TextMeshProBywayUIRuntimeHelper.cs b/Packages/com.bywaystudios.textmeshpro.ext/Runtime/TextMeshProBywayUIRuntimeHelper.cs index d734e08..ac7dc3e 100644 --- a/Packages/com.bywaystudios.textmeshpro.ext/Runtime/TextMeshProBywayUIRuntimeHelper.cs +++ b/Packages/com.bywaystudios.textmeshpro.ext/Runtime/TextMeshProBywayUIRuntimeHelper.cs @@ -7,14 +7,32 @@ namespace Byway.TMP public class TextMeshProBywayUIRuntimeHelper : MonoBehaviour { private TextMeshProBywayUI m_TextComponent; + private Material m_TextMaterialOriginal; private Material m_TextMaterialInstance; + private Material CreateInstance(Material source) + { + // Get Shader PropertyIDs if they haven't been cached already. + ShaderUtilities.GetShaderPropertyIDs(); + Material mat = new Material(source); + mat.shaderKeywords = source.shaderKeywords; + mat.name += " (Instance)"; + return mat; + } + private void CheckMaterialInstance() { - DestroyMaterialInstanceIfNeeded(); if (m_TextComponent != null && (m_TextComponent.outline || m_TextComponent.underlay)) { - m_TextMaterialInstance = m_TextComponent.fontMaterial; + if (m_TextMaterialOriginal == null) + { + m_TextMaterialOriginal = m_TextComponent.BackupOriginal(); + } + if (m_TextMaterialInstance == null) + { + m_TextMaterialInstance = CreateInstance(m_TextMaterialOriginal); + m_TextComponent.AssignInstance(m_TextMaterialInstance); + } } } @@ -33,7 +51,6 @@ namespace Byway.TMP m_TextMaterialInstance.DisableKeyword(ShaderUtilities.Keyword_Outline); } - if (m_TextComponent.underlay) { m_TextMaterialInstance.EnableKeyword(ShaderUtilities.Keyword_Underlay); @@ -50,31 +67,33 @@ namespace Byway.TMP } } - private void DestroyMaterialInstanceIfNeeded() - { - if(m_TextComponent == null || (!m_TextComponent.outline && !m_TextComponent.underlay)) - { - DestroyMaterialInstance(); - } - } - - private void DestroyMaterialInstance() + private void DestroyAllMaterialInstances() { if (m_TextMaterialInstance != null) { Destroy(m_TextMaterialInstance); } + if (m_TextMaterialOriginal != null) + { + Destroy(m_TextMaterialOriginal); + } } private void Awake() { m_TextComponent = GetComponent(); + CheckMaterialInstance(); + Notify(); + } + + private void OnEnable() + { Notify(); } private void OnDestroy() { - DestroyMaterialInstance(); + DestroyAllMaterialInstances(); } public void Notify() diff --git a/Packages/com.bywaystudios.textmeshpro.ext/package.json b/Packages/com.bywaystudios.textmeshpro.ext/package.json index 81230b5..d8ce3f6 100644 --- a/Packages/com.bywaystudios.textmeshpro.ext/package.json +++ b/Packages/com.bywaystudios.textmeshpro.ext/package.json @@ -1,7 +1,7 @@ { "name": "com.bywaystudios.textmeshpro.ext", "displayName": "TextmeshPro Extension", - "version": "0.1.3", + "version": "0.1.5", "dependencies": { "com.unity.textmeshpro": "3.0.9" },