2020/06/30에 해당하는 글 1

유니티 프로젝트 창 아이콘 Custom GUI

유니티 & C#|2020. 6. 30. 00:10
public class GizmoIconUtility
{
	[DidReloadScripts]
	static GizmoIconUtility()
	{
		EditorApplication.projectWindowItemOnGUI = ItemOnGUI;
	}

	static void ItemOnGUI(string guid, Rect rect)
	{
		string assetPath = AssetDatabase.GUIDToAssetPath(guid);
		ItemDefinition item = AssetDatabase.LoadAssetAtPath<ItemDefinition>(assetPath);

		if (item == null ||
			item.Icon == null)
		{
			return;
		}

		rect.height = rect.width;

		EditorGUI.DrawRect(rect, BackgroundColor);
		GUI.DrawTexture(rect, LoadTextureByRarity(item.Rarity));

		var iconRect = new Rect(rect);
		var center = iconRect.center;

		iconRect.width = rect.width * 0.75f;
		iconRect.height = rect.height * 0.75f;
		iconRect.center = center;

		GUI.DrawTexture(iconRect, item.Icon.texture, ScaleMode.ScaleToFit);

		var levelRect = new Rect(rect);
		levelRect.height = 18;

		string type = item.ItemType == ItemType.Weapon ? item.WeaponType.ToString() : item.ItemType.ToString();

		EditorGUI.LabelField(levelRect, $"Lv.{item.ItemLevel} / {type}", EditorStyles.miniBoldLabel);
	}

	static Texture LoadTextureByRarity(ItemRarity rarity)
	{
		switch (rarity)
		{
			case ItemRarity.Common:
				return AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Gizmos/Common.png");
			case ItemRarity.Uncommon:
				return AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Gizmos/Uncommon.png");
			case ItemRarity.Rare:
				return AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Gizmos/Rare.png");
			case ItemRarity.Epic:
				return AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Gizmos/Epic.png");
			case ItemRarity.Legendary:
				return AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Gizmos/Legendary.png");
			case ItemRarity.Artifact:
				return AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Gizmos/Artifact.png");
		}

		return Texture2D.blackTexture;
	}

	private static Color BackgroundColor => EditorGUIUtility.isProSkin
		? new Color32(56, 56, 56, 255)
		: new Color32(194, 194, 194, 255);

EditorApplication.projectWindowItemOnGUI를 통해 프로젝트 창에서 임의로 아이콘을 그려 넣을 수 있다

댓글()