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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GUITextures Fits ANY Screen Size

Discussion in 'Scripting' started by Cooper37, Aug 15, 2016.

  1. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Hello all! I have a bit of a problem, how can I get this piece of script to scale the texture along with any screen size? This script simply draws a GUITexture over an empty gameObject.
    cs.
    Code (csharp):
    1.  
    2. void OnGUI(){
    3.             var guiPosition = Camera.main.WorldToScreenPoint(transform.position);
    4.             guiPosition.y = Screen.height - guiPosition.y;
    5.             GUI.DrawTexture(new Rect(guiPosition.x - (blip.width/2),guiPosition.y - (blip.height/2), blip.width, blip.height), blip);
    6.     }
    7.  
    Thanks for the help! :)
     
  2. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    not tested but you get the idea:
    Code (CSharp):
    1. void OnGUI(){
    2.             var guiPosition = Camera.main.WorldToScreenPoint(transform.position);
    3.             guiPosition.y = Screen.height - guiPosition.y;
    4.             var screensizex = 1920/Screen.Height;
    5.             var screensizey = 1080/Screen.Width;
    6.             GUI.DrawTexture(new Rect(guiPosition.x - (blip.width/2) ,guiPosition.y - (blip.height/2), blip.width*screensizey, blip.height*screensizex), blip);
    7.     }
     
  3. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    For the most part, it works! I doesn't really work for supper tiny screens though, which is okay. I couldn't imagine my game being played on something smaller than a laptop screen. Thanks! :)