Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Alternative for Screen.width and Screen.height

Discussion in 'Scripting' started by dccoo, Mar 29, 2018.

  1. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    161
    Straightforward, I would like something that gives me the same result from Screen.width and Screen.height without using Screen.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    I suppose you could have a ui component that stretches across the height and width and then access it's recttransform to get the width and height of the rect. I believe that would give the same numbers, but not sure why you'd want that over the Screen.width/.height
     
    dccoo likes this.
  3. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Why are you avoiding Screen?
     
  4. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    161
    the guy that is working with me created a script called Screen and now unity complains that Screen.width do not exist, so I got curious if there was another simple way (instead of UnityEngine.Screen.width)
     
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    It's been answered in the other post now. Use Display https://docs.unity3d.com/ScriptReference/Display.html.
     
    dccoo likes this.
  6. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Just put that guys Screen class in a namespace. Then make sure to tell them to not make conflicting names without also putting them in a namespace. You should be able to use the API its there for easy of use.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Yep, just use a namespace...or, use Unity's namespace.

    So you could do
    int screenWIdth = UnityEngine.Screen.width; For example to access Unity's Screen vs the one created.
     
  8. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    You could also use an using alias to remap a namespace to whatever you prefer:

    Code (csharp):
    1.  
    2. using UnityScreen = UnityEngine.Screen;
    3.  
    4. public class Test : MonoBehaviour
    5. {
    6.     private void Update()
    7.     {
    8.         Debug.Log("Width = " + UnityScreen.width);
    9.     }
    10. }
    11.  
    Of course, I'd still go yell at the other guy for not putting his code into a namespace. :D

    Or threaten him with creating your own UnityEngine class and let them sort out the namespace mess that'd create. :p