![](/img/trans.png)
[英]How can I add a context menu option to a gameobject in the hierarchy to be at the bottom?
[英]How can I add a option to a GameObject in the Hierarchy context menu?
using UnityEditor;
using UnityEngine;
public class Test : EditorWindow
{
[MenuItem("GameObject/Test")]
static void Tests()
{
int width = 340;
int height = 300;
int x = (Screen.currentResolution.width - width) / 2;
int y = (Screen.currentResolution.height - height) / 2;
GetWindow<Test>().position = new Rect(x, y, width, height);
}
}
这将在GameObject下面的编辑器菜单中创建Test选项。 但我想在层次结构中的单个或多个GameObject / s上添加选项/属性而不是编辑器顶部菜单。
这是我试过的:
using UnityEditor;
using UnityEngine;
public class ExportObjects : EditorWindow
{
[MenuItem("GaemObject/Export", true, 1)]
static void Export()
{
int width = 340;
int height = 300;
int x = (Screen.currentResolution.width - width) / 2;
int y = (Screen.currentResolution.height - height) / 2;
GetWindow<ExportObjects>().position = new Rect(x, y, width, height);
}
}
但它没有做任何事情,它没有向层次结构中的对象上的右键单击鼠标上下文菜单添加任何内容。
如果我换行:
[MenuItem("GaemObject/Export", true, 1)]
至:
[MenuItem("GaemObject/Export")]
它将在编辑器顶部和Export中添加一个新的GameObject菜单。 但是我想在对象层次结构中的对象上单击鼠标右键时添加此项。 单个对象或所选对象。
试过真,1和真,-10或真,10
看看这里https://docs.unity3d.com/Manual/class-PresetManager.html
您可以使用它来更改添加到通用对象的对象的默认组件,或者为新类型的资产或对象创建预设
您将需要创建一个简单的脚本组件
看这篇文章取决于更多参数。 它将使用priority
参数出现在层次结构上下文菜单中,例如-10
[MenuItem("GameObject/Test", false, -10)]
没有选项可以控制显示或不显示的对象。
但您可以通过添加验证方法来启用和禁用该按钮。 例如,仅在所选对象具有Camera
组件时才启用该方法
// true turns it into a validation method
[MenuItem("GameObject/Test", true, -10)]
private static bool IsCanera()
{
return Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<Camera>();
}
以相同的方式,但使用[ContextMenu]
您可以将其添加到检查器中的组件
[ContextMenu("Example")]
private void DoSomething()
{
// Do something
}
您还可以使用[ContextMenuItem]
将方法直接添加到检查器中仅一个字段的上下文菜单中
[ContextMenuItem("reset this", "ResetExample")]
public int example;
private void ResetExample ()
{
example = 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.