This is a follow-up question from How to make individual anchor points of bezier continuous or non-continuous. Please refer to it for the relevant code in the accepted answer (please note that I did this to keep this question clean since the related code is quite lengthy).
I am trying to achieve the following:
Make the bezier curve handles/control points selectable in such a way that the properties (for example continuity) for an individual handle are displayed in the inspector window when selected. Please note I'd like this to be done without making creating game objects for the handles/ control points
Retain a single method that handles the movement of each point instead of having separate methods for the movement of each point.
Not entirely sure if I understood the question, but
You need to use OnSceneGUI option to be able to select the handles, and everytime you select a new point just store its value.
and for showing the point on inspector GUI
and call this on the InspectorGUI
Am I late to the party?
I like @jour's solution in general, except 1 thing: with
Handles.Button
you have to click a point to select it and then you click and drag to move the control point.I propose a different approach. Using the same
Handles.FreeMoveHandle
, but having a variable to remember the id of the last clicked handle, so I can identify it.Usually, a built in Handle won't give you more info than what it is designed to do.
FreeMoveHandle
, for example, returns the delta of its translation and that's all. The problem is: You want to capture a simple click, but if you just clicked and didn't drag, the return value isVector3.zero
and it is just the same as if you didn't do a click at all.Good news: among the overloads of any Handle, there are some calls with an argument named
controlID
- it is an identifier for each interactable GUI object. If you supress it, the Engine will choose one and you will never know what. But if you pass an int, that value will be the id of the handle. But if I pass an int and it happen to conflict with any of the other ids i don't see? Well, you can callGUIUtility.GetControlID
to get a safe id.Then, it is straightforward. If the id of a Handle is the same as
EditorGUIUtility.hotControl
(that is, the control that got clicked or have the keyboard focus), then I save the index of this point inselectedControlPointId
and use it to display a custom property in Inspector.Hmm... Here become the controversy. If I understood it correctly, you want a single code to draw both nodes and tangents. The thing is: this two things are different in nature. Of course, if you keep it plain and simple, they are bot movable points in scene. But, when you introduces things like constraints (the continuity, or
smooth
) and selection, they become different beasts, with different logic. Even if you want to makeControlPoint
astruct
(like i did now) and pass it over as a whole, you'd still need to point what component you are aiming to update, so the constraints will apply to the others - you will always need a "master" field to avoid circular updates (you changetangentBack
, and that makestangentFront
to update, that triggertangentBack
to update again and so on).That's why, even though I reorganized somehow the
ControlPoint
methods and made it astruct
, I can't make a single logic to draw both nodes and tangents.Here are some codes. I started over from the codes on my answer in the previous question.
ControlPoint.cs
I removed
ControlPointDrawer
, so the other propertis you added to it won't be hidden in inspector.Path.cs
PathCreator.cs is the same
PathCreatorEditor.cs
Note: I moved the drawing of the Bezier line from
OnSceneGui
toDrawGizmo
, so the green line will be visible even when the object is not delected, and it will be pickable in the Scene Editor, for having it selected.Lastly, I would suggest some further development of this scripts. It wouldn't be very hard to make multiple point selection posible. Maybe making the default handles (like position and rotation) to be appliable individually on points. Or changing the method for creating and deleting points to something move intuitive like double-clicking or dragging the path line. Or even a custom toolbar to smart point manipulation, like align, distribute, sculpt... Different constraints, like smooth, symetric, cusp or straight...