I am having a problem getting the current handle that is selected in my custom inspector for a waypoint system I am creating.
Currently using FreeMoveHandle for the waypoints.
My waypoint system only contains an array of Vector2’s.
I loop through all waypoints creating a handle for each one while drawing a line to the previous handle.
I can add waypoints using the custom inspector that get added to the end of the list of waypoints(they get added were the mouse is positioned).
I can see no way to get the currently selected handle or a position for that handle.
If anyone knows how to do this it would be greatly appreciated.
When working with handles you rarely need to know this (you can get it by giving the handle a name and then using GUI.GetNameOfFocusedControl(), but you usually don’t need to).
The Editor GUI system works in an ‘Immediate’ mode. Its an abstraction that is close to the metal, but can be pretty confusing.
During the repaint event the call to Handles.XXX draws the handle, but during other events (for example mouse move) the call to Handles.XXX returns the updated position.
Despite whats really happening you can effectively think of a Handle.XXX as a call to draw the handle which immediately gives a return value of its updated position.
So:
Vector3 newPos = Handles.PositionHandle(currentPos, Quaternion.identity)
if (newPos != currentPos)
{
// Handle was moved and is therefore active
}
Thanks for the reply. I already understand that about Handles, its how I am updating the position of the waypoint when the handles gets moved. That example will work, but I worry about rounding errors causing the conditional to be true even when the handle hasn’t been moved.
I am using the EditorGUIUtility.hotControl to track the last handle moved/selected.
That makes so it only updates the _currentHandle when the hotControl value != 0, as it will only tell you the control when the mouse is pressed on it.
I also pass in a custom draw function for the handles that adds the controlId to a list.
Good to know. I also found your solution to be much better for my situation, as I need the index of the selected handle that would match with the index of the position in the waypoints. My solution doesn’t allow for me to add a waypoint in the middle of the array. The new handle control id will always be added to the end of the list which throws the system out of sync. So if you haven’t seen any problems with your solution I am going to stick with that.
Thanks again for the reply, made it easier to implement my waypoint system.