Skip to content

Commit 8cbcd76

Browse files
committed
fix: UIEffect with TextMeshPro appears black in the editor when saving scene
1 parent 6f6a8c9 commit 8cbcd76

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

Packages/src/Runtime/UIEffect.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1060,13 +1060,13 @@ protected override void UpdateContext(UIEffectContext c)
10601060
c.gradationRotation = m_GradationRotation;
10611061
}
10621062

1063-
public override void ApplyContextToMaterial()
1063+
public override void ApplyContextToMaterial(Material material)
10641064
{
1065-
base.ApplyContextToMaterial();
1065+
base.ApplyContextToMaterial(material);
10661066
replicas.ForEach(c =>
10671067
{
10681068
if (!c) return;
1069-
c.ApplyContextToMaterial();
1069+
c.ApplyContextToMaterial(material);
10701070
});
10711071
}
10721072

Packages/src/Runtime/UIEffectBase.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public virtual Material GetModifiedMaterial(Material baseMaterial)
9898
Profiler.EndSample();
9999
}
100100

101-
ApplyContextToMaterial();
101+
ApplyContextToMaterial(_material);
102102
Profiler.EndSample();
103103
return _material;
104104
}
@@ -107,7 +107,7 @@ public virtual Material GetModifiedMaterial(Material baseMaterial)
107107
protected override void OnValidate()
108108
{
109109
UpdateContext(context);
110-
ApplyContextToMaterial();
110+
ApplyContextToMaterial(graphic ? graphic.canvasRenderer.GetMaterial() : _material);
111111
SetVerticesDirty();
112112
SetMaterialDirty();
113113

@@ -141,7 +141,7 @@ public virtual void SetVerticesDirty()
141141
if (graphic)
142142
{
143143
graphic.SetVerticesDirty();
144-
GraphicProxy.Find(graphic).SetVerticesDirty(graphic);
144+
GraphicProxy.Find(graphic).SetVerticesDirty(graphic, false);
145145
#if UNITY_EDITOR
146146
EditorApplication.QueuePlayerLoopUpdate();
147147
#endif
@@ -161,14 +161,14 @@ public virtual void SetMaterialDirty()
161161

162162
protected abstract void UpdateContext(UIEffectContext c);
163163

164-
public virtual void ApplyContextToMaterial()
164+
public virtual void ApplyContextToMaterial(Material material)
165165
{
166-
if (!isActiveAndEnabled || context == null) return;
166+
if (!isActiveAndEnabled || context == null || !material) return;
167167

168-
context.ApplyToMaterial(_material, actualSamplingScale);
168+
context.ApplyToMaterial(material, actualSamplingScale);
169169

170170
#if UNITY_EDITOR
171-
UIEffectProjectSettings.shaderRegistry.RegisterVariant(_material, "UI > UIEffect");
171+
UIEffectProjectSettings.shaderRegistry.RegisterVariant(material, "UI > UIEffect");
172172
if (!EditorApplication.isPlaying)
173173
{
174174
EditorApplication.QueuePlayerLoopUpdate();

Packages/src/Runtime/UIEffectReplica.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ protected override void UpdateContext(UIEffectContext c)
107107
{
108108
}
109109

110-
public override void ApplyContextToMaterial()
110+
public override void ApplyContextToMaterial(Material material)
111111
{
112112
if (!isActiveAndEnabled || !target || !target.isActiveAndEnabled) return;
113113

114-
base.ApplyContextToMaterial();
114+
base.ApplyContextToMaterial(material);
115115
}
116116

117117
public override void SetRate(float rate, UIEffectTweener.CullingMask mask)

Packages/src/Runtime/Utilities/GraphicProxy.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public virtual void OnPreModifyMesh(Graphic graphic)
6868
}
6969
}
7070

71-
public virtual void SetVerticesDirty(Graphic graphic)
71+
public virtual void SetVerticesDirty(Graphic graphic, bool enabled)
7272
{
7373
}
7474

Packages/src/Runtime/Utilities/TmpProxy.cs

+24-3
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,19 @@ public override void OnPreModifyMesh(Graphic graphic)
5353
}
5454
}
5555

56-
public override void SetVerticesDirty(Graphic graphic)
56+
public override void SetVerticesDirty(Graphic graphic, bool enabled)
5757
{
5858
if (graphic is TextMeshProUGUI textMeshProUGUI && textMeshProUGUI.isActiveAndEnabled)
5959
{
60-
OnChangeText(textMeshProUGUI);
60+
if (enabled)
61+
{
62+
OnChangeText(textMeshProUGUI);
63+
}
64+
else if (0 < textMeshProUGUI.textInfo?.meshInfo?.Length
65+
&& 0 < textMeshProUGUI.textInfo.meshInfo[0].vertexCount)
66+
{
67+
textMeshProUGUI.UpdateVertexData();
68+
}
6169
}
6270
}
6371

@@ -174,6 +182,20 @@ private static void InitializeOnLoad()
174182

175183
s_ChangedInstances.Clear();
176184
};
185+
186+
#if UNITY_EDITOR
187+
UnityEditor.SceneManagement.EditorSceneManager.sceneSaved += _ =>
188+
{
189+
foreach (var textMeshProUGUI in s_RegisteredInstances)
190+
{
191+
if (!textMeshProUGUI || !textMeshProUGUI.isActiveAndEnabled) continue;
192+
if (textMeshProUGUI.TryGetComponent<UIEffect>(out var effect) && effect.isActiveAndEnabled)
193+
{
194+
effect.SetMaterialDirty();
195+
}
196+
}
197+
};
198+
#endif
177199
}
178200

179201
private static void OnChangeText(Object obj)
@@ -236,7 +258,6 @@ private static void ModifyMesh(TextMeshProUGUI textMeshProUGUI)
236258
}
237259

238260
s_VertexHelper.FillMesh(s_Mesh);
239-
replica.ApplyContextToMaterial();
240261
subMeshUI.canvasRenderer.SetMesh(s_Mesh);
241262
}
242263
else

0 commit comments

Comments
 (0)