Unity编辑器拓展--自定义Window拓展

Unity编辑器拓展--自定义Window拓展

前言

我们在编辑器拓展的时候经常需要打开我们的自定义的窗口,那么这样一个功能应该怎么实现呢?
首先我们创建一个脚本来控制我们的Window的拓展:
在这里插入图片描述

自定义窗口拓展

比如现在我们需要打开我们之前创建的一个顶部菜单栏的Test2来打开我们自定义的这个窗口,具体的实现可以参考以下代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class TextMenu : Editor
{
    [MenuItem("MenuExpand/Test1", false, 3)]
    static void Test1() 
    {
        Debug.Log("1");
    }
    [MenuItem("MenuExpand/Test2", false, 2)]
    static void Test2()
    {
        //Debug.Log("2");
        TestWindow testWindow = EditorWindow.GetWindow<TestWindow>();
        testWindow.Show();
    }
    [MenuItem("MenuExpand/Test3", false, 1)]
    static void Test3()
    {
        Debug.Log("3");
    }
}

在这里插入图片描述
然后我们再在新创建的窗口中绘制内容,具体如下所示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class TestWindow : EditorWindow
{
    //重写OnGUI
    private void OnGUI()
    {
        if(GUILayout.Button("testWindow Button")) 
        {
            Debug.Log("test Window button");
        }
    }
    private void Awake()
    {
        //当初始化的时候调用
        Debug.Log("Awake");
    }
    private void OnDestroy()
    {
        //当销毁的时候调用
        Debug.Log("OnDestroy");
    }
    private void OnFocus()
    {
        //当处于焦点的时候调用
        Debug.Log("OnFocus");
    }
    private void OnLostFocus()
    {
        //失去焦点的时候调用
        Debug.Log("OnLostFocus");
    }
    private void Update()
    {
        //当更新的时候调用
        Debug.Log("Update");
    }
}

在这里插入图片描述
在这里插入图片描述

下拉菜单拓展

这个弹出框的下来菜单我们也是可以通过IHasCustomMenu该接口来实现拓展的:
在这里插入图片描述
代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class TestWindow : EditorWindow ,IHasCustomMenu
{
    //是不是打开的
    private bool isOn;
    //重写OnGUI
    private void OnGUI()
    {
        if(GUILayout.Button("testWindow Button")) 
        {
            Debug.Log("test Window button");
        }

    }
    private void Awake()
    {
        //当初始化的时候调用
        Debug.Log("Awake");
    }
    private void OnDestroy()
    {
        //当销毁的时候调用
        Debug.Log("OnDestroy");
    }
    private void OnFocus()
    {
        //当处于焦点的时候调用
        Debug.Log("OnFocus");
    }
    private void OnLostFocus()
    {
        //失去焦点的时候调用
        Debug.Log("OnLostFocus");
    }
    private void Update()
    {
        //当更新的时候调用
        Debug.Log("Update");
    }

    public void AddItemsToMenu(GenericMenu menu)
    {
        menu.AddItem(new GUIContent("test1"), false, () =>
          {
              Debug.Log("Addtest1");
              isOn = !isOn;
              if (isOn) 
              {
                  Debug.Log("isOn is true");
              }
              else
              {
                  Debug.Log("isOn is false");
              }
          });
        //添加不可操作的下来菜单选项
        menu.AddDisabledItem(new GUIContent("test2"),false);
        //throw new System.NotImplementedException();
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT学徒.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值