Search Unity

Handles.PositionHandle does not do anything

Discussion in 'Scripting' started by Aram-Azhari, Jun 21, 2014.

  1. Aram-Azhari

    Aram-Azhari

    Joined:
    Nov 18, 2009
    Posts:
    142
    Hi,

    I have been trying to edit the mesh using positionhandle. I can draw the handle, but dragging any component of it does not do anything to the object.

    Is positionHandle just for visuals ? I thought it to be some sort of control since it returns Vector3 (similar to vector3 slider).

    Code (csharp):
    1.  
    2. [ExecuteInEditMode]
    3. [CustomEditor(typeof(VertexEditorObject))]
    4. public class EditVertex : Editor
    5. {
    6.  
    7.     void OnInspectorGUI()
    8.     {
    9.         DrawDefaultInspector();
    10.     }
    11.  
    12.     MeshFilter mf;
    13.     int selectedControl = -1;
    14.     void OnSceneGUI()
    15.     {
    16.  
    17.         VertexEditorObject o = (target as VertexEditorObject);
    18.  
    19.         if (mf == null)
    20.         {
    21.             var temp = o.GetComponent<MeshFilter>();
    22.             if (temp != null)
    23.                 mf = temp;
    24.         }
    25.         else
    26.         {
    27.             if (o.DrawVertexHandles)
    28.             {
    29.                 Handles.color = Color.yellow;
    30.                 for (int i = 0; i < mf.sharedMesh.vertexCount; i++)
    31.                 {
    32.                     Handles.SphereCap(i, o.transform.TransformPoint(mf.mesh.vertices[i]), Quaternion.identity, 0.125f);
    33.                 }
    34.  
    35.                 for (int i = 0; i < mf.sharedMesh.vertexCount && i < 10; i++)
    36.                 {
    37.                     var r = Handles.PositionHandle(o.transform.TransformPoint(mf.sharedMesh.vertices[selectedControl]), Quaternion.identity);
    38.                     mf.sharedMesh.vertices[selectedControl] = o.transform.InverseTransformPoint(r);
    39.                     mf.sharedMesh.RecalculateBounds();
    40.                     mf.sharedMesh.RecalculateNormals();
    41.                 }
    42.             }
    43.         }
    44.         if (GUI.changed)
    45.             EditorUtility.SetDirty(target);
    46.     }
    47. }
    48.  
    I also tried to use GameDraw (free) and it seems his positionhandle doesn't work either.

    Note that I'm using the latest unity version.

    Any helps would be appreciated.

    Thanks.
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I haven't read your code properly but it doesn't look like your setting thy vertices to the handles position

    Your setting the handles to the vertex position, you need to set the vertex to the position handle.

    So it would be something like vertex position = handle.PositionHandle(vertexPosition)
     
  3. Aram-Azhari

    Aram-Azhari

    Joined:
    Nov 18, 2009
    Posts:
    142
    Take a look at line 37,38
     
  4. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    If I remember correctly, the vertices property of a mesh does NOT return the acctual internal vertex array, but just a copy. This does aswell make sense in terms of OOP design.
    So store the vertex array in a temporal array, modify this variable and write it back to the mesh when you are done.
    This does aswell make sense from the performance aspect, it doesnt make sense to call the vertices property three times per vertex just to accses it.

    If the method of writing back does not enitrly work, you might aswell consider to call Mesh.Clear() and freshly set the respective properties.
     
  5. Aram-Azhari

    Aram-Azhari

    Joined:
    Nov 18, 2009
    Posts:
    142
    Wow thank you. It worked!