1

I have a GUILayout.TextField where I needed to center the text. I added a GUIStyle as an argument where

centerStyle = new GUIStyle
{
    alignment = TextAnchor.MiddleCenter,
};

However, this causes the background box of my GUILayout.TextField to disappear and the text becomes misaligned.

Before centering: Before

After centering: After

(Sorry i can't really post pictures yet)

Here's the code where i created the TextField:

GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
guildName = GUILayout.TextField(guildName, 20, centerStyle, GUILayout.Width(200));
bool canCreate = true;

foreach (string word in guilds)
{
   if (guildName == word)
       canCreate = false;
}

if (GUILayout.Button("Create", GUILayout.Width(75)) && canCreate)
{
    GameManagerVik.CreateGuild(guildName);
}

GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();

Any help or advice on my codes will be much appreciated, thanks in advance!

Ryolu
  • 523
  • 3
  • 16
  • 1
    Stop using OnGui and use [the new UI](https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/the-new-ui) – Draco18s no longer trusts SE Jun 09 '18 at 03:52
  • As much as I'd like to do that, I can't. I was given this project to work with and I'm not experienced enough to change the entire project by myself. The entirety of the project uses OnGUI and I don't really have a choice since its schoolwork... – Ryolu Jun 09 '18 at 04:00
  • Are there other properties of GUIStyle that you forgot to set? https://docs.unity3d.com/ScriptReference/GUIStyle.html – mjwills Jun 09 '18 at 04:02
  • The properties that I don't set are will be set to the default values, won't they? Since I didn't set any values for the non-centered version, I thought the default values would work but apparently i was wrong. – Ryolu Jun 09 '18 at 04:04
  • 1
    *"I don't really have a choice since its schoolwork"*. Some schools still teach Unity with the old book and projects that comes with them but you can ignore the UI part and use the new UI system. You even write less code with the new UI. The most basic components: Text, Image and InputField, Button, Toggle, Slider. You can always come back to ask question about them if you are confused. You can do this if you are willing to. – PerformanceFreak Jun 09 '18 at 04:15
  • You make a fair point. However, its long overdue and I don't exactly have the time to redo everything I did, unless that's not what you meant @PerformanceFreak – Ryolu Jun 09 '18 at 04:52

1 Answers1

2

Do not use any API that requires to be placed in the OnGUI function. This includes GUILayout.TextField and GUIStyle. This is the old UI system and has been replaced with a new one. Use this only when making Editor plugin.

If you want to get a text from the user, use the InputField component.

To create one, go to GameObject ---> UI ---> Input Field.


If you want to get display a text to the user, use the Text component.

To create one, go to GameObject ---> UI ---> Text.

You can see other UI components from this menu.

To align the text in the Text component:

yourText.alignment = TextAnchor.MiddleCenter;

To detect when they are clicked, or when the input has changed, see this post.

Programmer
  • 121,791
  • 22
  • 236
  • 328