The manual and script reference don’t give specific enough instructions for me to get one to run in a game.
Script reference says “Editor windows are typically opened using a menu item.” But I don’t know what that means. I don’t see how to create a “menu item” or add one as a component. The sample script has the line
// Add menu named "My Window" to the Window menu
[MenuItem ("Window/My Window")]
but the script itself (see below) can’t be added to a game object because it generates the error “Can’t add script behavior MyWindow because it is an editor script. To attach a script it needs to be outside the ‘Editor’ folder.” But moving the script outside the editor folder and trying to put it on an object generates error “Can’t add script behavior MyWindow. The script needs to derive from MonoBehaviour!”
The sample script:
using UnityEngine;
using UnityEditor;
public class MyWindow : EditorWindow
{
string myString = "Hello World";
bool groupEnabled;
bool myBool = true;
float myFloat = 1.23f;
// Add menu named "My Window" to the Window menu
[MenuItem ("Window/My Window")]
static void Init () {
// Get existing open window or if none, make a new one:
MyWindow window = (MyWindow)EditorWindow.GetWindow (typeof (MyWindow));
}
void OnGUI ()
{
GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
myString = EditorGUILayout.TextField ("Text Field", myString);
groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", groupEnabled);
myBool = EditorGUILayout.Toggle ("Toggle", myBool);
myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);
EditorGUILayout.EndToggleGroup ();
}
}
Trying to trigger the static function from a different script like this one below generates the error “error CS0103: The name `MyWindow’ does not exist in the current context”
using UnityEngine;
using UnityEditor;
using System.Collections;
public class EditorGoScript : MonoBehaviour {
// Add menu named "My Window" to the Window menu
[MenuItem ("Window/My Window")]
// Use this for initialization
void Start () {
MyWindow.Init();
}
// Update is called once per frame
void Update () {
}
}
Obviously I’m missing something here, something that will be obvious and simple.
Thank you for your help!