Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by badjano · Jul 31, 2012 at 08:40 PM · guibuttonbackgroundstatestyle

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 );

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ScroodgeM · Jul 31, 2012 at 09:12 PM 0
Share

did you check _img2 and _img for null?

avatar image badjano · Jul 31, 2012 at 09:33 PM 0
Share

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

avatar image ScroodgeM · Jul 31, 2012 at 09:36 PM 0
Share

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

avatar image badjano · Jul 31, 2012 at 09:42 PM 0
Share

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

avatar image ScroodgeM · Aug 01, 2012 at 05:14 AM 0
Share

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?
Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Kirbyrawr · Aug 01, 2012 at 11:16 AM

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


captura.png (19.3 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image badjano · Aug 01, 2012 at 01:40 PM 0
Share

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

avatar image badjano · Aug 01, 2012 at 05:45 PM 0
Share

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

avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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(&quot;Fonts/Arial&quot;);

     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);
 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image badjano · Aug 01, 2012 at 05:45 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Resizing multiple GUI Textures to remain together 0 Answers

After Adding GUIStyle to GUI.Button, it is no longer clickable 1 Answer

Change GUI.Box color when click button 1 Answer

Changing background, font and style of buttons 1 Answer

Creating a GUI Button background texture 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges