Created
December 20, 2020 18:08
-
-
Save Quickz/0b62df1bfdcf86d35bc13b11cc6071ba to your computer and use it in GitHub Desktop.
Scriptable Object instance icon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
[CreateAssetMenu(fileName = "New Example", menuName = "Example")] | |
public class ExampleScriptableObject : ScriptableObject | |
{ | |
public Sprite Icon => icon; | |
[SerializeField] | |
private Sprite icon = null; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEditor.Callbacks; | |
using UnityEngine; | |
public static class GizmoUtility | |
{ | |
[DidReloadScripts] | |
private static void GizmoIconUtility() | |
{ | |
EditorApplication.projectWindowItemOnGUI = ItemOnGUI; | |
} | |
private static void ItemOnGUI(string guid, Rect rect) | |
{ | |
string assetPath = AssetDatabase.GUIDToAssetPath(guid); | |
ExampleScriptableObject instance = AssetDatabase.LoadAssetAtPath( | |
assetPath, | |
typeof(ExampleScriptableObject)) as ExampleScriptableObject; | |
if (instance != null && instance.Icon != null) | |
{ | |
float initialWidth = rect.width; | |
rect.width = Mathf.Min(rect.height, rect.width); | |
rect.height = Mathf.Min(rect.height, rect.width); | |
rect.position = new Vector2( | |
rect.position.x + (initialWidth - rect.width) / 2f, | |
rect.position.y); | |
GUI.DrawTexture( | |
rect, | |
instance.Icon.texture); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment