1、添加菜单项
Paste_Image.png Paste_Image.pngusing UnityEngine;
using UnityEditor;
public class Tools : MonoBehaviour {
//第一个参数为路径,如不存在就主动创建,第二个参数表示是否为验证方法,第三个参数为优先级,默认优先级为1000,每隔11就会有一条分割线
[MenuItem("Tools/test",false)]
static void Test()
{
print("asd");
}
//快捷键为t
[MenuItem("Tools/test1 _t",false,99)]
static void Test1()
{
print("asd1");
}
//快捷键为ctrl+t
[MenuItem("Tools/test2 %t",false,999)]
static void Test2()
{
print("asd2");
}
//添加到制定默认菜单位置的菜单项还可以显示在右击菜单中,如下可以显示在Hierarchy面板的右击菜单中,快捷键为shift+t
[MenuItem("GameObject/test3 #t", false, 1)]
static void Test3()
{
print("asd2");
}
//快捷键为alt+t
[MenuItem("Assets/test4 &t", false, 1)]
static void Test4()
{
print("asd2");
}
//验证当只有选中物体的时候才能够点击delete按钮
[MenuItem("Tools/delete", true, 989)]
static bool deleteVaildate()
{
if (Selection.objects.Length > 0)
return true;
else
return false;
}
[MenuItem("Tools/delete", false, 989)]
static void Delete() {
foreach (Object o in Selection.objects)
{
Undo.DestroyObjectImmediate(o);//利用Undo进行的删除操作可以撤销。
}
}
}
2、添加指定脚本的右击菜单
Paste_Image.png Paste_Image.pngusing UnityEngine;
using UnityEditor;
public class EditorTest : MonoBehaviour {
[MenuItem("CONTEXT/Character/method1")] //CONTEXT 组件名 按钮名
static void InitSpeeed(MenuCommand cmd)
{
//cmd.context正在操作的组件
Character test = (Character)cmd.context;
test.Speed = 10;
}
[MenuItem("CONTEXT/Transform/method2")] //CONTEXT 组件名 按钮名
static void Inited(MenuCommand cmd)
{
//cmd.context正在操作的组件
Character test = (cmd.context as Transform).GetComponent<Character>();
test.Speed = 0;
}
}
3、创建一个新窗口
图一:
Paste_Image.png
图二:
Paste_Image.png
图三:
Paste_Image.png
窗口脚本:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class CharacterChange :ScriptableWizard {
public int speed = 100;
public const string speedkey = "speed";
[MenuItem("Tools/DisplayCharacterChange", false)]
static void Test()
{
//第一个参数窗口名(必选,其余可选),第二个参数按钮名1(点击后会自动关闭,点击事件函数OnWizardCreate()),第三个参数按钮名字2(点击后不自动关闭,点击事件函数OnWizardOtherButton())
ScriptableWizard.DisplayWizard<CharacterChange>("修改速度","按钮1","按钮2");
}
//窗口启动时运行
private void OnEnable()
{
speed=EditorPrefs.GetInt(speedkey);
}
//检测按钮点击事件
void OnWizardCreate()
{
GameObject[] enemyPrefabs = Selection.gameObjects;
foreach (GameObject go in enemyPrefabs)
{
Character character = go.GetComponent<Character>();
//记录修改前的值以便可以撤销
Undo.RecordObject(character, "change hp");
character.Speed += speed;
}
//可以显示图三的提示信息
ShowNotification(new GUIContent(Selection.gameObjects.Length+"个物体被修改了"));
}
//当前字段值改变或窗口刚打开的时候调用
void OnWizardUpdate()
{
helpString = null;
errorString = null;
if (Selection.gameObjects.Length > 0)
{
//展示图1提示信息
helpString = "当前选择了" + Selection.gameObjects.Length + "物体";
}
else
{
//展示图二错误信息
errorString = "至少选择一个物体";
}
//相对于PlayerPrefs的编辑器模式下持久化储存的方法,记录修改
EditorPrefs.SetInt(speedkey, speed);
}
//EditorWindow中继承的事件
private void OnSelectionChange()
{
OnWizardUpdate();
}
character脚本,挂上后可通过窗口多选,批量修改
using System;
using UnityEngine;
public class Character:MonoBehaviour {
public int Speed;
public int HP;
}
网友评论