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

[Solved] Question - OnGui Alignment Text

Discussion in 'Scripting' started by kholyphoenix1, Nov 18, 2015.

  1. kholyphoenix1

    kholyphoenix1

    Joined:
    Apr 17, 2015
    Posts:
    57
    Hello,

    I've got this script "OnGui.Box"
    I wonder how I do so regardless of the size of the "box" in the game text is aligned to the left?

    Thanks a lot!

    Code (CSharp):
    1. // Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
    2. // Do test the code! You usually need to change a few small bits.
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class TimerCounter : MonoBehaviour {
    8.  
    9.  
    10.     float timer = 300.0f;
    11.  
    12.     void  Update (){
    13.  
    14.         timer -= Time.deltaTime;
    15.  
    16.         if(timer <= 0) {
    17.             timer = 0;
    18.         }
    19.  
    20.     }
    21.  
    22.     void  OnGUI (){
    23.  
    24.         GUI.Box(new Rect(10, 40, 500, 20), "Time: " + timer.ToString("0"));
    25.  
    26.     }
    27.  
    28. }
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    OnGUI is obsolete for most use cases. Consider switching to the new UI.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. kholyphoenix1

    kholyphoenix1

    Joined:
    Apr 17, 2015
    Posts:
    57
    I've seen that.
    More to be honest I consider complex.
    I want something simple like to appear in the game.

    Thanks a lot!
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    add canvas, add textbox, drag align where you want on the screen, drag size how you want, check the left align option in the font settings.

    Code (csharp):
    1.  
    2. //sample code
    3. include UnityEngine.UI;
    4.  
    5. Text myScoreField;
    6.  
    7. myScoreField.text = "Score: " + someValue;
    8.  
    might be slightly more complex for something as basic as displaying a single line of text, but once you start getting into anything even slightly more involved you'll be thankful of the new UI system.
     
    Kiwasi likes this.