Unity Community


Results 1 to 8 of 8
  1. Unity Developer

    Posts
    877

    Currently selected Handle in editor script

    Is there a way of knowing if one of your Handles is currently selected or gets selected in CustomEditor?

    I'm currently using Handles.FreeMoveHandle for every control point in my spline-tool. I try to keep track of the selected point via hacky code, where I'm making an intelligent guess during a mouseUp-event, that tries to determine which of the points is the closest of the click. This works "ok", but I'm getting reports that it's not 100%.
    Last edited by keely; 05-06-2011 at 12:29 AM.

  2. Unity Developer

    Posts
    877
    There's so much noise that I'm going to bump this once.


  3. Posts
    2,251
    If I understand your issue correctly, have you tried checking GUI.changed after drawing each handle? In theory, this should tell you which one was the last one with which the user interacted. Does that help?

  4. Unity Developer

    Posts
    877
    Quote Originally Posted by Brady View Post
    If I understand your issue correctly, have you tried checking GUI.changed after drawing each handle? In theory, this should tell you which one was the last one with which the user interacted. Does that help?
    I don't know yet, but seems like potential winner. I'm going to test it later when I get home. Thanks!

  5. mas mas is offline

    Location
    The Netherlands
    Posts
    694
    Any more info on this issue? I am wrestling with the same problem for my Orthello OTPath spline.

    The GUI.changed technique is only set when a handle has been moved.changed, not when you just select a handle ( and it gets outlined with a yellow line )

    Will go and check if I can cast a ray against the handle.

    Update

    I have managed to find the selected handle by doing this :

    - Code goes into the OnSceneGUI() of the editor class

    - Check if the GUIUtility.hotControl has changed ( value of 0 = no control selected )

    - If it hotControl changed, find the current handle by ray casting into the world using HandleUtility.GUIPointToWorldRay(Event.current.mou sePosition); and checking if that
    ray intersects with any of the created handles.

    This worlks because I know where I create my handles and within what radius the specific ray should fall to intersect with the handle.

    After I have found the right control, I can cache the controlID for future and quicker lookup. I have not managed to find a way to get a handle's ControlID right after I have created it. ( anyone??)
    Last edited by mas; 08-08-2011 at 06:26 AM.
    Martijn Segers | WyrmTale Games |

    “The intuitive mind is a sacred gift and the rational mind is a faithful servant. We have created a society that honors the servant and has forgotten the gift.” - Albert Einstein


  6. Posts
    1
    Cheers for the info, it pointed me in the right direction.

    The easiest way I found to do this is to name the Control using GUI.SetNextControlName ( ".." ).

    Unfortunately as some of the Handles ( PositionHandle ) create multiple controls only
    the first one will be named, but it easy enough to build your own from the components
    provided.

    Take the PositionHandle as an example we can add the following function to recreate
    it from the other handles provided. As you can see we set the name of each of the
    controls, this will let us use the function GUI.GetNameOfFocusedControl () to get the
    name of the control and use that to lookup an array or dictionary to find the related
    item.

    Vector3 PositionHandle ( Vector3 position, Quaternion rotation, string name )
    {
    // right Axis
    GUI.SetNextControlName ( name );
    Handles.color = new Color ( 1.0f, 0.0f, 0.0f, 0.75f );
    Vector3 newPos = Handles.Slider ( position, rotation*Vector3.right );

    // Up Axis
    GUI.SetNextControlName ( name );
    Handles.color = new Color ( 0.0f, 1.0f, 0.0f, 0.75f );
    newPos += Handles.Slider ( position, rotation*Vector3.up ) - position;

    // Forward Axis
    GUI.SetNextControlName ( name );
    Handles.color = new Color ( 0.0f, 0.0f, 1.0f, 0.75f );
    newPos += Handles.Slider ( position, rotation*Vector3.forward ) - position;

    float sizeMultiplier = 0.0f;

    if ( Camera.current.orthographic )
    {
    sizeMultiplier = Camera.current.orthographicSize * 2.5f;
    }
    else
    {
    Plane screen = new Plane ( Camera.current.transform.forward, Camera.current.transform.position );
    screen.Raycast ( new Ray ( position, Camera.current.transform.forward ), out sizeMultiplier );
    }

    GUI.SetNextControlName ( name );
    Handles.color = new Color ( 1.0f, 1.0f, 1.0f, 0.75f );
    newPos += Handles.FreeMoveHandle ( position, rotation, 0.02f * sizeMultiplier, Vector3.zero, Handles.RectangleCap ) - position;
    }


  7. Posts
    781
    Quote Originally Posted by mas View Post
    - Code goes into the OnSceneGUI() of the editor class

    - Check if the GUIUtility.hotControl has changed ( value of 0 = no control selected )
    Thanks for this post. I wasn't looking for the handle name, but I did want to check if a handle was pressed before raycasting.

    Thanks.
    Paint scripting via video training.

    Need a tree view control in Unity?

    Easily add vegetation to your scene.

    Toolbox directly exports to VS 2010/VS 2012.

    Remotely edit the scene while it's running on a target device.

    Docs Unity on OUYA


  8. Posts
    1
    Just a little addition to this thread, in Unity 4.2 (at least) you can set the ID of some of the handles EG:

    static Vector3 Slider2D(
    int id,
    Vector3 handlePos,
    Vector3 offset,
    Vector3 handleDir,
    Vector3 slideDir1,
    Vector3 slideDir2,
    float handleSize,
    DrawCapFunction drawFunc,
    Vector2 snap,
    bool drawHelper);

    This can be used with GUIUtility.hotControl to identify which handle the user is currently interacting with

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •