- Home /
GUI error when using HandleUtility.PickGameObject()
So I have been building a map editor for a game that our class is making. So far the results and power of Unity Editor has been great. The editor is built as an EditorWindow and edits and gets information form the Scene via a delgate added to
SceneView.onSceneGUIDelegate. For selecting objects I was using a ray cast to detect objects within the scene however that means that every object needs to have a collider on it to work. Looking at the HandleUtility again I saw and tried to use PickGameObject() , this I assumed was the way the basic Unity Editor does it, and guess what? Works perfectly, even gets object information from a gizmo which is perfect for a spawn point or the likes that is only a empty game object. Only problem is that I get two errors when this function is being called. I have looked up the errors and use of the function, my searching has not helped. The errors don't crash the engine nor stop the function from working. Also a small black box appears at the tip of the cursor and lags behind when cursor is moved.
Errors
Error 1 - "GUI Window tries to begin rendering while something else has not finished rendering! Either you have a recursive OnGUI rendering, or previous OnGUI did not clean up properly."
Error 2 - "device.IsInsideFrame()"
Code
//Function that is added to SceneView.onSceneGUIDelegate
void SceneGUI()
{
//creates very accurate ray based on raw mouse position.
ray = HandleUtility.GUIPointToWorldRay(mousePos);
//only gets an object if there is an object to look for
if(HandleUtility.RaySnap(ray) != null){
//gets the gameobject the mouse is over in scene view
hitObj = HandleUtility.PickGameObject(mousePos, true);
}
}
An idea about what the problem might be is that the SceneView class is already calling this function and since I am calling it in an added delegate in the SceneView it is causeing somekind of conflict. Of course no idea how to test that. This is a poorly documented feature of the HandleUtility so any help is greatly appreciated!
Since the time of posting this I have needed to do a different approach to my tool so I am not using this method any more. However it would be great to use else where, so if anyone has any ideas about how to get rid of the error or how to use it properly it would still be appreciated.
Running into the exact situation you described. Unfortunately I actually need this to work without error, so solving the problem is going to be a requirement for me. If I figure out what the issue is I'll post the answer here.
Answer by karl_ · Oct 06, 2014 at 12:32 AM
I also ran into trouble when I tried running PickGameObjects
in the OnSceneGUI
loop. Since it works as expected when nested under an if(eventType == EventType.MouseUp)
, I figured it was probably just an issue with it being called between Layout
and Repaint
. I was able to get it working by only checking for GameObjects on MouseMove
type events, since that's really the only time it should change in the SceneView
anyways.
if( Event.current.type == EventType.MouseMove )
go = HandleUtility.PickGameObject(Event.current.mousePosition, false);
That makes sense. While the project is on the back burner and past that issue I will still go back and try it with the SceneView delegate. However it does work with the instpector OnSceneGUI loop. Thanks.