fix: UIEffect with TextMeshPro appears black in the editor when saving scene

This commit is contained in:
mob-sakai
2025-02-13 11:51:48 +09:00
parent 6f6a8c95f8
commit 8cbcd765ed
5 changed files with 37 additions and 16 deletions
+3 -3
View File
@@ -1060,13 +1060,13 @@ namespace Coffee.UIEffects
c.gradationRotation = m_GradationRotation;
}
public override void ApplyContextToMaterial()
public override void ApplyContextToMaterial(Material material)
{
base.ApplyContextToMaterial();
base.ApplyContextToMaterial(material);
replicas.ForEach(c =>
{
if (!c) return;
c.ApplyContextToMaterial();
c.ApplyContextToMaterial(material);
});
}
+7 -7
View File
@@ -98,7 +98,7 @@ namespace Coffee.UIEffects
Profiler.EndSample();
}
ApplyContextToMaterial();
ApplyContextToMaterial(_material);
Profiler.EndSample();
return _material;
}
@@ -107,7 +107,7 @@ namespace Coffee.UIEffects
protected override void OnValidate()
{
UpdateContext(context);
ApplyContextToMaterial();
ApplyContextToMaterial(graphic ? graphic.canvasRenderer.GetMaterial() : _material);
SetVerticesDirty();
SetMaterialDirty();
@@ -141,7 +141,7 @@ namespace Coffee.UIEffects
if (graphic)
{
graphic.SetVerticesDirty();
GraphicProxy.Find(graphic).SetVerticesDirty(graphic);
GraphicProxy.Find(graphic).SetVerticesDirty(graphic, false);
#if UNITY_EDITOR
EditorApplication.QueuePlayerLoopUpdate();
#endif
@@ -161,14 +161,14 @@ namespace Coffee.UIEffects
protected abstract void UpdateContext(UIEffectContext c);
public virtual void ApplyContextToMaterial()
public virtual void ApplyContextToMaterial(Material material)
{
if (!isActiveAndEnabled || context == null) return;
if (!isActiveAndEnabled || context == null || !material) return;
context.ApplyToMaterial(_material, actualSamplingScale);
context.ApplyToMaterial(material, actualSamplingScale);
#if UNITY_EDITOR
UIEffectProjectSettings.shaderRegistry.RegisterVariant(_material, "UI > UIEffect");
UIEffectProjectSettings.shaderRegistry.RegisterVariant(material, "UI > UIEffect");
if (!EditorApplication.isPlaying)
{
EditorApplication.QueuePlayerLoopUpdate();
+2 -2
View File
@@ -107,11 +107,11 @@ namespace Coffee.UIEffects
{
}
public override void ApplyContextToMaterial()
public override void ApplyContextToMaterial(Material material)
{
if (!isActiveAndEnabled || !target || !target.isActiveAndEnabled) return;
base.ApplyContextToMaterial();
base.ApplyContextToMaterial(material);
}
public override void SetRate(float rate, UIEffectTweener.CullingMask mask)
@@ -68,7 +68,7 @@ namespace Coffee.UIEffects
}
}
public virtual void SetVerticesDirty(Graphic graphic)
public virtual void SetVerticesDirty(Graphic graphic, bool enabled)
{
}
+24 -3
View File
@@ -53,11 +53,19 @@ namespace Coffee.UIEffects
}
}
public override void SetVerticesDirty(Graphic graphic)
public override void SetVerticesDirty(Graphic graphic, bool enabled)
{
if (graphic is TextMeshProUGUI textMeshProUGUI && textMeshProUGUI.isActiveAndEnabled)
{
OnChangeText(textMeshProUGUI);
if (enabled)
{
OnChangeText(textMeshProUGUI);
}
else if (0 < textMeshProUGUI.textInfo?.meshInfo?.Length
&& 0 < textMeshProUGUI.textInfo.meshInfo[0].vertexCount)
{
textMeshProUGUI.UpdateVertexData();
}
}
}
@@ -174,6 +182,20 @@ namespace Coffee.UIEffects
s_ChangedInstances.Clear();
};
#if UNITY_EDITOR
UnityEditor.SceneManagement.EditorSceneManager.sceneSaved += _ =>
{
foreach (var textMeshProUGUI in s_RegisteredInstances)
{
if (!textMeshProUGUI || !textMeshProUGUI.isActiveAndEnabled) continue;
if (textMeshProUGUI.TryGetComponent<UIEffect>(out var effect) && effect.isActiveAndEnabled)
{
effect.SetMaterialDirty();
}
}
};
#endif
}
private static void OnChangeText(Object obj)
@@ -236,7 +258,6 @@ namespace Coffee.UIEffects
}
s_VertexHelper.FillMesh(s_Mesh);
replica.ApplyContextToMaterial();
subMeshUI.canvasRenderer.SetMesh(s_Mesh);
}
else