方案一:
编辑脚本类的在Inspector面板的Icon。如图:
有几点需要注意的:
-
必须修改脚本的Icon,修改由类派生的编辑器对象只能修改单个的Icon。
如图,
GraphInfo
类继承自ScriptableObject
,且已经创建了两个Graph对象如果我们需要修改Graph对象的Icon,需要修改的是②处Inspector面板上的Icon,而非①
-
修改后会发现只有脚本对象发生了改变,这时候新生成的Graph对象的Icon都会自动发生改变,如图,我们创建了Graph2,Icon已经改变。
如果需要其他原有对象也随之改变,请重新打开Unity
方案二:
使用编辑器脚本对对象进行手动绘制。
使用到的关键方法:InitializeOnLoad,EditorApplication.hierarchyWindowItemOnGUI,EditorApplication.projectWindowItemOnGUI
此处粘贴下他人的完整代码,供参考。
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
[UnityEditor.InitializeOnLoad]
public class ProjectIcons : Editor
{
static string basePath = "Assets/Unity Chan Run Run/Editor/Icons/";
static ProjectIcons()
{
EditorApplication.projectWindowItemOnGUI += ProjectIcons.MyCallback();
}
static EditorApplication.ProjectWindowItemCallback MyCallback()
{
EditorApplication.ProjectWindowItemCallback myCallback = new EditorApplication.ProjectWindowItemCallback(IconGUI);
return myCallback;
}
static void IconGUI(string s, Rect r)
{
Regex rootGameFolderPathRegex = new Regex(@"Assets/Unity Chan Run Run$");
string fileName = AssetDatabase.GUIDToAssetPath(s);
MatchThenDraw(r, rootGameFolderPathRegex, fileName, Path.Combine(basePath, "game.png"));
}
static void MatchThenDraw(Rect r, Regex regexPattern, String fileName, String texturePath)
{
Match match = regexPattern.Match(fileName);
if (match.Success)
{
r.width = r.height;
Texture2D icon = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));
if (icon != null)
{
GUI.DrawTexture(r, icon);
}
}
}
}