- Home /
 
How to center a rect in a gui.label
I feel kinda dumb asking this, but is there anyway to center a rect based on how long the sentence is? Here is my script so far using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
 public class TextScript : MonoBehaviour {
 
     public bool onLoad = false;
     [HideInInspector]public bool isLoaded = false;
     public string Text = "test of how long this sentence can be before it uncenters";
 
     private GUIStyle guiStyle = new GUIStyle();
 
 
     private void Start()
     {
         StartCoroutine(Wait());
         OnGUI();
         if (onLoad)
         {
             isLoaded = true;
         }
     }
 
     private void OnGUI()
     {
         guiStyle.fontSize = 20;
         guiStyle.normal.textColor = Color.white;
         if (onLoad)
         {
             if (isLoaded)
             {
                 GUI.Label(new Rect(Screen.width / 2, Screen.height - 50, Screen.width, Screen.height), Text, guiStyle);
             }
         }
     }
 
     IEnumerator Wait()
     {
         yield return new WaitForSeconds(5);
         isLoaded = false;
     }
 }
 
               If i play the scene, the text doesn't center with the rect. How do i figure this out? Oh and this is unity GUI.label, not GUIText.
Answer by Lost_Syndicate · Mar 28, 2018 at 01:55 AM
i found out how. I used guiStyle.alignment = TextAnchor.LowerLeft and changed the rect to (0, 0, Screen.width, Screen.height)
Your answer
 
             Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Unity C# lean script not working properly 1 Answer
Is it possible to give a color to int variables? 1 Answer
EventTrigger PointerEnter 1 Answer