实现的功能如下图
可以在对象的右侧画一个小图标,代码如下,放到Editor目录下,代码注释较多,就不详细介绍了
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class DrawHierarchyLockIcon
{
// 层级窗口项回调
private static readonly EditorApplication.HierarchyWindowItemCallback hiearchyItemCallback;
//放在Hierarchy 面板右边的图片
static Texture2D lockIcon = Resources.Load<Texture2D>("Ads/lock");
/// <summary>
/// 静态构造
/// </summary>
static DrawHierarchyLockIcon()
{
//初始化层级窗口项回调
DrawHierarchyLockIcon.hiearchyItemCallback = new EditorApplication.HierarchyWindowItemCallback(DrawHierarchyLockIcon.IconsOnGUI);
//加到委托列表
EditorApplication.hierarchyWindowItemOnGUI = (EditorApplication.HierarchyWindowItemCallback) Delegate.Combine(
EditorApplication.hierarchyWindowItemOnGUI,
DrawHierarchyLockIcon.hiearchyItemCallback);
}
//添加了对应脚本的游戏对象
static Dictionary<int, GameObject> lockRendererTable = new Dictionary<int, GameObject>();
//绘制图片放在 Hierarchy 面板右边
internal static void IconsOnGUI(int instanceId, Rect selectionRect)
{
//获取有特定脚本的对象
ItemLock[] arr = UnityEngine.Object.FindObjectsOfType<ItemLock>();//ItemLock 对象挂的脚本的类型,可以是任意脚本
//存放到字典
foreach (ItemLock item in arr)
{
lockRendererTable[item.gameObject.GetInstanceID()] = item.gameObject;
}
//生成放图片的矩形
Rect r = new Rect(selectionRect);
//如果当前的对象是要绘制的对象
if (lockRendererTable.ContainsKey(instanceId))
{
//如果移除了对应的脚本就不绘制
if (lockRendererTable[instanceId].GetComponent<ItemLock>() != null)//ItemLock 对象挂的脚本的类型,可以是任意脚本
{
//矩形赋值
r.x = r.width - 15;
r.width = 15;
//GUI绘制
GUI.Label(r, lockIcon);
}
}
}
}
跟新一下,这个脚本不能给不活动的对象加,而且只能加最多一个图标
新版修复了这些功能,如果有多个组件会显示2个图标而且图标的位置有了一点调整,不活动的对象现在也会被加图标了
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
[InitializeOnLoad]
public class DrawHierarchyLockIcon
{
// 层级窗口项回调
private static readonly EditorApplication.HierarchyWindowItemCallback hiearchyItemCallback;
//放在Hierarchy 面板右边的图片
static Texture2D lockIcon = Resources.Load<Texture2D>("Ads/lock");
/// <summary>
/// 静态构造
/// </summary>
static DrawHierarchyLockIcon()
{
//初始化层级窗口项回调
DrawHierarchyLockIcon.hiearchyItemCallback = new EditorApplication.HierarchyWindowItemCallback(DrawHierarchyLockIcon.IconsOnGUI);
//加到委托列表
EditorApplication.hierarchyWindowItemOnGUI = (EditorApplication.HierarchyWindowItemCallback) Delegate.Combine(
EditorApplication.hierarchyWindowItemOnGUI,
DrawHierarchyLockIcon.hiearchyItemCallback);
}
//添加了对应脚本的游戏对象
static Dictionary<int, GameObject> lockRendererTable = new Dictionary<int, GameObject>();
//绘制图片放在 Hierarchy 面板右边
internal static void IconsOnGUI(int instanceId, Rect selectionRect)
{
//获取有特定脚本的对象
ItemLock[] arr = Resources.FindObjectsOfTypeAll<ItemLock>(); //ItemLock 对象挂的脚本的类型,可以是任意脚本
//存放到字典
foreach (ItemLock item in arr)
{
lockRendererTable[item.gameObject.GetInstanceID()] = item.gameObject;
}
//生成放图片的矩形
Rect r = new Rect(selectionRect);
//如果当前的对象是要绘制的对象
if (lockRendererTable.ContainsKey(instanceId))
{
//如果移除了对应的脚本就不绘制
if (lockRendererTable[instanceId].GetComponent<ItemLock>() != null) //ItemLock 对象挂的脚本的类型,可以是任意脚本
{
//矩形赋值
r.xMin = 0;
r.x = r.width - 15;
r.width = 15;
//GUI绘制
GUI.Label(r, lockIcon);
if (lockRendererTable[instanceId].GetComponents<ItemLock>().Length != 1)
{
//多余的加了锁的脚本
//GUI绘制
var tempR = r;
tempR.position += new Vector2(-20, 0);
GUI.Label(tempR, lockIcon);
}
}
}
}
}