I am currently learning how to write scripts in Unity and decided to try my hand at writing a path editor for an upcoming game I am planning. I have a path class which holds the actual path data and a custom editor for the path. Now I was trying to use the SceneView to do most of my editing work, that is adding path nodes and whatnot. I can successfully display the path nodes using PositionHandles, or FreeMoveHandles. I have been able to add nodes by clicking in the SceneView. Here is where I am getting tripped up.
If I attach my path script to an empty GameObject, when I click the scene the SceneView will add the node, then select the object I clicked. I thought that by calling the Event.current.use (), the mouse click event would essentially not be processed anymore. This doesn’t seem to be the case. I have scoured the reference docs and searched the forums to come up with a good solution and have done some tests using keyboard commands which may work, but is not optimal. Is there something I am missing being a complete noob to Unity? Or am I going about the problem wrong?
Here is my code for those interested:
public void OnSceneGUI ()
{
if (Event.current.type == EventType.MouseDown)
{
if (Event.current.button == 0)
{
Ray worldRay = HandleUtility.GUIPointToWorldRay (Event.current.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast (worldRay, out hitInfo))
{
Undo.RegisterUndo (target, "Add Path Node");
((Path)target).AddNode (hitInfo.point);
}
Event.current.Use ();
}
}
for (int i = 0; i < ((Path)target).nodes.Count; i++)
((Path)target).nodes[i] = Handles.PositionHandle(((Path)target).nodes[i], Quaternion.identity);
Handles.DrawPolyLine (((Path)target).nodes.ToArray ());
if (GUI.changed)
EditorUtility.SetDirty(target);
}
Again any help or suggestions would be greatly appreciated. Thanks.
I setup a boolean variable to tell the function if the add node tool is active. However, this still presents the problem of adding the node, then the mouse click gets used again to select or deselect the object by the Scene View.
Taking another look at the docs reveals that calling Event.current.Use () in my OnSceneGUI function should make it so the Scene View doesn’t process the event. This is not what is happening.
I am using Unity 2.6 on a windows machine and the script is C#. Has anyone else had the same problem or am I misunderstanding the whole situation? Any help is greatly appreciated. Thanks.
After searching long and hard to no avail, I finally found what my problem was. I wasn’t getting a controlID for my new control and setting it as the default. This was something that I had overlooked being so new to Unity. By doing so, the proper behavior that I was trying to achieve was obtained. All it took was some reflection. Below is my new code for anyone that is experiencing the same issue.
void OnSceneGUI()
{
Event current = Event.current;
int controlID = GUIUtility.GetControlID(mPathEditorHash, FocusType.Passive);
switch (current.type)
{
case EventType.mouseUp:
switch (mActiveTool)
{
case Tools.toolAdd:
addNode();
break;
}
current.Use();
break;
case EventType.layout:
HandleUtility.AddDefaultControl(controlID);
break;
}
for (int i = 0; i < ((Path)target).nodes.Count; i++)
((Path)target).nodes[i] =
Handles.FreeMoveHandle(((Path)target).nodes[i],
Quaternion.identity, 0.25f, new Vector3(1.0f, 1.0f, 1.0f),
Handles.DrawRectangle);
Handles.DrawPolyLine(((Path)target).nodes.ToArray());
if (GUI.changed)
EditorUtility.SetDirty(target);
}
Really though, this post is 3 years old and it seems that people still run into this problem. Perhaps I’ll bring it up with some people on the IRC channel tonight to if anything, add some note to the documentation.
At least now my tool now allows me to populate waypoints quickly and the editor doesn’t try to use my mouse clicks to select objects in the scene.
For all the years I’m wondering about those strange control IDs and what they’re good for, until I need to solve the Event.Use() problem and found this thread. You made my day! Thanks a lot!
Thanks a lot. I was wondering so long how to get Left Mouse Click Up Event !
But there is a big issue with this ‘solution’ : It disables Handles and GameObjectSelection in Scene ! Given your script receive the event mouseUp, the Unity-based script for those doesn’t get it anymore, and so, you can no more move or deselect your object by clicking in Scene…