目录
扩展布局 (Project 视图脚本 代码的右侧扩展自定义按钮)
扩展布局(点击Hierarch中的游戏对象在右侧扩展出一组按钮 )
扩展Project视图中的右键视图菜单
● 编译器使用的代码应该仅限于编辑模式下, 也就是说正式的游戏包不应该包含这些代码。
● Unity的C#脚本分为两种,一种是给游戏本身运行的,一种是个编辑器运行的;其中给编辑器运行的脚本必须在Editor文件夹下,如果属于运行时执行的代码,放在任意非 Editor 文件夹下即可。
● 可以把脚本资源放在一个命名为scripts的文件夹下。
● 如何创建My Tools 菜单集合:
首先先跟上述那样操作,创建一个Scripts文件夹,然后子文件夹Editor,然后鼠标右键Editor 文件夹点击: Create-------->C# scripts. 这样。
然后把下列代码打进去:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class Script_03_01
{
[MenuItem("Assets/My Tools/Tools 1", false, 2)]
static void MyTools1()
{
Debug.Log(Selection.activeObject.name);
}
[MenuItem("Assets/My Tools/Tools 2", false, 1)]
static void MyTools2()
{
Debug.Log(Selection.activeObject.name);
}
}
然后切换到Unity3D,后发现右下角,有一个在转圈的东西,这是Unity再编译你刚刚改的代码;带起转圈完毕后,就说明编译好了。
然后在project视图中的脚本代码上鼠标右键 或者 主菜单栏上的Assets里就能看见我们在Unity 中扩展的功能。
我们点击其中一个 Tools, 就会在 Console 中
输出相应的日志了、
● 说明: 自定义菜单的参数需要在MenuItem 方法中写入显示的菜单路径。 如果菜单条比较多,可以在第三个参数处输入表示排序的整数,数值越小,它的排序就越靠越靠前。 最后使用 Debug.Log 打印选择的游戏对象 Selection.activeObject.name 即可。
在project视图中的Create按钮中扩展资源创建菜单
● 跟上面方法一样,贴代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_03_02
{
[MenuItem("Assets/Create/My Create/Cube", false, 2)]
static void CreateCube()
{
GameObject.CreatePrimitive(PrimitiveType.Cube); //创建立方体
}
[MenuItem("Assets/Create/My Create/Sphere", false, 1)]
static void CreateSphere()
{
GameObject.CreatePrimitive(PrimitiveType.Sphere);//创建球体
}
}
● 扩展菜单的关键就是找到正确的菜单路径,通过 “/ ” 符号将它们拼合而成,
● GameObject.CreatePrimitive (PrimitiveType. Type) 方法用于创建 Unity 基础原始游戏对象;
参数Type表示要创建的原始对象的类型。
最后成功扩展了 My Create 菜单:
using UnityEngine;
public class Example : MonoBehaviour
{
//在场景中创建一个平面,球体,立方体,胶囊,圆柱体
void Start()
{
GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = new Vector3(0, 0.5f, 0);
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = new Vector3(0, 1.5f, 0);
GameObject capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
capsule.transform.position = new Vector3(2, 1, 0);
GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
cylinder.transform.position = new Vector3(-2, 1, 0);
}
}
扩展布局 (Project 视图脚本 代码的右侧扩展自定义按钮)
● 跟上面描述的方法一样, 先写 C# Script, 代码:
using UnityEngine;
using UnityEditor;
public class Script_03_03
{
[InitializeOnLoadMethod]
static void InitializeOnLoadMethod()
{
EditorApplication.projectWindowItemOnGUI = delegate (string guid, Rect selectionRect)
{
//在Project视图中选择一个资源
if (Selection.activeObject &&
guid == AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Selection.activeObject)))
{
//设置拓展按钮区域
float width = 50f;
selectionRect.x += (selectionRect.width - width);
selectionRect.y += 2f;
selectionRect.width = width;
GUI.color = Color.red;
//点击事件
if (GUI.Button(selectionRect, "click"))
{
Debug.LogFormat("click : {0}", Selection.activeObject.name);//使用 Debug.Log 打印选择的游戏对象 Selection.activeObject.name 即可。
}
GUI.color = Color.white;
}
}; //这里要添加分号
}
}
● 这样就可以在Project 视图 代码的右侧扩展自定义按钮, 在代码中既可以设置按钮的区域,也可以监听按钮的事件。
可以看到图中的一个资源的上面有一个扩展后的按钮, 点击这个按钮, 程序会自动在 Console 窗口中打印选中的资源名。看下图,点击后,Console 打印的信息:
● EditorApplication.projectWindowItemOnGUI :为ProjectWindow中的每个可见列表项委托GUI事件
● Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。
● AssetDatabase.AssetPathToGUID(string path) : 参数path,是资产的文件系统路径。