Search Unity

Visulization of a rect

Discussion in 'Getting Started' started by DownER149, Sep 20, 2018.

  1. DownER149

    DownER149

    Joined:
    Sep 11, 2018
    Posts:
    61
    im trying to use
    IB = new Rect(a);
    IB.Contains((Input.mousePosition))

    is there any way that i can visualize a rect?
    trying to find the bounds of an invisible box can be quite tedious.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    There's no easy way to visualize it, no. But you can certainly Debug.Log it, and compare the numbers you see to what you would expect (and the mouse position).
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
    Yes. You can have the editor draw gizmos representing the rect.

    https://docs.unity3d.com/ScriptReference/Gizmos.DrawWireCube.html

    I have not tested this code but it should work. Just add it to the script with the rect you want to visualize.

    Code (csharp):
    1. void OnDrawGizmos()
    2. {
    3.     // Green
    4.     Gizmos.color = new Color(0.0f, 1.0f, 0.0f);
    5.     DrawRect(your_rect_variable);
    6. }
    7.  
    8. void OnDrawGizmosSelected()
    9. {
    10.     // Orange
    11.     Gizmos.color = new Color(1.0f, 0.5f, 0.0f);
    12.     DrawRect(your_rect_variable);
    13. }
    14.  
    15. void DrawRect(Rect rect)
    16. {
    17.     Gizmos.DrawWireCube(new Vector3(rect.center.x, rect.center.y, 0.01f), new Vector3(rect.size.x, rect.size.y, 0.01f));
    18. }
     
    Last edited: Sep 20, 2018
    Nit_Ram, Kokowolo, hms0589 and 5 others like this.
  4. blazingPotato

    blazingPotato

    Joined:
    Feb 17, 2020
    Posts:
    5
    thanks, it works!
     
  5. JarekKaa94

    JarekKaa94

    Joined:
    May 31, 2018
    Posts:
    10
    No, no, no. You can do it better:
    Code (CSharp):
    1. Handles.DrawSolidRectangleWithOutline(new[]
    2.             {
    3.                 transform.position + new Vector3(-neededPlace / 2, 0, -neededPlace / 2),
    4.                 transform.position + new Vector3(neededPlace / 2, 0, -neededPlace / 2),
    5.                 transform.position + new Vector3(neededPlace / 2, 0, neededPlace / 2),
    6.                 transform.position + new Vector3(-neededPlace / 2, 0, neededPlace / 2),
    7.             }, new Color(0, 1, 0, 0.2f), Color.green);
    Its solid rect with outline.
     
    LemonShaped likes this.