- Home /
OnGUI() never being called in ExecuteInEditMode script
I'm trying to do something that I thought would be pretty simple, but is turning out to be very frustrating. I have a system of pathfinding waypoints, and I would like to make it easier to make connections between the nodes with a simple point and click interface. As in, you click the first node, click a button that says "add connection," then click the node you want to connect to, all in the editor.
First I tried to do this with a custom inspector for the waypoint class. This seemed to be working, except that it could never handle the part where you click on a second waypoint, because as soon as you click in the scene the object becomes deselected and the inspector script stops running.
To get around that issue, I made it so the custom editor just has a button which switches the Waypoint instance into "making a new connection" mode. So I had to make the Waypoint an ExecuteInEditMode script, and in OnGUI it looks for mouseclicks using Event.current.isMouse and Event.current.button then casts a from camera.current to the mouse.
However, a new problem has come up that I just can't figure out at all--the OnGUI() method is NEVER being called while in edit mode. It gets called in play mode, and other functions (like Update()) get called in edit mode, but never OnGUI(), under any circumstance. I understand that OnGUI() only fires when it receives an event in edit mode, but I can't figure out any way to get it to fire even once, no matter what I do.
Here's the code for the class, but most of it is kind of irrelevant because the problem is that the very first Debug.Log("OnGUI Firing") in the OnGUI method is never happening, much less any of the rest of the method. What am I doing wrong here?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[ExecuteInEditMode]
public class AIWaypoint : MonoBehaviour {
public List<AIWaypoint> edges;
public bool addEdgeMode = false;
void OnEnable () {
if (edges == null)
{
edges = new List<AIWaypoint>();
}
}
void OnGUI()
{
Debug.Log("OnGUI firing");
if (addEdgeMode)
{
if (Event.current.isMouse && Event.current.button == 0)
{
Ray clickRay = Camera.current.ScreenPointToRay(Event.current.mousePosition);
RaycastHit hit;
if (Physics.Raycast(clickRay, out hit, 100, (1 << 31)))
{
AIWaypoint hitWP = hit.collider.gameObject.GetComponent(typeof(AIWaypoint)) as AIWaypoint;
if (hitWP != null)
{
AddEdge(hitWP);
addEdgeMode = false;
}
}
}
}
}
}
Answer by Bunny83 · May 08, 2013 at 11:43 PM
Are you sure that your GameView is visible in the editor and you actually focused it? ExecuteInEditMode just pretends to be in playmode. There are no constant update intervals, only when something changed. OnGUI is an event processing function (even at runtime). Make sure your GameView is visible. Of course you won't get any events in OnGUI when you're in the SceneView since that doesn't belong to the runtime at all.
You really should go the custom inspector way ;) Here's my GUI crash course. The important part is the section about the Event class and the Use function. Also see OnSceneGUI which mentions the Use function as well.
Also you should set the Tools.viewTool to ViewTool.None so it doesn't interfer with the built-in tools.
Perfect, I was doing a couple of things wrong, and your answer helped me figure out what they were. I switched over to doing it entirely in a custom inspector (way better!), using the OnSceneGUI method, rather than OnGUI.
I tried adding Tool.viewTool = ViewTool.None to my method, but it doesn't seem to actually change the tool in use in the editor. All the same, manually changing to the pan tool fixes my focus-losing problem, so I'm good there, too.
Thanks again for the help.