Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Customizing and Resizing GUI Text

Discussion in 'Immediate Mode GUI (IMGUI)' started by AreebM, Aug 15, 2014.

  1. AreebM

    AreebM

    Joined:
    Mar 27, 2014
    Posts:
    32
    I would like to create a few boxes to be placed on the Menu Screen, each of which contain different text. I'd like to set a certain font size, color and type.

    The problem with using GUI Text is that although I can customize it nicely, I'm not sure how to resize it based on different screen resolutions. I've used the Rect function for GUI Textures, but I'm not sure what to use for GUI Text.

    What is the best way to do this? Would I use GUI Styles? I'm scripting in C, but the Unity documentation only has examples in Java.

    An example menu would be:

    Play
    Tutorial
    Upgrades
    About


    Thank you!
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    i've been using this for guitextures & guitexts (code is mostly from unity answers),
    this one is not the latest version though it might had some problems in HD..

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. // guitext & guitexture autoscaler
    5.  
    6. public class GUIScaler : MonoBehaviour
    7. {
    8.     public float scaleAdjust = 1.0f;
    9.     private float scaleFix = 1.4f;
    10.  
    11.     private const float BASE_WIDTH = 1280;
    12.     private const float BASE_HEIGHT = 800;
    13.  
    14.     void Start()
    15.     {
    16.         float _baseHeightInverted = 1.0f/BASE_HEIGHT;
    17.         float ratio = (Screen.height * _baseHeightInverted)*scaleFix*scaleAdjust;
    18.         //Debug.Log ("scale ratio:"+ratio);
    19.  
    20.         if (GetComponent<GUITexture>()!=null)
    21.         {
    22.             GUITexture _guiTextureRef = GetComponent<GUITexture>();
    23.             _guiTextureRef.pixelInset = new Rect(_guiTextureRef.pixelInset.x* ratio, _guiTextureRef.pixelInset.y* ratio, _guiTextureRef.pixelInset.width * ratio, _guiTextureRef.pixelInset.height * ratio);
    24.         }else{
    25.             if (GetComponent<GUIText>()!=null)
    26.             {
    27.                 GetComponent<GUIText>().pixelOffset = new Vector2(GetComponent<GUIText>().pixelOffset.x*ratio, GetComponent<GUIText>().pixelOffset.y*ratio);
    28.                 GetComponent<GUIText>().fontSize = (int)(GetComponent<GUIText>().fontSize*ratio);
    29.             }
    30.         }
    31.     }
    32. }
    33.  
     
    AreebM likes this.
  3. AreebM

    AreebM

    Joined:
    Mar 27, 2014
    Posts:
    32
    Thank you!

    What does _baseHeightInverted do? (what is its value and where does it come from?) And what is the reason for the 1.0f and 1.4f values for scaleAdjust and scaleFix respectively?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350