Skip to content

Commit 40ff828

Browse files
committed
fix: component icons will no longer be displayed in the scene view (Unity 2021.2 or later)
1 parent bdbf293 commit 40ff828

9 files changed

+67
-6
lines changed

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

+58-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
using UnityEditor;
44
using UnityEngine;
55
using Object = UnityEngine.Object;
6-
#if UNITY_EDITOR && UNITY_2021_2_OR_NEWER
6+
#if UNITY_EDITOR
7+
using System.IO;
8+
using System.Linq;
9+
using System.Reflection;
10+
#if UNITY_2021_2_OR_NEWER
711
using UnityEditor.SceneManagement;
8-
#elif UNITY_EDITOR
12+
#else
913
using UnityEditor.Experimental.SceneManagement;
1014
#endif
15+
#endif
1116

1217
namespace Coffee.UIEffectInternal
1318
{
@@ -72,5 +77,56 @@ public static T[] GetAllComponentsInPrefabStage<T>() where T : Component
7277

7378
public static bool isBatchOrBuilding => Application.isBatchMode || BuildPipeline.isBuildingPlayer;
7479
#endif
80+
81+
[Conditional("UNITY_EDITOR")]
82+
public static void QueuePlayerLoopUpdate()
83+
{
84+
#if UNITY_EDITOR
85+
if (!EditorApplication.isPlaying)
86+
{
87+
EditorApplication.QueuePlayerLoopUpdate();
88+
}
89+
#endif
90+
}
7591
}
92+
93+
#if !UNITY_2021_2_OR_NEWER
94+
[AttributeUsage(AttributeTargets.Class)]
95+
[Conditional("UNITY_EDITOR")]
96+
internal class IconAttribute : Attribute
97+
{
98+
private readonly string _path;
99+
100+
public IconAttribute(string path)
101+
{
102+
_path = path;
103+
}
104+
105+
#if UNITY_EDITOR
106+
private static Action<Object, Texture2D> s_SetIconForObject = typeof(EditorGUIUtility)
107+
.GetMethod("SetIconForObject", BindingFlags.Static | BindingFlags.NonPublic)
108+
.CreateDelegate(typeof(Action<Object, Texture2D>), null) as Action<Object, Texture2D>;
109+
110+
[InitializeOnLoadMethod]
111+
private static void InitializeOnLoadMethod()
112+
{
113+
if (Misc.isBatchOrBuilding) return;
114+
115+
var types = TypeCache.GetTypesWithAttribute<IconAttribute>();
116+
var scripts = MonoImporter.GetAllRuntimeMonoScripts();
117+
foreach (var type in types)
118+
{
119+
var script = scripts.FirstOrDefault(x => x.GetClass() == type);
120+
if (!script) continue;
121+
122+
var path = type.GetCustomAttribute<IconAttribute>()?._path;
123+
var icon = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
124+
if (!icon) continue;
125+
126+
s_SetIconForObject(script, icon);
127+
}
128+
}
129+
#endif
130+
}
131+
#endif
76132
}

Packages/src/Runtime/UIEffect.cs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace Coffee.UIEffects
77
{
8+
[Icon("Packages/com.coffee.ui-effect/Editor/UIEffectIconIcon.png")]
89
[ExecuteAlways]
910
[DisallowMultipleComponent]
1011
public class UIEffect : UIEffectBase

Packages/src/Runtime/UIEffect.cs.meta

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

Packages/src/Runtime/UIEffectProjectSettings.cs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Coffee.UIEffects
1010
{
11+
[Icon("Packages/com.coffee.ui-effect/Editor/UIEffectIconIcon.png")]
1112
public class UIEffectProjectSettings : PreloadedProjectSettings<UIEffectProjectSettings>
1213
{
1314
[Tooltip(

Packages/src/Runtime/UIEffectProjectSettings.cs.meta

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

Packages/src/Runtime/UIEffectReplica.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Coffee.UIEffects
55
{
6+
[Icon("Packages/com.coffee.ui-effect/Editor/UIEffectIconIcon.png")]
67
public class UIEffectReplica : UIEffectBase
78
{
89
[SerializeField] private UIEffect m_Target;

Packages/src/Runtime/UIEffectReplica.cs.meta

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

Packages/src/Runtime/UIEffectTweener.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
2+
using Coffee.UIEffectInternal;
23
using UnityEngine;
34
using UnityEngine.Events;
45
using UnityEngine.Serialization;
56

67
namespace Coffee.UIEffects
78
{
9+
[Icon("Packages/com.coffee.ui-effect/Editor/UIEffectIconIcon.png")]
810
[ExecuteAlways]
911
[RequireComponent(typeof(UIEffectBase))]
1012
public class UIEffectTweener : MonoBehaviour

Packages/src/Runtime/UIEffectTweener.cs.meta

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

0 commit comments

Comments
 (0)