x


GUIStyle not working on GUI.Button

I created a new GUIStyle for a button, the normal color, font, alignment is set properly, but backgrounds and other states text color aren't working, does anyone have a clue of what's wrong? Here is the simplified code of what I'm doing:


		_img = (Texture2D) Resources.Load( "bg1" );
		_img2 = (Texture2D) Resources.Load( "bg2" );
    
		style = new GUIStyle();
		
		style.font = (Font) Resources.Load( "Fonts/Arial" );
		
		style.active.background = _img2; // not working
		style.hover.background = _img2; // not working
		style.normal.background = _img; // not working
		
		style.active.textColor = Color.red; // not working
		style.hover.textColor = Color.blue; // not working
		style.normal.textColor = Color.white;
		
		int border = 30;
		
		style.border.left = border; // not working, since backgrounds aren't showing
		style.border.right = border; // ---
		style.border.top = border; // ---
		style.border.bottom = border; // ---
		
		style.stretchWidth = true; // ---
		style.stretchHeight = true; // not working, since backgrounds aren't showing
		
		style.alignment = TextAnchor.MiddleCenter;
    
    

and I'm drawing it like this:


		GUI.Button( rect, label, style );

more ▼

asked Jul 31 '12 at 08:40 PM

badjano gravatar image

badjano
75 2 9 12

did you check _img2 and _img for null?

Jul 31 '12 at 09:12 PM ScroodgeM

yes, if I put the _img# in a GUI.box or a GUI.drawTexture it works... :(

Jul 31 '12 at 09:33 PM badjano

are these two parts of code are in same method and executed coherently?

Jul 31 '12 at 09:36 PM ScroodgeM

the first part is in the constructor, the second part is called within the OnGUI scope

Jul 31 '12 at 09:42 PM badjano

check style variable in OnGUI scope, for exaple

Debug.Log(style.active.textColor);
- does it have at least one property you'd set in constructor?
Aug 01 '12 at 05:14 AM ScroodgeM
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

have you tried making a copy of the default button first?

style = new GUIStyle(GUI.skin.button);

then modify that style

more ▼

answered Aug 01 '12 at 06:00 PM

anonymousUser gravatar image

anonymousUser
16 1 5 6

(comments are locked)
10|3000 characters needed characters left

try code below. it works fine, just put 'bg1' and 'bg2' to Resources folder. then check your code again - it seems you miss something. if you unable to find solution, post your whole code here.

DynamicGUIStyle.cs

using UnityEngine;
using System.Collections;
public class DynamicGUIStyle : MonoBehaviour
{
	public Rect rect;
	public string label;
	Texture2D _img;
	Texture2D _img2;
	GUIStyle style;
	void Start()
	{
		_img = (Texture2D)Resources.Load("bg1");
		_img2 = (Texture2D)Resources.Load("bg2");

		style = new GUIStyle();

		style.font = (Font)Resources.Load("Fonts/Arial");

		style.active.background = _img2; // not working
		style.hover.background = _img2; // not working
		style.normal.background = _img; // not working

		style.active.textColor = Color.red; // not working
		style.hover.textColor = Color.blue; // not working
		style.normal.textColor = Color.white;

		int border = 30;

		style.border.left = border; // not working, since backgrounds aren't showing
		style.border.right = border; // ---
		style.border.top = border; // ---
		style.border.bottom = border; // ---

		style.stretchWidth = true; // ---
		style.stretchHeight = true; // not working, since backgrounds aren't showing

		style.alignment = TextAnchor.MiddleCenter;
	}
	void OnGUI()
	{
		GUI.Button(rect, label, style);
	}
}
more ▼

answered Aug 01 '12 at 05:08 PM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

I'm sorry guys, my mistake, I was putting the images inside a "Images" folder, forgot to put it in the path :( I'm very sorry

Aug 01 '12 at 05:45 PM badjano
(comments are locked)
10|3000 characters needed characters left

I'm sorry guys, my mistake, I was putting the images inside a "Images" folder, forgot to put it in the path :( I'm very sorry

more ▼

answered Aug 01 '12 at 05:58 PM

badjano gravatar image

badjano
75 2 9 12

(comments are locked)
10|3000 characters needed characters left

Why don't you do it with editor like this ? alt text

captura.png (19.3 kB)
more ▼

answered Aug 01 '12 at 11:16 AM

KirbyRawr gravatar image

KirbyRawr
165 1 4 7

I'm trying to do a framework that is all code, except for images and fonts

Aug 01 '12 at 01:40 PM badjano

I'm sorry guys, my mistake, I was putting the images inside a "Images" folder, forgot to put it in the path :( I'm very sorry

Aug 01 '12 at 05:45 PM badjano
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3692
x789
x167
x54
x48

asked: Jul 31 '12 at 08:40 PM

Seen: 1559 times

Last Updated: Aug 01 '12 at 06:00 PM