Unity Editor OnInspectorGUI的重写与面板的创建

Unity Editor OnInspectorGUI的重写与面板的创建

对编辑器类的相关操作我们都是放在Unity文件夹下的Editor目录下的。

 

OnInspectorGUI()是Unity的Editor类里的相关函数,通过对该方法的重写,可以自定义对Inspector面板的绘制。

 

using UnityEngine;
using System.Collections;

public class Test1 : MonoBehaviour 
{
    public bool myBool;
    public string myString;
}

 

using UnityEditor;
using UnityEngine;
 
[CustomEditor(typeof(Test1))]
public class Test1Editor : Editor 
{
    public override void OnInspectorGUI() 
	{
        Test1 test = (Test1) target;
        test.myBool = EditorGUILayout.Toggle("是否选中", test.myBool);
        test.myString = EditorGUILayout.TextField("输入文本",test.myString);    
	}
}

 

达到了重新自定义绘制这一面板的功能。

 

EditorWindow可以用来创建面板,如下代码所示:

 

using UnityEngine;
using UnityEditor;

public class Test2Editor : EditorWindow
{
    [MenuItem("MyEditor/CreatWindow")]
    static void AddWindow()
    {
        Rect wr = new Rect(0, 0, 500, 500);
        Test2Editor window = (Test2Editor)EditorWindow.GetWindowWithRect(typeof(Test2Editor), wr, true, "CreatWindow");
        window.Show();
    }
    private string text;

    //绘制窗口时调用
    void OnGUI()
    {
        text = EditorGUILayout.TextField("输入文字:", text);

        if (GUILayout.Button("打开通知", GUILayout.Width(200)))
        {
            this.ShowNotification(new GUIContent("This is a Notification"));
        }

        if (GUILayout.Button("关闭通知", GUILayout.Width(200)))
        {
            this.RemoveNotification();
        }

        if (GUILayout.Button("关闭窗口", GUILayout.Width(200)))
        {
            this.Close();
        }

    }

    void OnFocus()
    {
        Debug.Log("获得焦点时调用一次");
    }

    void OnLostFocus()
    {
        Debug.Log("丢失焦点时调用一次");
    }

    void OnHierarchyChange()
    {
        Debug.Log("Hierarchy视图中的任何对象发生改变时调用一次");
    }

    void OnProjectChange()
    {
        Debug.Log("Project视图中的资源发生改变时调用一次");
    }

    void OnInspectorUpdate()
    {
        Debug.Log("面板的更新");
        this.Repaint();
    }

    void OnSelectionChange()
    {
        //处于开启状态,并且在Hierarchy视图中选择某游戏对象时调用
        foreach (Transform t in Selection.transforms)
        {
            Debug.Log("OnSelectionChange" + t.name);
        }
    }

    void OnDestroy()
    {
        Debug.Log("关闭时调用");
    }
}

通过以上代码,会在Unity的菜单栏增加一项,点击时就会弹出面板。

 

 

结束。

转载于:https://my.oschina.net/ffs/blog/847042

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值