Build in GUI Text Alignment

Hello all,

How can I access the build in GUI Text allignment thru scripting.

Trying something like the code below

GUISkin.settings = TextAlignment.left;

but getting errors.

Thanks,
Ray

This is something I was battling with lately. See My solution:

http://forum.unity3d.com/viewtopic.php?p=67155#67155

You may also want to look at the GUIStyle.alignment scripting reference.

Hi CoherentInk,

Works great thank you.

What I found out is if I make my GUIStlyle = null it affects all other GUI’s that use the default built in GUI skin.

theNationTextFile = theTextFile[0].text;
myGUIStyle = null;
myGUIStyle.alignment = TextAnchor.UpperLeft;
GUI.Box(Rect(11,100,330,315),GUIContent(theNationTextFile));

The snippet above is inside an if condition.

I guess we can not alter the builtin gui with out altering the other GUI’s using the default built in GUIS. :?:

Also,
When I make my GUIStyle = null and remove that script, the rest of the GUI remains on the upper left, needed to re start Unity(open a different project then re open the project I’m working on) for the GUI’s to go back to the original state(Middle)

Maybe I’m doing something gooffy to this GUIS. :?

Thanks,
Ray

Oh no! You’re right! I thought that I had found a way to create a new style through scripting, but alas, I have not!

It appears that my attempt to copy the style doesn’t do that at all-- it merely copies the address (or the pointer) to the current style, and any modifications to it actually modify the original style!

That said, I think I finally have the solution! The GUI system behaves in a manner unlike anything I’ve ever worked with, with all its oddities within the OnGUI() function.

To change just one attribute for a given style, you go about it the same way you would gray-out disabled controls: make a statement in OnGUI() to change the state of the GUI, and then change it back later. Below is an example of how to change alignment of text:

function OnGUI () {
    toggle = GUI.Toggle(Rect(x, y, width, height), toggle, "Enable/Disable");
    // enable or disable a field based on the previous toggle
    GUI.enabled = toggle;
    slider = GUI.HorizontalSlider(Rect(x, y + height, width, height), slider, 0.0, 1.0);
    // enable fields after here
    GUI.enabled = true;
    
    // a normal label
    GUI.Label(Rect(x, y + 2 * height, width, height), "I'm a normal label");
    // now change the alignment for labels to the right
    GUI.skin.label.alignment = TextAnchor.UpperRight;
    GUI.Label(Rect(x, y + 3 * height, width, height), "I'm a right aligned label");
    // now change the alignment for labels to the middle
    GUI.skin.label.alignment = TextAnchor.UpperCenter;
    GUI.Label(Rect(x, y + 4 * height, width, height), "I'm a center aligned label");
    // now make labels normal again
    GUI.skin.label.alignment = TextAnchor.UpperLeft;
    GUI.Label(Rect(x, y + 5 * height, width, height), "I'm a normal label again");
}

Sorry for the confusion, but I’m still working this out myself!

Hopefully this functionality can be added to the manual in future releases.

Hello,

Thank you, yellowLabrador and CoherentInk, I was working whole day in this issue but still I am not totally satisfied with the solution.

With the final solution CoherentInk provides, it works, though next time you creates a new (e.g.) GUI.Label the alignment is still the same was modified last time.
So, for me the perfect solution is the one that copies all the values the default GUIStyle provides into a new GUIStyle. And then, in this new GUIStyle you can modify (e.g.) the GUI.Label alignment.
But I was working for few hours in this I can’t bring with nothing new. Hopefully, someone could bring to light this issue.

Cheers

Hey guys I found a solution to this problem that works for me. That is how to copy an existing GUIStyle into a new GUIStyle and modify just a few of its variables. I’m working in C# and not sure how well this may translate to javascript.

First have a reference to the existing style you want to copy and then create a new GUIStyle using the new keyword and pass it the reference.

InventoryHeaderStyle = new GUIStyle(DialogBoxStyle);
InventoryHeaderStyle.alignment = TextAnchor.UpperLeft;
InventoryHeaderStyle.padding.left = 10;
InventoryHeaderStyle.padding.top = 0;
InventoryHeaderStyle.normal.background = null;

Here is a little bit more of that class for contex.

public class PlayerQuestionWindow 
{
	float currentPlayerDialogHeight = 0;
	public float targetPlayerDialogHeight;
	float lineoffset;
	float ScrollViewHeight;
	float ScrollViewOffset;
	float ScrollViewLineOffset;
	int MaxQuestionInView = 6;
	GUIStyle DialogBoxStyle;
	GUIStyle InventoryHeaderStyle;
	GUIStyle PlayerDialogTextStyle;
	public Dictionary<int,InterviewQuestion> Questions;
	public List<long> SelectedAnswers; 
	public Texture2D NextIcon;
	int MaxNumOfChars = 110;
	Vector2 scrollViewVector = Vector2.zero;
	string StepName = "";

	public PlayerQuestionWindow(GUIStyle boxstyle, GUIStyle playertextstyle,Dictionary<int,InterviewQuestion>  questions,List<long> answers, Texture2D nexticon, string stepname )
	{
		DialogBoxStyle = boxstyle;
		PlayerDialogTextStyle = playertextstyle;
		Questions = questions;
		SelectedAnswers = answers;
		lineoffset = PlayerDialogTextStyle.fontSize + 10;
		targetPlayerDialogHeight = (Questions.Count * lineoffset) + lineoffset;
		NextIcon = nexticon;
		ScrollViewHeight = lineoffset*MaxQuestionInView;
		ScrollViewOffset = 0;
		ScrollViewLineOffset = 0;
		StepName = stepname;
		
		InventoryHeaderStyle = new GUIStyle(DialogBoxStyle);
		InventoryHeaderStyle.alignment = TextAnchor.UpperLeft;
		InventoryHeaderStyle.padding.left = 10;
		InventoryHeaderStyle.padding.top = 0;
		InventoryHeaderStyle.normal.background = null;
	}

Again this is C# and I’m not sure if the new keyword works the same way in javascript. But this solution is indeed working for me.

I hope that helps.