• 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 HHameline · Sep 15, 2011 at 12:42 PM · guibuttongui.buttoncenteringcustom style

Centering a texture in a GUI.Button

Hello, I'm trying to make a button with width and height of 585x400, in this button I want to center a 336x100 image for normal and one for hover. I'm doing it like this.

             GUIStyle buttonContentStyle  = new GUIStyle(guiSkin.GetStyle("myButton"));
             if(buttonImage != null)
             {
                 buttonContentStyle.normal.background = buttonImage[0];
                 buttonContentStyle.hover.background = buttonImage[1];
                 buttonContentStyle.fixedHeight = buttonImage[0].height;
                 buttonContentStyle.fixedWidth = buttonImage[0].width;
                 buttonContentStyle.alignment = TextAnchor.MiddleCenter;
             }                
             if(GUI.Button(new Rect(0,0,585,400), "", buttonContentStyle))
             {
             
             }


This just makes the image the correct size but aligns it to the top left, not to the center of the button. Any help regarding this issue would be most welcome.

Thanks, Hans

Comment
Add comment
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

4 Replies

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

Answer by jahroy · Sep 16, 2011 at 08:21 AM

According to the documentation the alignment property deals with text alignment.

I believe you might want to use the padding, margin, or contentOffset properties to align your image. You already know the width of the GUI element and the width of the image in your code.

GUIStyle also has an imagePosition property, but I've never figured that one out.

I would also recommend using a GUISkin in stead. It is very easy to assign a texture for each of the 8 button states using the Inspector (on and off: normal, hover, active, focused).

You can also change these images using code (like you already are). Here is the documentation for the normal property of a GUIStyle. As you can see, its type is GUIStyleState, which has two properties: a background image and text color. The onHover property would represent the background image and text color of a Toggle button that was turned on and has the mouse hovering over it.

You can also manipulate the border property of a GUIStyle to control how images will react if they are smaller than the GUI control they're used on.

For example: If you set the border to be (1, 1, 1, 1) the outermost pixel of the background image will remain unscaled at the perimeter of the control. The rest of the image (the inside) will stretch to fill the control.

I had a hard time finding this, but this thread from the forums covers how to use the border property to scale images on GUI controls.

It also shows you how to create your own custom GUISkin images.

Hope some of this helps.

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 Owen-Reynolds · Sep 16, 2011 at 03:02 PM 0
Share

The border numbers are when you want the image to stretch over the entire background, as usual, but an overall stretch looks bad.

Suppose you have a border image and the corners are rounded with little leafy things in them. You can set "borders" so the corners are always drawn "as is" (never stretched to long, skinny leaves) and only the middle, long lines, are stretched.

avatar image HHameline · Sep 17, 2011 at 05:46 PM 0
Share

Thanks for the help guys really appreciate it :D

avatar image
0

Answer by Litobyte_Softworks · Sep 15, 2011 at 01:05 PM

Give a look to the GUISkin component (Menu->Assets->Create->GUISkin), there are many parameters to make the button texture proper. From those parameters you have full control on how the gui button texture should behave/repeat/align.

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 HHameline · Sep 15, 2011 at 01:28 PM 0
Share

I have looked :/ the buttonContentStyle is allowing me to update the GUISkin's properties for the myButton style in my GUISkin. However The alignment aspect only works for the content of the button such as the string/texture on the button face but I can not find where the control for the normal/hover textures is. I'm not sure if it is even possible. I've been searching the forums and have yet to find another question like $$anonymous$$e as well. :/ Thanks for the comment though. I appreciate you taking the time to help.

avatar image jahroy · Sep 16, 2011 at 08:25 AM 0
Share

See my answer above/below about where to control the textures for the different button states.

There are many ways to approach the layout of your image.

Let me know if you have any questions after reading all the junk I wrote.

avatar image
0

Answer by squidbot · Sep 15, 2011 at 09:45 PM

I think what you want to do is not make the image part of the style, because the style is always used to fill the entire controls rectangle. Instead, you would want to set the image as the GUIContent and use the style content offsets to center the image.

Conversely, if you really want to use the style, you could just make your art the full size of the control and put a transparent border around it in the art itself.

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
0

Answer by Owen-Reynolds · Sep 16, 2011 at 07:08 AM

Setting OverFlow to negative seems to shrink the texture down and let the text hang over.

Squidbots's transparent border sounds like the more official way. "Everyone knows" that Button/GUI textures always fill the entire clickable area, even if they are a black border and transparent insides. That's probably why you haven't found a similar Q.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

(Unity 4.6) Button animations 1 Answer

multiple GUI.Buttons at the same time 1 Answer

Make more buttons appear, on button click. 1 Answer

GUI Button Not Displaying? 1 Answer

Toggling multiple button states 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