- Home /
Adding drag function to a polygon editor
Hi, Im writing a onscenegui polygon editor, trying to mimic the polygoncollider2D editor. I am using a FreeMoveHandle for each vertex (green) and there is a dotcap handle (shown in cyan) to show where new vertices would be placed
A new vertex is placed when the cyan dotcap handle is visible and the mouse button is clicked. This means i have to click to place, then click to drag the FreeMoveHandle of the new vertex.
This is pretty cumbersome and I really would like to just click and drag. Does anyone know how i could achieve this?
The code to add a new vertex is as follows;
Handles.color = Color.cyan;
Vector3 closestPoint = HandleUtility.ClosestPointToPolyLine(polygonArray);
Handles.DotCap(0,closestPoint,Quaternion.identity,handleSize);
if(e.type == EventType.mouseDown && e.button == 0)
{
AddPoint(closestPoint);
}
Handles.color = Color.green;
and
private void AddPoint(Vector3 pointToAdd)
{
m_PointCount.intValue++;
int[] polygonEdge = FindEdgeByPoint(pointToAdd);
for(int i = m_PointCount.intValue-2 ; i >= polygonEdge[1] ;i--)
{
SetPointArray(i+1, GetPointAtIndex(i));
}
SetPointArray(polygonEdge[1], pointToAdd);
}
Answer by KMKxJOEY1 · Oct 07, 2014 at 07:19 PM
without posting code it is hard to really tell what is going on but from what I understand this could be a solution:
When you check the mouse button the click for placing a new vtx, make it check MouseButtonDown and then create the vtx. Flag that vtx as the active editing vtx that same frame, and the update method where you are handling dragging, it should automatically begin to start updating that vtx's position until the mouse button is released and un flagging the vtx.
added the code in, dont know how i would set the vertex as the active editing vertex as you suggest though
Your answer
![](https://koobas.hobune.stream/wayback/20220613164608im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Working with the editor Selection 0 Answers
Editor.OnSceneGUI never called. 3 Answers
Editor Extension, OnSceneGUI Problem 1 Answer
OnSceneGUI has odd behaviour with multiple objects selected 1 Answer
PositionHandle causing problems 0 Answers