- Home /
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;
</code>
</pre>
and I'm drawing it like this:
GUI.Button( rect, label, style );
yes, if I put the _img# in a GUI.box or a GUI.drawTexture it works... :(
are these two parts of code are in same method and executed coherently?
the first part is in the constructor, the second part is called within the OnGUI scope
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?
Answer by badjano · Aug 01, 2012 at 05:58 PM
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
Answer by Kirbyrawr · Aug 01, 2012 at 11:16 AM
Why don't you do it with editor like this ?
I'm trying to do a framework that is all code, except for images and fonts
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
Answer by anonymousUser · Aug 01, 2012 at 06:00 PM
have you tried making a copy of the default button first?
style = new GUIStyle(GUI.skin.button);
then modify that style
Answer by ScroodgeM · Aug 01, 2012 at 05:08 PM
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);
}
}
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