- Home /
Editor Menu Item open Window
I have a c# script which creates a menu item in the editor. How would I make it so that when clicked it opens a window with a textbox and OK button, then uses the text boxes text in the script?
Answer by Joshua · Jun 25, 2011 at 01:45 PM
It's all explained here. This script should serve as an example of how to use a textbox and a button. A textbox will appear when you select something in the hierarchy, you can then change the name and press ok to apply the changes.
class MyWindow extends EditorWindow
{
@MenuItem ( "Window/My Window" )
static function ShowWindow ()
{
EditorWindow.GetWindow ( MyWindow );
}
var newName : String;
function OnGUI ()
{
GUILayout.Label( "Select an object in the hierarchy view" );
if( Selection.activeGameObject )
{
newName = EditorGUILayout.TextField( "Object Name: ", newName );
if( newName == "" )
{
newName = Selection.activeGameObject.name;
}
if( GUILayout.Button( "Ok" ) )
{
Selection.activeGameObject.name = newName;
}
}
else
{
newName = "";
}
this.Repaint();
}
}
:D it's funny, you check if there's an activeGameObject but when you press the O$$anonymous$$ button you just set the name even if there's no GameObject selected.
Also it's kinda useless to call this.Repaint();
every GUI event. It just marks the window to get repainted but usually that happens automatically when it's needed. You only need a constant repaint if you want to display something animated.
Hah, you're right, I'll fix that, Bunny. I did the repaint so it would instantly show when you (de)select something from the hierarchy ins$$anonymous$$d of only when the window regains focus. I guess it would've been better to check if something got (de)selected and only then repaint, but I didn't bother :p
There's a nice event for that case ;)
Answer by Theinsanekiller · Jan 22, 2015 at 06:24 AM
I need a help. I dont want my window to open via Menu. I want it to open on some event or a editor button click. Is it possible to do? In short I dont want my window to become a menu Item.