// itemName是表示为路径名的菜单项
// 例如 "GameObject/Do Something".
// isValidateFunction 控制方法是否激活。 默认为false。
// 为true时菜单栏不显示本选项或者本选项置灰不可以被点击
// priority 控制菜单的显示上下的顺序。值越小越在上面显示
== 注意 ==
public class UIUtils
{
// 添加到快捷栏
[MenuItem("Test/item0", false)]
static void TestFunc0()
{
}
[MenuItem("Test/item1", true)]
static void TestFunc1()
{
}
// 添加到Assets目录下
[MenuItem("Assets/图片修改为Sprite", priority = 1)]
public static void changeTextureType()
{
Object[] m_objects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
for (int i = 0; i < m_objects.Length; i++)
{
string path_tex = AssetDatabase.GetAssetPath(m_objects[i]);
//Debug.Log("--" + m_objects[i].GetType());
if (m_objects[i].GetType() == typeof(Texture2D))
{
TextureImporter texture = AssetImporter.GetAtPath(path_tex) as TextureImporter;
// 修改图片格式为Sprite
texture.textureType = TextureImporterType.Sprite;
texture.spritePixelsPerUnit = 1;
texture.filterMode = FilterMode.Trilinear;
texture.mipmapEnabled = false;
AssetDatabase.ImportAsset(path_tex);
}
}
// 提示框
EditorUtility.DisplayDialog("图片工具", "图片格式转换完毕!", "确认");
}
[MenuItem("GameObject/UIUtils/BtnComp", false, priority = 10)]
static void creatBtnComp(MenuCommand menuCommand)
{
// Load Prefabs
// AssetDatabase.LoadAssetAtPath("UIPrefabs/ImageButton");
GameObject go = new GameObject();
go.name = "BtnComp";
// menuCommand.context是当前鼠标左键选中的GameObjet游戏物体
// 设置为当前选中物体的子物体
GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
// 注册到U3D的Undo系统中,方便撤销操作
Undo.RegisterCreatedObjectUndo(go, "Creat " + go.name);
go.AddComponent<Image>();
go.GetComponent<RectTransform>().sizeDelta = new Vector2(250, 100);
go.AddComponent<BtnComp>();
// 设置选中对象为新创建的go
Selection.activeObject = go;
}
通过 == MenuItem(“COMTEXT/组件名/右键菜单名称”) ==添加
[MenuItem("CONTEXT/BtnComp/SetScale")]
public static void ReName(MenuCommand command)
{
BtnComp btn = (BtnComp)command.context as BtnComp;
btn.scaleBtn = !btn.scaleBtn;
}
[MenuItem("Test/item2 %L")]
static void TestFunc2()
{
}
// [MenuItem("菜单名/右键选项名称 快捷键组合")]
// [MenuItem("Test/item2 %L")] 快捷键为 Ctrl + L
% + Key
在Win下
Ctrl + Key
在macOS下
Cmd + Key
_ + Key
设置单一按键为快捷键
# + Key
Shift + Key
# LEFT
shift + left
其他单一按键为:LEFT, RIGHT, UP, DOWN, F1 ~ F12, HOME, END, PGUP, PGDN等
& + Key
Alt + Key