Search Unity

Clean / Simple way to turn Rect values into ScreenSpace

Discussion in 'Scripting' started by Thimble2600, Dec 12, 2018.

  1. Thimble2600

    Thimble2600

    Joined:
    Nov 27, 2015
    Posts:
    165
    Basically I want to create a rectangle inside part of a image and to make sure the boundaries are correct I'd like to use Debug.DrawLine to form the rectangle.

    Here's a couple methods I've tried. The top method works, however it's a lot of code for something so basic. If it were as simple as
    Debug.DrawRectangle(rect);
    I'd be set. Or maybe not, cause of the whole' having to transform local position to world space.

    Anyway, here's what I've written. If you're reading and you know of an easier way to do what I've done below (that doesn't involve Unity's Collision) let me know.

    Code (CSharp):
    1.     [ExecuteInEditMode]
    2.     private void Update()
    3.     {
    4.         //WORKS
    5.  
    6.         Vector2 topLeft = new Vector2 ( 4.0f , -5.0f );
    7.         Vector2 topRight = topLeft + ( Vector2.right * 202.0f );
    8.  
    9.         Vector2 botRight = topRight + ( Vector2.down * 14.0f );
    10.         Vector2 botLeft = topLeft + ( Vector2.down * 14.0f );
    11.  
    12.         topLeft = transform.TransformPoint ( botLeft );
    13.         topRight = transform.TransformPoint ( botRight );
    14.         topRight = transform.TransformPoint ( topRight );
    15.         topLeft = transform.TransformPoint ( topLeft );
    16.  
    17.         Debug.DrawLine ( topLeft , topRight );
    18.         Debug.DrawLine ( topRight , botRight );
    19.         Debug.DrawLine ( botRight , botLeft );
    20.         Debug.DrawLine ( botLeft , topLeft );
    21.     }


    Code (CSharp):
    1.     [ExecuteInEditMode]
    2.     private void Update()
    3.     {
    4.         //DOESN'T WORK
    5.  
    6.         float x = 4;
    7.         float y = 5.0f;
    8.         float width = 202.0f;
    9.         float height = 14.0f;
    10.         Vector2 size = new Vector2 ( width , height );
    11.         Vector2 position = new Vector2 ( x , y );
    12.  
    13.         Rect rect = new Rect ( position ,  size );
    14.  
    15.         Vector2 topLeft = new Vector2 ( rect.xMin , rect.yMin );
    16.         Vector2 topRight = new Vector2 ( rect.xMax , rect.yMin );
    17.         Vector2 botRight = new Vector2 ( rect.xMax , rect.yMax );
    18.         Vector2 botLeft = new Vector2 ( rect.xMin , rect.yMax );
    19.  
    20.         Debug.DrawLine ( topLeft , topRight );
    21.         Debug.DrawLine ( topRight , botRight );
    22.         Debug.DrawLine ( botRight , botLeft );
    23.         Debug.DrawLine ( botLeft , topLeft );
    24.     }
     
    Last edited: Dec 12, 2018
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    How to map to screenspace is completely dependent on where you're coming from.

    Unity UI uses Canvases in one of a few different modes: overlay, camera, and world, iirc. Each one has a slightly-different source of "what is where." You will likely need to involve the RectTransform of the root canvas of your UI, as I have taken this approach in the past, using it as my basis, then scaling to screen resolution.

    The world itself maps to the camera as follows: Orthosize is half the height of an ortho camera's view, and FieldOfView is the angle between "straight forward" and "to the top edge of my screen" in perspective cameras, i.e, half the height of your view.