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:
<pre>`
GUI.Button( rect, label, style );
`
Why don’t you do it with editor like this ?
have you tried making a copy of the default button first?
style = new GUIStyle(GUI.skin.button);
then modify that style
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