public class FunEditorWindow : EditorWindow
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
[MenuItem("WindowCreate/WindowEditor")]
public static void CreateWindow()
{
FunEditorWindow.CreateInstance<FunEditorWindow>().Show();
}
public void OnGUI()
{
if (GUILayout.Button("Button"))
{
Debug.Log("click ok");
}
}
}
Unity3D编辑器插件开发
学习视频连接地址
目录:
一,菜单项相关操作
1.菜单项(MenuItem)
首先在Asset目录下新建一个Editor文件夹,然后在该目录下添加一个脚本MenuTest.cs
代码内容:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class MenuTest : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
/// <summary>
/// _w 单一的快捷键 W
/// #w shift + w
/// %w ctrl + w
/// &w alt + w
/// </summary>
[MenuItem("菜单/Test _w")]
public static void Test1()
{
Debug.Log("Test_-----");
}
[MenuItem("菜单/Test# #w")]
public static void Test2 ()
{
Debug.Log("Test#-----");
}
[MenuItem("菜单/Test% %w")]
public static void Test3 ()
{
Debug.Log("Test%-----");
}
[MenuItem("菜单/Test& &w")]
public static void Test4 ()
{
Debug.Log("Test&-----");
}
}
在编辑器中选择菜单点击效果如下:
2.组件菜单(AddComponentMenu)
就是将自己所写的脚本添加到Component菜单下面
在Scripts目录下新建一个脚本组件,Test1.cs
代码内容为:
using UnityEngine;
using System.Collections;
using UnityEditor;
[AddComponentMenu("菜单组件/MenuTest")]
public class Test1 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
然后就可以在Component组件中看到 菜单组件下的MenuTest组件
然后在一个GameObject Add Component下也可以看到刚才添加的菜单组件
3.上下文菜单(ContextMenu)
就是在一个物体的组件上添加上下文菜单,即点击右键出现的菜单
就在上文的Test1中添加一个上下文菜单
代码内容如下:
using UnityEngine;
using System.Collections;
using UnityEditor;
[AddComponentMenu("菜单组件/MenuTest")]
public class Test1 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
[ContextMenu("ContextMenu")]
public void Test()
{
Debug.Log("上下文测试");
}
}
然后将Test1.cs这个组件添加到物体上,然后再点击右键,可以看到如下效果:
4.必要组件菜单(RequireComponent)
必要组件是在添加改组件的同时添加其他的组件,而且是依赖于这个组件的,
同样在上文的Test.cs中添加必要组件,
代码内容为:
using UnityEngine;
using System.Collections;
using UnityEditor;
[RequireComponent(typeof(Rigidbody))]
[AddComponentMenu("菜单组件/MenuTest")]
public class Test1 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
[ContextMenu("ContextMenu")]
public void Test()
{
Debug.Log("上下文测试");
}
}
此时在一个物体上添加Test.cs组件的同时就将Rigidbody组件也添加上去了。在移除这个组件的时候要先移除Test1组件,然后才能移除Rigidbody,否则不能移除成功
二,自定义Inspector界面
1.属性相关标识
1)隐藏属性显示 (HideInInspector)
2)不显示的属性(NonSerialized)
3)关键字序列化(Serializable)
4)序列化域(SerializeField)
首先新建脚本文件,InspectorTest.cs 和脚本Type1.cs
InspectorTest.cs:
using System;
using UnityEngine;
using System.Collections;
public class InspectorTest : MonoBehaviour
{
public