- Home /
How to create a control that allows to select position within a line?
Hey everybody. I'm new to editor scripting and it looks like it's not well documented. At least I can't find any information about how to do this.
What I want to do is to create a control that looks like this: https://s16.postimg.org/kmzeylrrp/GIF.gif
This is how it's implemented when you edit a 2D polygon collider but I want to have a control like that for my own shape editor thingy. And I don't really know if it's possible.
What I want is ability to draw a line and indicate a point on it on mouse move, and if you click that point it passes the coordinates to the script so I can create a new point and draw more lines and so on. Is there a control like that? Is it possible to do at all?
Answer by gameplay4all · Mar 21, 2017 at 03:44 PM
Well, if you'd like it to be like the 2D polygon collider, why not use that?
It has some variables which will be useful for this like points
and functions like GetPath
. So you could have an object with a 2D polygon collider and when you edit your custom class, you pass this object the polygon data it needs, edit the data on the 2D Polygon Collider, and when you're done you retrieve the points variable plus other things you need and use it to form the -now edited- custom class that uses these polygons.
It's a bit of a hack, if you're doing it a lot, a custom editor will be the better option. And you've already tagged the right class for this: Handles (gizmos is more for in scene view, which could be used for 3D polygons).
Hope this puts you in the right direction,
-Gameplay4all
I thought about using this hack but it doesn't really work for me. I want to create a vertex editor that allows to create and edit simple polygons that may or may not have a collider attached. And shapes of collider and mesh could differ.
It seems that I'll need to create this tool by tracking a mouse position and drawing a line with a point manually.
To illustrate it better, here's my progress with vertex editor.
Huh. I've just accidentally found a script that does just that. Uses Polygon Collider 2D to build the mesh. It's very basic: a dozen lines of code + common triangulator. I'll leave it here just in case.
Answer by FortisVenaliter · Mar 21, 2017 at 10:08 PM
What you want to do is implement an EditorWindow. From there, you can use the editor graphics calls to generate the lines and nodes. You will most likely have to write all the logic for creation/dragging/etc. yourself, but that will provide a good venue to do so. See here for how to create the Window, and here for how to draw the components.
Answer by Andrii · Mar 22, 2017 at 06:14 PM
I've figured out how to do this, but maybe I did it the hard way.
I'm tracking the position of a mouse using custom editor script, calculating where the point should be by comparing bunch of distances and angles and then drawing Handle.Button() at that location. I hope it's the best way because I've spent too much time writing complicated math I don't understand.
That looks very useful and clean, I'm impressed :) Well done!
Your answer
Follow this Question
Related Questions
Showing Handles in the Game View 1 Answer
Handles.PositionHandle not working anymore in version 2019.3? 0 Answers
Using handles to edit custom classes 2 Answers
Draw Gizmos and Handles in the scene from an EditorWindow 1 Answer
Gizmos.DrawLine is dissapearing after returning to editor after Playing the scene 0 Answers