在开发过程中hierarchy面板加一些提示,当然你也可以画一些icon,用来提示制作prefab的过程中不规范的操作,可以实时监测,避免出现问题,再回头优化!效果如下:
具体做法如下:
1.自定义静态类必须注明[InitializeOnLoad] //Unity启动的时候执行编辑器的脚本 需要静态的构造函数
2.一个静态构造,主要用来load icon资源,另一个就是注册编辑器事件
using UnityEditor;
using UnityEngine.UI;
[InitializeOnLoad]
public class HierarchyExtension
{
/// <summary>
/// 静态构造
/// </summary>
static HierarchyExtension()
{
//load icon icon = (Texture2D)Resources.Load ("icon");
EditorApplication.hierarchyWindowItemOnGUI += DrawHierarchyIcon;
}
private static void DrawHierarchyIcon(int instanceID, Rect selectionRect)
{
GameObject obj = EditorUtility.InstanceIDToObject (instanceID) as GameObject;
DrawErrorImage(obj , selectionRect);
}
static void DrawErrorImage(GameObject go, Rect selectionRect)
{
Image image = go.GetComponent<Image>();
if (image != null)
{
SerializedObject so = new SerializedObject(image);
SerializedProperty sp = so.GetIterator();
Rect rect;
while (sp.NextVisible(true))
{
if (sp.propertyType == SerializedPropertyType.ObjectReference)
{
if (sp.displayName == "Sprite" && sp.objectReferenceValue == null)
{
rect = new Rect(selectionRect.x + selectionRect.width - 206f - UsedWidth, selectionRect.y, 86f, 16f);
UsedWidth += 80;
textStyle.normal.textColor = Color.yellow;
GUI.Label(rect, "Image为空,用EmptyImage替换", textStyle);
}
}
}
}
}
}