![2e89ef2c8476a37fd32d80c8a9b1ce36.png](https://img-blog.csdnimg.cn/img_convert/2e89ef2c8476a37fd32d80c8a9b1ce36.png)
Unity Editor扩展概览
unity editor是一个通用的编辑器,提供了unity内部对象的创建,预览,编辑的功能及可视化界面。但对于特定类型的游戏开发可能还不能完全满足需求,还要根据程序,策划和美术的需求来进行扩展。常用的一些扩展包括打包界面,特定游戏对象的编辑界面,技能编辑器界面,自定义的场景编辑界面,美术资源导入批量设置等等。unity提供了丰富的接口来帮助开发者定制需求的功能。
用于扩展editor的类需要放到名字为“Editor"的文件夹中,这个文件夹可以直接放到”Assets“文件夹下,也可以是项目目录任何文件夹下的子文件夹,比如”Assets/BigWorld/Editor"。
几种类型的扩展
unity提供了由简单到复制的多种编辑器扩展功能,下面给大家进行介绍常用的4种。
扩展工具栏菜单
使用MenuItem特性扩展工具栏菜单。此扩展方法可以单独使用执行某些功能,也可以和其他窗口扩展功能一起使用,打开其他窗口。
using UnityEngine;
using System.Collections;
using UnityEditor;
public class ArtResChecker
{
[MenuItem("ArtResChecker/CheckArtRes")]
static public void ATestCheckArtResFunction()
{
CheckTextures();
CheckMeshes();
}
}
代码1-1
代码1-1是一个简单的示例。使用"MenuItem"需要引入"UnityEditor"命名空间。这样会在菜单栏中出现"ArtResChecker"菜单,点击下拉菜单中的"CheckArtRes"项,会执行”ATestCheckArtResFunction"函数。
也可以扩展在已有的菜单中进行扩展,如代码1-2所示在“GameObject"菜单中增加了一项"AddChild"。
using UnityEngine;
using System.Collections;
using UnityEditor;
public class ArtResChecker
{
[MenuItem("GameObject/AddChild")]
static void ATestAddChildFunction()
{
Transform[] transforms = Selection.GetTransforms(SelectionMode.Unfiltered);
foreach(var tf in transforms)
{
GameObject newGo = new GameObject("_Child");
newGo.transform.parent = tf;
}
}
}
代码1-2
这个扩展脚本从菜单的“GameObject->Add Child”