[英]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);
}
}
This will create the Test option in the editor menu on the top under GameObject. 这将在GameObject下面的编辑器菜单中创建Test选项。 But I want to add option/property on a single or more GameObject/s in the Hierarchy not the editor top menu.
但我想在层次结构中的单个或多个GameObject / s上添加选项/属性而不是编辑器顶部菜单。
This is what I tried: 这是我试过的:
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);
}
}
But it does nothing it didn't add anything to the right click mouse context menu on the objects in the hierarchy. 但它没有做任何事情,它没有向层次结构中的对象上的右键单击鼠标上下文菜单添加任何内容。
If I change the line: 如果我换行:
[MenuItem("GaemObject/Export", true, 1)]
To: 至:
[MenuItem("GaemObject/Export")]
It will add a new GameObject menu at the top of the editor and the Export. 它将在编辑器顶部和Export中添加一个新的GameObject菜单。 But I want to add this when doing right click mouse on a object in the hierarchy.
但是我想在对象层次结构中的对象上单击鼠标右键时添加此项。 Single object or for selected objects.
单个对象或所选对象。
Tried true, 1 and true, -10 or true, 10 试过真,1和真,-10或真,10
Have a look here https://docs.unity3d.com/Manual/class-PresetManager.html 看看这里https://docs.unity3d.com/Manual/class-PresetManager.html
You can use this to change the default components added to an object for generic objects or create a preset for a new type of asset or object 您可以使用它来更改添加到通用对象的对象的默认组件,或者为新类型的资产或对象创建预设
You will need to create the component which would simply be a script 您将需要创建一个简单的脚本组件
See this post it depends on more parameters. 看这篇文章取决于更多参数。 it will appear in the hierarchy context menu using the
priority
parameter eg with -10
它将使用
priority
参数出现在层次结构上下文菜单中,例如-10
[MenuItem("GameObject/Test", false, -10)]
There is no option to control for which objects this shall be displayed or not. 没有选项可以控制显示或不显示的对象。
But you can enable and disable the button by adding a validation method. 但您可以通过添加验证方法来启用和禁用该按钮。 Eg only enable the method if the selected object has a
Camera
component 例如,仅在所选对象具有
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>();
}
The same way but using [ContextMenu]
instead you can add it to the component in the Inspector 以相同的方式,但使用
[ContextMenu]
您可以将其添加到检查器中的组件
[ContextMenu("Example")]
private void DoSomething()
{
// Do something
}
You can also add a method directly to the context menu of only one field in the Inspector using [ContextMenuItem]
您还可以使用
[ContextMenuItem]
将方法直接添加到检查器中仅一个字段的上下文菜单中
[ContextMenuItem("reset this", "ResetExample")]
public int example;
private void ResetExample ()
{
example = 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.