Search Unity

PositionHandle causing me problems

Discussion in 'Immediate Mode GUI (IMGUI)' started by MikeUpchat, Jan 28, 2017.

  1. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    I am trying to right a very simple editor script to show some waypoints for my charcater to move around, I have it working and the editor script shows the points. But for some reason if I use Handle.PositionHandle I cant select the handles or get them to move but if I change the line for Handle.FreeMoveHandle then everything works just fine. I think it is somehow connected to the GUI.SetNextControlName but I just cant get it to work, if anyone can let me know what I am missing that stops PositionHandles from working when FreeMoveHandles work just fine I would be very grateful. Below is the OnSceneGUI() method I have in my editor script. I am using control names so I dont have all the waypoints showing PositionHandles as that gets a bit much when lots of points have been placed.
    If I change a value in the inspector or click the 2d icon to switch between 2d and 3d makes the position handles work correctly, and also when the position handles cant be selected I can drag the scene or select the game view tab, it is all very frustrating.

    Code (csharp):
    1.    int selected = 0;
    2.    string lastfocus = "";
    3.  
    4.     public void OnSceneGUI()
    5.     {
    6.         MyClass obj = (MyClass)target;
    7.         Handles.matrix = obj.transform.localToWorldMatrix;
    8.         Handles.color = Color.black;
    9.  
    10.         string hn = GUI.GetNameOfFocusedControl();
    11.  
    12.         if ( hn != lastfocus )
    13.         {
    14.             if ( hn.Length > 0 )
    15.             {
    16.                 if ( hn[0] == 'k' )
    17.                 {
    18.                     GUI.FocusControl("");
    19.                     string hn1 = hn.Replace('k', ' ');
    20.                     selected = int.Parse(hn1);
    21.                 }
    22.             }
    23.  
    24.             lastfocus = hn;
    25.         }
    26.  
    27.         for ( int p = 0; p < obj.points.Count; p++ )
    28.         {
    29.             pm = obj.points[p].p;
    30.  
    31.             GUI.SetNextControlName("k" + p.ToString());
    32.             if ( p == selected )
    33.             {
    34.                 Vector3 newp = Handles.PositionHandle(pm, Quaternion.identity);
    35.                 //Vector3 newp = Handles.FreeMoveHandle(pm, Quaternion.identity, 0.1f, Vector3.zero, Handles.CircleCap);
    36.  
    37.                 Vector3 dl = newp - pm;
    38.  
    39.                 obj.points[p].p += dl;
    40.             }
    41.             else
    42.                 Handles.FreeMoveHandle(pm, Quaternion.identity, 0.01f, Vector3.zero, Handles.CircleCap);
    43.         }
    44.  
    45.         Handles.matrix = Matrix4x4.identity;
    46.     }
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Oh I just came across this issue myself :)
    FreeMoveHandle will keep control of mouse events if you break out of it when its currently being used. E.G stop drawing it.
    You need to reset the hot control back to 0.
    Code (csharp):
    1. EditorGUIUtility.hotControl = 0;
     
    richardkettlewell likes this.