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 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 re$$anonymous$$ding 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.

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


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