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. But I want to add option/property on a single or more GameObject/s in the Hierarchy not the editor top menu.
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. 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
Have a look here 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
[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
// 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("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("reset this", "ResetExample")]
public int example;
private void ResetExample ()
{
example = 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.