unity中自定義窗口的擴展

1,新建C#腳本命名爲MyWindowEditor

腳本內容如下:

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class MyWindowEditor : EditorWindow {

	[MenuItem("GameObject/ShowWindow")]
    static void ShowWindow()
    {
        Rect theRect = new Rect(0, 0, 500, 500);
        MyWindowEditor window=(MyWindowEditor)EditorWindow.GetWindowWithRect(typeof(MyWindowEditor),theRect,true,"我的窗口");
        //若爲false,則可以進行拖動,true爲獨立的窗口,窗口類型true表示窗口不會被別的窗口覆蓋
        window.Show();
    }
}

結果如下:

2.在窗口中進行一些基礎設置

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class MyWindowEditor : EditorWindow {
    bool groundEnable = false;//一個布爾值的變量
    float value1 = 0;
    float value2 = 0;
    [MenuItem("GameObject/ShowWindow")]
    static void ShowWindow()
    {
        Rect theRect = new Rect(0, 0, 500, 500);
        MyWindowEditor window=(MyWindowEditor)EditorWindow.GetWindowWithRect(typeof(MyWindowEditor),theRect,true,"我的窗口");
        //若爲false,則可以進行拖動,true爲獨立的窗口,窗口類型true表示窗口不會被別的窗口覆蓋
        window.Show();
    }
    void OnGUI()
    {
        if(GUILayout.Button("打開通知",GUILayout.Width(220)))
        {
            this.ShowNotification(new GUIContent("要通知的內容"));
        }
        if (GUILayout.Button("關閉通知", GUILayout.Width(220)))
        {
            this.RemoveNotification();
        }
        if (GUILayout.Button("關閉窗口", GUILayout.Width(220)))
        {
            this.Close();
        }
        GUILayout.Label("以下是基礎設置");
        groundEnable = EditorGUILayout.BeginToggleGroup("啓用", groundEnable);
        value1 = EditorGUILayout.FloatField("值1", value1);
        value2 = EditorGUILayout.FloatField("值2", value2);
        //用複寫框把選項管理起來
        EditorGUILayout.EndFadeGroup();
    }
}

窗口結果如下:

以下是窗口中常用到的函數:

//窗口獲得焦點的時候被調用
    void OnFocus()
    {
        Debug.Log("窗口獲得了焦點");
    }
    //窗口失去焦點的時候被調用
    void OnLostFocus()
    {
        Debug.Log("窗口失去了焦點");
    }
    void OnHierarchyChange()
    {
        Debug.Log("層次面板的對象發生改變");
    }
    void OnProjectChange()
    {
        Debug.Log("項目面板的對象發生改變");
    }
    void OnSelectionChange()
    {
        foreach(Transform item in Selection.transforms)
        {
            Debug.Log("被選中的物體是" + item.name);
        }
    }
    void OnDestroy()
    {
        Debug.Log("窗口被關閉了");
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章