如何为你的美术妹子做Unity的小工具(二)

你想像这样一样  为自己的Unity 小工具打开一个Unity的窗口吗?   看起来就很厉害对不对   妹子看了还不激动吗 ?!

在做你不知道的事情之前先去查一下Manual 是个很好的习惯  我们一起来看看吧

想要一个界面 但是又知道具体的类叫什么?   那我们先来搜Window看看能不能找到

好像还真有    Derive from this class to create an editor window.  派生这个类用来创建editor的窗口   好像很不错的样子

我们打开 先拿它的例子放到自己的代码中来看一下

using UnityEngine;
using UnityEditor;
public class MyWindow : EditorWindow {
    string myString = "Hello World";
    bool groupEnabled;
    bool myBool = true;
    float myFloat = 1.23f;
    
    // Add menu named "My Window" to the Window menu
    [MenuItem ("Window/My Window")]
    static void Init () {
        // Get existing open window or if none, make a new one:
        MyWindow window = (MyWindow)EditorWindow.GetWindow (typeof (MyWindow));
    }
    
    void OnGUI () {
        GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
            myString = EditorGUILayout.TextField ("Text Field", myString);
        
        groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", groupEnabled);
            myBool = EditorGUILayout.Toggle ("Toggle", myBool);
            myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);
        EditorGUILayout.EndToggleGroup ();
    }
}

它是在Window 下的 My window  我们找到打开   是不是有了一个界面呢

好吧  那我们结合manual的api 一句一句来看

EditorWindow.GetWindow

public static EditorWindow.GetWindow(Type t, bool utility = false, string title = null, bool focus = true);

带了三个参数 type t  创建的窗口的类型必须是继承EditorWindow的     utility 如果为真 则创建一个浮动的窗口  为真时时无法像Project窗口一样拖放到界面中的,为假 创建普通窗口可以拖放到界面中

title  窗口左上角显示的字   focus焦点   

Getwindow有很多的重载方法 也有泛型的写法  具体使用的时候可以根据自己的需求选择

之后的代码就是EditorGUILayout(编辑环境下自动布局GUI)画组件 继承于GUILayout 就不全部看了  创建更多的组件可以manual 中搜索EditorGUILayout进行查看

posted @ 2015-12-22 17:39  我爱吃橙子  阅读(409)  评论(0编辑  收藏  举报