1
\$\begingroup\$

I am trying to create handles without any visible game objects. The image below from a sample scene shows rects (top left rect is at position (-2, 4), size is (1, 1)) of the handles I want to appear when the mouse is hovered over the rectangular areas. So basically in a scene with no game objects I want to be able to hover over with the mouse to show the squares below as handles.

enter image description here

I believe the issue is most likely with the Raycast in UpdateHandlePosition(). The handles only appear when there are objects on the screen and they appear at the wrong places. Please see the relevant code below.

static void UpdateHandlePosition()
{
    if( Event.current == null )
    {
        return;
    }

    Vector2 mousePosition = new Vector2( Event.current.mousePosition.x, Event.current.mousePosition.y );

    Ray ray = HandleUtility.GUIPointToWorldRay( mousePosition );
    RaycastHit hit;

    if( Physics.Raycast( ray, out hit, Mathf.Infinity, 1 << LayerMask.NameToLayer( "Default" ) ) == true )
    {
        Vector3 offset = Vector3.zero;

        if( EditorPrefs.GetBool( "SelectBlockNextToMousePosition", true ) == true )
        {
            offset = hit.normal;
        }

        CurrentHandlePosition.x = Mathf.Floor( hit.point.x - hit.normal.x * 0.001f + offset.x );
        CurrentHandlePosition.y = Mathf.Floor( hit.point.y - hit.normal.y * 0.001f + offset.y );
        CurrentHandlePosition.z = Mathf.Floor( hit.point.z - hit.normal.z * 0.001f + offset.z );

        //CurrentHandlePosition += new Vector3( 0.5f, 0.5f, 0.5f );
    }
}


    static void DrawHandlesCube( Vector3 center )
{

    Vector3 p1 = center + Vector3.up * 0.5f + Vector3.right * 0.5f + Vector3.forward * 0.5f;
    Vector3 p2 = center + Vector3.up * 0.5f + Vector3.right * 0.5f - Vector3.forward * 0.5f;
    Vector3 p3 = center + Vector3.up * 0.5f - Vector3.right * 0.5f - Vector3.forward * 0.5f;
    Vector3 p4 = center + Vector3.up * 0.5f - Vector3.right * 0.5f + Vector3.forward * 0.5f;

    Vector3 p5 = center - Vector3.up * 0.5f + Vector3.right * 0.5f + Vector3.forward * 0.5f;
    Vector3 p6 = center - Vector3.up * 0.5f + Vector3.right * 0.5f - Vector3.forward * 0.5f;
    Vector3 p7 = center - Vector3.up * 0.5f - Vector3.right * 0.5f - Vector3.forward * 0.5f;
    Vector3 p8 = center - Vector3.up * 0.5f - Vector3.right * 0.5f + Vector3.forward * 0.5f;

    //You can use Handles to draw 3d objects into the SceneView. If defined properly the
    //user can even interact with the handles. For example Unitys move tool is implemented using Handles
    //However here we simply draw a cube that the 3D position the mouse is pointing to
    Handles.DrawLine( p1, p2 );
    Handles.DrawLine( p2, p3 );
    Handles.DrawLine( p3, p4 );
    Handles.DrawLine( p4, p1 );

    Handles.DrawLine( p5, p6 );
    Handles.DrawLine( p6, p7 );
    Handles.DrawLine( p7, p8 );
    Handles.DrawLine( p8, p5 );

    Handles.DrawLine( p1, p5 );
    Handles.DrawLine( p2, p6 );
    Handles.DrawLine( p3, p7 );   
    Handles.DrawLine( p4, p8 );

}
}
\$\endgroup\$
  • \$\begingroup\$ Uh. You can easily have a gameobject with a collider attached, but no renderer. That would create a zone that you can mouse-over without drawing anything. \$\endgroup\$ – Draco18s Jun 20 '17 at 19:29
  • \$\begingroup\$ @Draco18s I would like to achieve this without colliders and especially game objects, that's the main reason I want to use handles. \$\endgroup\$ – Containment Jun 20 '17 at 20:39
  • \$\begingroup\$ If you dont have a gameobject, the component is not attached to anything, and thus, will be removed by the garbage collector. \$\endgroup\$ – Gnemlock Jun 20 '17 at 21:39
  • 1
    \$\begingroup\$ Note the difference between having a game object and having a visible game object. \$\endgroup\$ – Gnemlock Jun 20 '17 at 21:40
  • \$\begingroup\$ Handles, in no way, help you solve your problem. You're still making a call to Physics.Raycast which still requires that a collider exist in the scene and a collider requires that it be attached to a game object. \$\endgroup\$ – Draco18s Jun 21 '17 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.