Skip to content

Commit 5889486

Browse files
committed
fix: TextMeshPro objects appeared as black blocks when saving prefabs in prefab mode
close #285
1 parent f39097a commit 5889486

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Diagnostics;
3+
using UnityEditor;
4+
using UnityEngine;
5+
using Object = UnityEngine.Object;
6+
#if UNITY_EDITOR && UNITY_2021_2_OR_NEWER
7+
using UnityEditor.SceneManagement;
8+
#elif UNITY_EDITOR
9+
using UnityEditor.Experimental.SceneManagement;
10+
#endif
11+
12+
namespace Coffee.UIEffectInternal
13+
{
14+
internal static class Misc
15+
{
16+
public static T[] FindObjectsOfType<T>() where T : Object
17+
{
18+
#if UNITY_2023_1_OR_NEWER
19+
return Object.FindObjectsByType<T>(FindObjectsInactive.Include, FindObjectsSortMode.None);
20+
#else
21+
return Object.FindObjectsOfType<T>();
22+
#endif
23+
}
24+
25+
public static void Destroy(Object obj)
26+
{
27+
if (!obj) return;
28+
#if UNITY_EDITOR
29+
if (!Application.isPlaying)
30+
{
31+
Object.DestroyImmediate(obj);
32+
}
33+
else
34+
#endif
35+
{
36+
Object.Destroy(obj);
37+
}
38+
}
39+
40+
public static void DestroyImmediate(Object obj)
41+
{
42+
if (!obj) return;
43+
#if UNITY_EDITOR
44+
if (Application.isEditor)
45+
{
46+
Object.DestroyImmediate(obj);
47+
}
48+
else
49+
#endif
50+
{
51+
Object.Destroy(obj);
52+
}
53+
}
54+
55+
[Conditional("UNITY_EDITOR")]
56+
public static void SetDirty(Object obj)
57+
{
58+
#if UNITY_EDITOR
59+
if (!obj) return;
60+
EditorUtility.SetDirty(obj);
61+
#endif
62+
}
63+
64+
#if UNITY_EDITOR
65+
public static T[] GetAllComponentsInPrefabStage<T>() where T : Component
66+
{
67+
if (!PrefabStageUtility.GetCurrentPrefabStage()) return Array.Empty<T>();
68+
69+
var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
70+
if (!prefabStage) return Array.Empty<T>();
71+
72+
return prefabStage.prefabContentsRoot.GetComponentsInChildren<T>(true);
73+
}
74+
#endif
75+
}
76+
}

Packages/src/Runtime/Internal/Utilities/Misc.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/src/Runtime/UIEffectBase.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Runtime.CompilerServices;
34
using Coffee.UIEffectInternal;
45
using UnityEngine;
@@ -245,11 +246,8 @@ private static void OnPostprocessAllAssets(string[] _, string[] __, string[] ___
245246
if (Application.isBatchMode || BuildPipeline.isBuildingPlayer) return;
246247

247248
s_ShaderNameCache.Clear();
248-
#if UNITY_2021_3 || UNITY_2022_2_OR_NEWER
249-
foreach (var effect in FindObjectsByType<UIEffectBase>(FindObjectsSortMode.None))
250-
#else
251-
foreach (var effect in FindObjectsOfType<UIEffectBase>())
252-
#endif
249+
foreach (var effect in Misc.FindObjectsOfType<UIEffectBase>()
250+
.Concat(Misc.GetAllComponentsInPrefabStage<UIEffectBase>()))
253251
{
254252
if (!effect.isActiveAndEnabled) continue;
255253
if (!(effect.graphic is TextMeshProUGUI tmp) || !tmp.isActiveAndEnabled) continue;

0 commit comments

Comments
 (0)