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.

[Tip] Adjusting GUITextures to Screen Size

Discussion in 'Immediate Mode GUI (IMGUI)' started by reduktion, Jul 30, 2013.

  1. reduktion

    reduktion

    Joined:
    Sep 27, 2012
    Posts:
    24
    GUITextures are scaled relative to the viewport aspect ratio and get distorted when run at another aspect ratio. The following script solved the problem for me. Might be helpful. (It won't work when GUITextures are parented to other GUITextures.)

    Code (csharp):
    1.  
    2. float originalAspect = 960f / 640f;
    3. float currentAspect = Screen.width / (float) Screen.height;
    4. Vector3 scaleVector = new Vector3(originalAspect / currentAspect, 1, 1);
    5.  
    6. GUITexture[] allGUITextures = FindObjectsOfType(typeof(GUITexture)) as GUITexture[];
    7. for(int i = 0; i < allGUITextures.Length; i++){
    8.     allGUITextures[i].transform.localScale = Vector3.Scale(allGUITextures[i].transform.localScale, scaleVector);
    9.            
    10. }