Hey guys,
Working on an editor script that will allow my designers to modify NPC Waypoints easily. Right now I want to allow them to change the name of each waypoint for the sake of clarity.
Currently, if you press the GUILayout.Button to toggle the edit for each waypoint on and off, it works fine. I also wanted to give the option to simply press Enter. This “works” but only when the sceneview has been selected. So currently, you have to click the “change” button, type the name you want, select the scene view, then press enter.
Is there a way I can get the enter event to be detected from within the focus of the EditorWindow itself (or more specifically the TextField) without having to select the Scene View each time?
Here is a simplified version of my code with only the applicable code included.
Any suggestions are appreciated! Thanks guys!
void SceneGUI(SceneView sceneView)
{
//If we are changing the name of ANY of the waypoints, this should be true
if (changingName)
{
Debug.Log("Changing");
//Check for Enter Key Press
if (Event.current.keyCode == KeyCode.Return)
{
Debug.Log("hit");
waypoints[nameChangeID].GetComponent<Waypoint>().changingName = false;
SceneView.RepaintAll();
Repaint();
changingName = false;
}
}
}
void OnGUI()
{
//For each waypoint, draw a row in our inspector
for (int i = 0; i < waypoints.Count; i++)
{
GameObject go = waypoints*;*
GUILayout.BeginHorizontal();
//Make sure we don’t print anything that might have turned up null
if (go != null)
{
//Print a Label or TextField based on the changingName state of each instance of a Waypoint
if (go.GetComponent().changingName)
go.GetComponent().name = GUILayout.TextField(go.GetComponent().name, 20);
else
GUILayout.Label(go.GetComponent().name);
//If the “Change Name” button is clicked, toggle changeName bool
if (GUILayout.Button(Resources.LoadAssetAtPath(“Assets/Scripts/AI/Patrolling Editor/Images/pencil.png”), GUILayout.Width(30), GUILayout.Height(30)))
{
Waypoint wps = go.GetComponent();
//If true, set false, confirm name
if (wps.changingName)
{
wps.changingName = false;
changingName = false;
SceneView.RepaintAll();
}
else //If false, set true, set the ID of the item being changed so we can confirm it with the enter button
{
wps.changingName = true;
changingName = true;
nameChangeID = i;
SceneView.RepaintAll();
}
}
}
}
}