[MenuItem("路径/方法名 快捷键符号",{bool:是否可用},{int:层级})]
参数1: 显示位置, string类型, 在菜单栏显示方法,
快捷键符号:
@ Shift # 用于组合键
@ Ctrl % 用于组合键
@ Alt & 用于组合键
@ 英文字母 _字母 单独使用需要加上'_',也可以与上面组合使用
参数2: 是否显示, bool类型, 可选参数, 默认为false, 按钮可用, true不可用
参数3: 优先层级, int类型, 可选参数, 从0开始, 越小位置越靠上
using UnityEditor;
using UnityEngine;
public class UnityToolsEditor
{
[MenuItem("Tools/测试方法1 _a")]
public static void Test1()
{
Debug.Log("Tools/测试方法1");
}
[MenuItem("Tools/测试方法2 _B", false)]
public static void Test2()
{
Debug.Log("Tools/测试方法2");
}
[MenuItem("Tools/测试方法3 &A", false, 4)]
public static void Test3()
{
Debug.Log("Tools/测试方法3");
}
[MenuItem("Tools/测试方法4 %a", false, 3)]
public static void Test4()
{
//说明:测试方法4会在测试方法3的上面
Debug.Log("Tools/测试方法4");
}
// 设置可用状态, 执行后不可用, 测试方法5和测试方法6设置
private static int _CheckedTest5_6 = 5;
[MenuItem("Tools/测试方法5 #A")]
public static void Test5()
{
//执行后该方法不可用
_CheckedTest5_6 = 6;
Debug.Log("Tools/测试方法5");
}
[MenuItem("Tools/测试方法5", validate = true)]
public static bool Test5Validate()
{
//说明:检测并设置Test5是否可用,需配合Test5使用,单独不显示
return _CheckedTest5_6 == 5;
}
[MenuItem("Tools/测试方法6")]
public static void Test6()
{
//执行后该方法不可用
_CheckedTest5_6 = 5;
Debug.Log("Tools/测试方法6");
}
[MenuItem("Tools/测试方法6", validate = true)]
public static bool Test6Validate()
{
//说明:检测并设置Test6是否可用,需配合Test6使用,单独不显示
return _CheckedTest5_6 == 6;
}
// 设置选中状态, 即每次打开都会选中该项, 测试方法7和测试方法8交替选中
private static int _CheckedTest7_8 = -1;
[MenuItem("Tools/测试方法7")]
public static void Test7()
{
_CheckedTest7_8 = 7;
SetMenuSelected();
}
[MenuItem("Tools/测试方法8")]
public static void Test8()
{
_CheckedTest7_8 = 8;
SetMenuSelected();
}
private static void SetMenuSelected()
{
// Unity提供的API,可设置菜单的勾选状态
Menu.SetChecked("Tools/测试方法7", _CheckedTest7_8 == 7);
Menu.SetChecked("Tools/测试方法8", _CheckedTest7_8 == 8);
}
}
//[续]
public class UnityToolsEditor
{
[MenuItem("Tools/OpenCS")]
public static void OpenCS()
{
var paths = UnityEditor.AssetDatabase.FindAssets("文件名");
if (paths.Length <= 0)
return;
var assetPath = AssetDatabase.GUIDToAssetPath(paths[0]);
var obj = AssetImporter.GetAtPath(assetPath);
AssetDatabase.OpenAsset(obj);
}
}
//需要添加说明的类
public class UIBuilder
{
}
//Editor目录中
[CustomEditor(typeof("UIBuilder"))]
class UIBuilderCustomEditor: Editor
{
public override void OnInspectorGUI()
{
//实现具体的逻辑.
//可参考:https://blog.csdn.net/m0_37920739/article/details/104659247
}
}
//[续]
public class UnityToolsEditor
{
//必须是静态方法
[UnityEditor.Callbacks.DidReloadScripts]
public static void ReloadScriptsCallback(){
}
}
public class WillSaveAssetsEditor : UnityEditor.AssetModificationProcessor
{
//资源保存时会调用此函数,必须是静态函数
public static void OnWillSaveAssets(string[] names)
{
}
}
//[续]
public class UnityToolsEditor
{
//必须是静态方法
[UnityEditor.InitializeOnEnterPlayMode]
public static void OnEnterPlayModeCallBack(){
}
}
public class TestObject : MonoBehaviour
{
//修饰方法,属于类级别,选择该标签执行OnRandomData方法
[ContextMenu("Random Data")]
public void OnRandomData(){
Debug.Log("OnTestMenu 被执行");
OnRandomAge();
OnRandomName();
}
//修饰属性,选择该标签执行OnRandomAge方法
[ContextMenuItem("Random Age", "OnRandomAge")]
public int Age;
void OnRandomAge()
{
Age = new System.Random(DateTime.Now.Millisecond).Next(1, 100);
}
//修饰属性,选择该标签执行OnRandomAge方法
[ContextMenuItem("Random Name", "OnRandomName")]
public string Name;
private void OnRandomName()
{
string[] names = new string[] { "Jack", "Jim", "Tomas", "Han", "Ann" };
Name = names[new System.Random(DateTime.Now.Millisecond).Next(0, names.Lenth + 1)];
}
}
//[续]
public class UnityToolsEditor
{
//加载时调用的方法
[InitializeOnLoadMethod]
public static void InitializeOnLoad()
{
SceneView.duringSceneGui += (SceneView) =>
{
// 鼠标点击抬起,鼠标左右键都会响应到
if(Event.current != null &&
Event.current.type == EventType.MouseUp)
{
Debug.Log("鼠标点击事件");
}
};
}
}
通过继承ScriptableWizard可以创建编辑器向导,Unity已经为我们封装好了一些变量、方法、消息。如:
//创建一个Cube提示
using UnityEditor;
using UnityEngine;
#region CreateHelper
public class CreateHelper : ScriptableWizard
{
private static PrimitiveType _nType = PrimitiveType.Sphere;
//声明为public可以被向导序列化显示在界面上,且可以改变其值
public float size = 1f;
public string name = "_N_U_L_L";
//创建时的回调
void OnWizardCreate()
{
GameObject obj = GameObject.CreatePrimitive(_nType);
obj.transform.localScale = new Vector3(size, size, size);
obj.name = name;
}
// 创建结束
void OnWizardOtherButton()
{
//点击otherButton,需要调用Close()方法,才关闭向导。
//但是点击createButton,并无需调用Close()方法而自动关闭。
Close();
}
//输入过程提示
void OnWizardUpdate()
{
if (name.Equals("_N_U_L_L"))
name = _nType.ToString();
switch (_nType)
{
case PrimitiveType.Sphere:
OnSphereHelper();
break;
case PrimitiveType.Capsule:
OnCapsuleHelper();
break;
case PrimitiveType.Cylinder:
OnCylinderHelper();
break;
case PrimitiveType.Cube:
OnCubeHelper();
break;
case PrimitiveType.Plane:
OnPlaneHelper();
break;
case PrimitiveType.Quad:
OnQuadHelper();
break;
}
}
#region Sphere
[MenuItem("GameObject/3D Object/ASphere")]
static void CreateSphere()
{
_nType = PrimitiveType.Sphere;
ScriptableWizard.DisplayWizard<CreateHelper>("创建Sphere", "确定", "取消");
}
private void OnSphereHelper()
{
helpString = "创建Sphere,并修改默认名字防止重复";
errorString = "";
isValid = true;
if (size >= 10)
{
errorString = "size要小于10";
isValid = false;
}
else if (size <= 0)
{
errorString = "size要大于0";
isValid = false;
}
if (string.IsNullOrEmpty(name))
{
errorString = "名字不能为空";
isValid = false;
}
else if (name.Equals("Sphere"))
{
errorString = "修改默认名字";
isValid = false;
}
}
#endregion Sphere
#region Capsule
[MenuItem("GameObject/3D Object/ACapsule")]
static void CreateCapsule()
{
_nType = PrimitiveType.Capsule;
ScriptableWizard.DisplayWizard<CreateHelper>("创建Capsule", "确定", "取消");
}
private void OnCapsuleHelper()
{
helpString = "创建Capsule,并修改默认名字防止重复";
errorString = "";
isValid = true;
if (size >= 10)
{
errorString = "size要小于10";
isValid = false;
}
else if (size <= 0)
{
errorString = "size要大于0";
isValid = false;
}
if (string.IsNullOrEmpty(name))
{
errorString = "名字不能为空";
isValid = false;
}
else if (name.Equals("Capsule"))
{
errorString = "修改默认名字";
isValid = false;
}
}
#endregion Capsule
#region Cylinder
[MenuItem("GameObject/3D Object/ACylinder")]
static void CreateCylinder()
{
_nType = PrimitiveType.Cylinder;
ScriptableWizard.DisplayWizard<CreateHelper>("创建Cylinder", "确定", "取消");
}
private void OnCylinderHelper()
{
helpString = "创建Cylinder,并修改默认名字防止重复";
errorString = "";
isValid = true;
if (size >= 10)
{
errorString = "size要小于10";
isValid = false;
}
else if (size <= 0)
{
errorString = "size要大于0";
isValid = false;
}
if (string.IsNullOrEmpty(name))
{
errorString = "名字不能为空";
isValid = false;
}
else if (name.Equals("Cylinder"))
{
errorString = "修改默认名字";
isValid = false;
}
}
#endregion Cylinder
#region Cube
[MenuItem("GameObject/3D Object/ACube")]
static void CreateCube()
{
_nType = PrimitiveType.Cube;
ScriptableWizard.DisplayWizard<CreateHelper>("创建Cube", "确定", "取消");
}
private void OnCubeHelper()
{
helpString = "创建Cube,并修改默认名字防止重复";
errorString = "";
isValid = true;
if (size >= 10)
{
errorString = "size要小于10";
isValid = false;
}
else if (size <= 0)
{
errorString = "size要大于0";
isValid = false;
}
if (string.IsNullOrEmpty(name))
{
errorString = "名字不能为空";
isValid = false;
}
else if (name.Equals("Cube"))
{
errorString = "修改默认名字";
isValid = false;
}
}
#endregion Cube
#region Plane
[MenuItem("GameObject/3D Object/APlane")]
static void CreatePlane()
{
_nType = PrimitiveType.Plane;
ScriptableWizard.DisplayWizard<CreateHelper>("创建Plane", "确定", "取消");
}
private void OnPlaneHelper()
{
helpString = "创建Plane,并修改默认名字防止重复";
errorString = "";
isValid = true;
if (size >= 10)
{
errorString = "size要小于10";
isValid = false;
}
else if (size <= 0)
{
errorString = "size要大于0";
isValid = false;
}
if (string.IsNullOrEmpty(name))
{
errorString = "名字不能为空";
isValid = false;
}
else if (name.Equals("Plane"))
{
errorString = "修改默认名字";
isValid = false;
}
}
#endregion Plane
#region Quad
[MenuItem("GameObject/3D Object/AQuad")]
static void CreateQuad()
{
_nType = PrimitiveType.Quad;
ScriptableWizard.DisplayWizard<CreateHelper>("创建Quad", "确定", "取消");
}
private void OnQuadHelper()
{
helpString = "创建Quad,并修改默认名字防止重复";
errorString = "";
isValid = true;
if (size >= 10)
{
errorString = "size要小于10";
isValid = false;
}
else if (size <= 0)
{
errorString = "size要大于0";
isValid = false;
}
if (string.IsNullOrEmpty(name))
{
errorString = "名字不能为空";
isValid = false;
}
else if (name.Equals("Quad"))
{
errorString = "修改默认名字";
isValid = false;
}
}
#endregion Quad
}
#endregion CreateHelper