继续偷懒ing…
- [InitializeOnLoad] unity 会在用户之前去初始化这个编辑器类
- EditorApplication.hierarchyWindowItemOnGUI 当绘制层次窗口里面Item项时候
- EditorApplication.hierarchyWindowChanged 当绘制层次窗口改变的时候
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
[InitializeOnLoad]
public class HierarchyEditor
{
private static Texture2D hierarchyIcon;
public static Texture2D HierarchyIcon
{
get
{
if (hierarchyIcon == null)
{
HierarchyEditor.hierarchyIcon = Resources.Load<Texture2D>("bishi");
}
return hierarchyIcon;
}
}
static HierarchyEditor()
{
//添加绘制回调
EditorApplication.hierarchyWindowItemOnGUI = (EditorApplication.HierarchyWindowItemCallback)Delegate.Combine(
EditorApplication.hierarchyWindowItemOnGUI,
new EditorApplication.HierarchyWindowItemCallback(hierarchyItemCallback));
//添加改变回调
EditorApplication.hierarchyWindowChanged += hierarchyChangeCallback;
}
private static void hierarchyItemCallback(int instanceID, Rect selectionRect)
{
GameObject gameObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (gameObject != null && gameObject.name.Contains("Player"))
{
Rect rect = new Rect(selectionRect.x + selectionRect.width - 16f, selectionRect.y, 16f, 16f);
GUI.DrawTexture(rect, HierarchyEditor.HierarchyIcon);
}
}
private static void hierarchyChangeCallback()
{
//没办法知道谁改变了
//EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
}
}