Unity编辑器扩展_窗口创建

方法1、ScriptableWizard

using UnityEditor;
//展示对话框中的属性
public class PlayerChange : ScriptableWizard
{
    public string nameId = "吕布";
    public int level = 10;
    public float health = 150;
    public string skill = "狂魔乱舞";
    public float hurt = 15.3f;
    
    /// 
    /// 监测"Change Player"按钮的点击并执行方法
    /// 方法名必须为OnWizardCreate
    /// 
    private void OnWizardCreate()
    {
        Debug.Log("点击了Change Player按钮");
    }
    /// 
    /// 创建表和属性被修改的时候每帧被调用
    /// 
    private void OnWizardUpdate()
    {
        //显示一个提示信息
        ShowNotification(new GUIContent("OnWizardUpdate被调用了"));
        //Debug.Log("OnWizardUpdate被调用");
        OnSelectionChange();  //物体状态发生改变时每帧被调用
    }
    /// 
    /// 当物体在选中或未选中状态发生改变时调用
    /// 
    private void OnSelectionChange()
    {
        if (Selection.gameObjects.Length > 0)
        {
            errorString = null;
            helpString = "您当前选择了" + Selection.gameObjects.Length + "个物体";
        }
        else
        {
            errorString = "请选择至少一个物体";
        }
    }
}
using UnityEditor;
//创建按钮即对话框
public class playerDialog 
{
    //把下面代码写到上面脚本里也是可以的,两个脚本合一个脚本看起来更直观
    [MenuItem("MyTool/CreatWizard")]
    static void CreatWizard()
    {
        //创建对话框
        //参数1:标题名称;参数2:右下角按钮名称,不指定默认为"Create"
        ScriptableWizard.DisplayWizard<PlayerChange>("修改player属性","Change Player");
    }
}

效果展示:
Unity编辑器扩展_窗口创建_第1张图片

方法2、EditorWindow

using UnityEngine;
using UnityEditor;

public class MyWindow : EditorWindow
{
    string myString = "Hello World";
    bool groupEnabled;
    bool myBool = true;
    float myFloat = 1.23f;

    [MenuItem("MyTool/My Window")]
    static void Init()
    {
        //写法1
        //MyWindow window = (MyWindow)EditorWindow.GetWindow(typeof(MyWindow));
        //写法2
        MyWindow window = EditorWindow.GetWindow<MyWindow>();
        window.Show();
        //写法3
        //MyWindow.GetWindow(typeof(MyWindow));
    }
    void OnGUI()
    {
        GUILayout.Label("Base Settings", EditorStyles.boldLabel);
        myString = EditorGUILayout.TextField("Text Field", myString);
        groupEnabled = EditorGUILayout.BeginToggleGroup("BeginToggleGroup", groupEnabled);
        myBool = EditorGUILayout.Toggle("Toggle", myBool);
        myFloat = EditorGUILayout.Slider("Slider", myFloat, -3, 3);
        EditorGUILayout.EndToggleGroup();
    }
}

效果展示:
Unity编辑器扩展_窗口创建_第2张图片

附:使用EditorPrefs保存数据
using UnityEngine;
using UnityEditor;

public class MyWindow : EditorWindow
{
    int intValue = 42;
    int temp = 0;
    [MenuItem("My Tool/Prefs.GetInt Example")]
    static void Init()
    {
        MyWindow window = (MyWindow)EditorWindow.GetWindow(typeof(MyWindow));
        window.Show();
    }
    /// 
    /// 保存数据
    /// 
    void OnGUI()
    {
        temp = EditorPrefs.GetInt("SetIntExample", temp);
        EditorGUILayout.LabelField("Current stored value: " + temp.ToString());
        intValue = EditorGUILayout.IntField("Value to write to Prefs: ", intValue);
        if (GUILayout.Button("Save value: " + intValue.ToString()))
        {
            EditorPrefs.SetInt("SetIntExample", intValue);
            Debug.Log("SetInt: " + intValue);
        }
    }
}

你可能感兴趣的:(unity)