扩展Hierarchy之后的效果:
我们调试战斗的时候,需要在Hierarchy界面选中对应的武将,然后操作快捷键控制武将。由于多个操作我觉得比较麻烦,就在网上查找了一下扩展Hierarchy菜单的方法。
代码如下:
[InitializeOnLoadMethod]
private static void StartInitializeOnLoadMethod()
{
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}
private static void OnHierarchyGUI(int instanceID, Rect selectionRect)
{
if (!Application.isPlaying)
return;
var data = Selection.activeGameObject;
if (data == null)
{
return;
}
if (Event.current != null
&& selectionRect.Contains(Event.current.mousePosition)
&& Event.current.button == 1
&& Event.current.type <= EventType.MouseUp
&& CheckShow())
{
Vector2 mousePosition = Event.current.mousePosition;
EditorUtility.DisplayPopupMenu(new Rect(mousePosition.x, mousePosition.y, 0, 0), "Tools/战斗脚本控制", null);
Event.current.Use();
}
}
[MenuItem("Tools/战斗脚本控制/控制该武将")]
public static void SelectGameObject1()
{
if (Application.isPlaying)
{
var data = Selection.activeGameObject;
if (data != null)
{
var roleControll = data.GetComponent<RoleController>();
if (roleControll != null)
{
roleControll.PlayerControl = !roleControll.PlayerControl;
}
}
}
}
private static bool CheckShow()
{
var data = Selection.activeGameObject;
if (data != null)
{
var roleControll = data.GetComponent<RoleController>();
if (roleControll != null)
{
return true;
}
}
return false;
}
注意一定要添加自己的条件,不然会把Unity原生的菜单覆盖掉。
参考:
Unity编辑器Hierarchy扩展中文教程——Chinar图文详解_ChinarCSDN的博客-CSDN博客
|