Inspector 窗口下方的 preview 窗口也可以扩展,绘制自己的 GUI。
方法:继承 ObjectPreview,并声明 [CustomPreview(typeof(GameObject))]。
using UnityEngine;
using UnityEditor;
/// <summary>
/// 扩展选中对象后的预览窗口
/// </summary>
[CustomPreview(typeof(GameObject))]
public class ObjectPreviewExtend : ObjectPreview
{
public override bool HasPreviewGUI()
{
GameObject go = Selection.activeGameObject;
// 可以根据对象判断是否需要显示
return true;
}
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
Texture tex = AssetDatabase.LoadAssetAtPath<Texture>("Assets/unity.png");
if(tex != null)
GUI.DrawTexture(r, tex);
GUILayout.Label("Hello world!");
if(Selection.activeGameObject)
GUILayout.Label(Selection.activeGameObject.name);
//base.OnPreviewGUI(r, background);
}
}