Skip to content

Instantly share code, notes, and snippets.

@Quickz
Created December 20, 2020 18:08
Show Gist options
  • Save Quickz/0b62df1bfdcf86d35bc13b11cc6071ba to your computer and use it in GitHub Desktop.
Save Quickz/0b62df1bfdcf86d35bc13b11cc6071ba to your computer and use it in GitHub Desktop.
Scriptable Object instance icon
using UnityEngine;
[CreateAssetMenu(fileName = "New Example", menuName = "Example")]
public class ExampleScriptableObject : ScriptableObject
{
public Sprite Icon => icon;
[SerializeField]
private Sprite icon = null;
}
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