Skip to content

Commit

Permalink
fix: TextMeshPro objects appeared as black blocks when saving prefabs…
Browse files Browse the repository at this point in the history
… in prefab mode

close #285
  • Loading branch information
mob-sakai committed Dec 14, 2024
1 parent f39097a commit 5889486
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
76 changes: 76 additions & 0 deletions Packages/src/Runtime/Internal/Utilities/Misc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Diagnostics;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
#if UNITY_EDITOR && UNITY_2021_2_OR_NEWER
using UnityEditor.SceneManagement;
#elif UNITY_EDITOR
using UnityEditor.Experimental.SceneManagement;
#endif

namespace Coffee.UIEffectInternal
{
internal static class Misc
{
public static T[] FindObjectsOfType<T>() where T : Object
{
#if UNITY_2023_1_OR_NEWER
return Object.FindObjectsByType<T>(FindObjectsInactive.Include, FindObjectsSortMode.None);
#else
return Object.FindObjectsOfType<T>();
#endif
}

public static void Destroy(Object obj)
{
if (!obj) return;
#if UNITY_EDITOR
if (!Application.isPlaying)
{
Object.DestroyImmediate(obj);
}
else
#endif
{
Object.Destroy(obj);
}
}

public static void DestroyImmediate(Object obj)
{
if (!obj) return;
#if UNITY_EDITOR
if (Application.isEditor)
{
Object.DestroyImmediate(obj);
}
else
#endif
{
Object.Destroy(obj);
}
}

[Conditional("UNITY_EDITOR")]
public static void SetDirty(Object obj)
{
#if UNITY_EDITOR
if (!obj) return;
EditorUtility.SetDirty(obj);
#endif
}

#if UNITY_EDITOR
public static T[] GetAllComponentsInPrefabStage<T>() where T : Component
{
if (!PrefabStageUtility.GetCurrentPrefabStage()) return Array.Empty<T>();

var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
if (!prefabStage) return Array.Empty<T>();

return prefabStage.prefabContentsRoot.GetComponentsInChildren<T>(true);
}
#endif
}
}
11 changes: 11 additions & 0 deletions Packages/src/Runtime/Internal/Utilities/Misc.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions Packages/src/Runtime/UIEffectBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Coffee.UIEffectInternal;
using UnityEngine;
Expand Down Expand Up @@ -245,11 +246,8 @@ private static void OnPostprocessAllAssets(string[] _, string[] __, string[] ___
if (Application.isBatchMode || BuildPipeline.isBuildingPlayer) return;

s_ShaderNameCache.Clear();
#if UNITY_2021_3 || UNITY_2022_2_OR_NEWER
foreach (var effect in FindObjectsByType<UIEffectBase>(FindObjectsSortMode.None))
#else
foreach (var effect in FindObjectsOfType<UIEffectBase>())
#endif
foreach (var effect in Misc.FindObjectsOfType<UIEffectBase>()
.Concat(Misc.GetAllComponentsInPrefabStage<UIEffectBase>()))
{
if (!effect.isActiveAndEnabled) continue;
if (!(effect.graphic is TextMeshProUGUI tmp) || !tmp.isActiveAndEnabled) continue;
Expand Down

0 comments on commit 5889486

Please sign in to comment.