Search Unity

2D. How to create a rect stat has the size of the camera (screen)?

Discussion in '2D' started by Tosek, Sep 21, 2017.

  1. Tosek

    Tosek

    Joined:
    Apr 28, 2015
    Posts:
    3
    Hi there,

    How can I create (calculate the size of) a rect that has the exact size of the current camera for a 2D game? I want to use that rect as background that has exactly the same size as the screen.

    Thanks in advance
    Tosek
     
  2. Joimer

    Joimer

    Joined:
    Aug 1, 2017
    Posts:
    14
    You can get the camera's rect to work with it:
    Code (CSharp):
    1. var cam = GetComponent<Camera>();
    2. cam.rect; // Its Rect object
    3. cam.rect.width; // The width of the camera rect, in a float number.
     
  3. Deleted User

    Deleted User

    Guest

    You can also just get the size
    Code (csharp):
    1.  
    2. Camera cam = Camera.main;
    3. float height = 2f * cam.orthographicSize
    4. float width = height * cam.aspect;
    5.  
     
  4. Tosek

    Tosek

    Joined:
    Apr 28, 2015
    Posts:
    3
    Tnak you guys.