unity 扩展编辑器二 新建窗体

using UnityEngine;
using UnityEditor;
public class MyEditor : EditorWindow 
{
 
    [MenuItem ("GameObject/window")]
    static void AddWindow ()
    {       
        //创建窗口
        Rect  wr = new Rect (0,0,500,500);
        MyEditor window = (MyEditor)EditorWindow.GetWindowWithRect (typeof (MyEditor),wr,true,"widow name");    
        window.Show();
 
    }
}

EditorWindow.GetWindowWithRect() 和 EditorWindow.GetWindow()都可以创建一个窗口。前者可以规定窗口的区域,后者可通过鼠标动态的延伸窗口。参数1表示窗口的对象,参数2表示窗口的区域,参数3表示窗口类型true表示窗口不会被别的窗口覆盖,参数4表示窗口的名称。

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


public class MyEditor : EditorWindow {

    [MenuItem("GameObject/window")]
    static void AddWindow()
    {
        //创建窗口
        Rect wr=new Rect(0,0,500,500);
        MyEditor window =(MyEditor)EditorWindow.GetWindowWithRect(typeof(MyEditor),wr,true,"window name");
        window.Show();
    }

    //输入文字到内容
    private string text;

    //选择贴图的对象
    private Texture texture;


    public void Awake()
    {
        //在资源中读取一站贴图
        texture = Resources.Load("1") as Texture;
    }

    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();
        }

        //文本框显示鼠标再窗口的位置
        EditorGUILayout.LabelField("鼠标在窗口的位置",Event.current.mousePosition.ToString());

        //选择贴图
        texture = EditorGUILayout.ObjectField( "添加贴图" , texture , typeof ( Texture ) , true ) as Texture;


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

    }



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

    void OnLostFocus()
    {
        Debug.Log("当窗口失去焦点时调用一次");
    }

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

    void OnInspectorUpdate()
    {
        //重新绘制
        this.Repaint();
    }


    void OnSelectionChange()
    {
        //当窗口出去开启壮体啊,并且在Hierarchy视图中选择某个游戏对象时调用
        foreach(Transform t in Selection.transforms)
        {
            Debug.Log("OnSelectionChange"+t.name);
        }
    }

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

548583-20170617094302134-1972366111.png

转载于:https://www.cnblogs.com/yufenghou/p/7039410.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值