• 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 Brastik · Mar 22, 2013 at 04:26 AM · guitexturebuttonrelativefit

Make GUI Button's size fit texture?

Hi, i'm new to Unity and now having some troubles with making GUI stuffs. I want to make a menu consisting of some simple buttons aligned center of the menu. I don't want to fix the position or size of buttons (like setting it x:20, y:20, w:200, h:50 or something like that). All I want is to make size of the buttons and box and any further controls I may add fit the textures given. By now I only found some examples which use fixed width, height for buttons/boxes. Anyone have a solution for this? Sorry if i'm not making it clear enough, i'm not good at expressing things :(

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

2 Replies

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

Answer by robertbu · Mar 22, 2013 at 04:57 AM

Just use the texture width and height when you specify the button. Example:

 var tex : Texture;
 
 function OnGUI() {
     if (GUI.Button(Rect(50,50,tex.width, tex.height), tex))
         Debug.Log("Button Pressed");
 }

Or maybe what you have more in mind is the GUILayout class. You can use GUILayout.Button() instead of GUI.Button().

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 Brastik · Mar 22, 2013 at 06:09 AM 0
Share

Ahh I got the first idea. So I will need to load the texture using Resources.Load("myTexture") is that right? About GUILayout class, yes I'm actually using GUILayout.Button() ins$$anonymous$$d of GUI.Button() but I set its texture in a GUISkin then write it like this GUI.skin = $$anonymous$$yGUISkin; which doesn't seem to work as the button's size scaled down to fit its text.

avatar image Brastik · Mar 22, 2013 at 06:44 AM 0
Share

UPDATED: it works with the skin now. +1 for reminding me to get the texture's width and height :D Ty so much!

avatar image
0

Answer by Brastik · Mar 22, 2013 at 07:03 AM

I get it working with the GUISkin by retrieving the width and height of the button's texture that I attached in the skin. In details:

GUISkin MenuSkin = (GUISkin)Resources.Load ("MenuSkin");

GUI.skin = MenuSkin;

GUILayout.BeginArea (new Rect (0, 0, Screen.width, Screen.height));

int buttonNormalWidth = MenuSkin.button.normal.background.width;

int buttonNormalHeight = MenuSkin.button.normal.background.height;

GUILayoutOption[] options = {GUILayout.Width(buttonNormalWidth),GUILayout.Height(buttonNormalHeight)};

GUILayout.Button ("Game",options);

GUILayout.EndArea ();

Hope that helps someone :D

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

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

Make more buttons appear, on button click. 1 Answer

Null reference exception[SOLVED] 1 Answer

GUI texture to fit the screen at any resolution? 4 Answers

Button Texture Issues?? 1 Answer

proper inventory system issue.. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges