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
1
Question by cute999 · Oct 22, 2014 at 01:35 AM · guigui-button

GUI Button The BASICS.

I need some help guys. Correct me if I am wrong. By the way. I trying to create a small button in left bottom of the screen. I made a gui skin with a set up specific for that button. I set its normal background and active background. my problem is the center button is also using that skin. How do I prevent a center button to use the gui skin of left bottom button and set its own GUI skin with different sets of background? I was using an object and put a rigidbody so the onmouseup command will work. the problem is the unity keeps showing GUI warning mouse events

 public GUISkin mGui;
 
 float nativeWidth = 1080f;
 float nativeHeight = 1920f;
 
 void OnGUI()
     {  
         GUI.skin = mGui;
         float rx = Screen.width / nativeWidth;
         float ry = Screen.height / nativeHeight; 
         GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(ry, ry, 1)); 
         float adjustedWidth = nativeWidth * (rx / ry); 
         float adjustedHeight = nativeHeight * (ry / rx); 
 
 //Left Bottom
         if (GUI.Button (new Rect (10, adjustedHeight + 190, 100, 100), "")) {
             Debug.Log( "Clicked" );        
         }

              //center 
             if (GUI.Button (new Rect (adjustedWidth / 2, adjustedHeight /2 , 200, 100), "")) {
             Debug.Log( "Clicked" );        
         }
     }

alt text

untitled-2-compressed.jpg (21.4 kB)
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
2
Best Answer

Answer by Bunny83 · Oct 22, 2014 at 02:42 AM

You should only have one GUISkin and have all your styles in that one skin. Don't touch the default styles unless you want to change how a "normal", "standard" button looks like. For each "special" button like your facebook button you would create a custom style at the bottom of your skin.

In your Button code you have to tell the button which style you want to use:

 if (GUI.Button (new Rect (10, adjustedHeight + 190, 100, 100), "", "FacebookButton")){
     Debug.Log( "Clicked" );
 }
  
 //center
 if (GUI.Button (new Rect (adjustedWidth / 2, adjustedHeight /2 , 200, 100), "")) {
     Debug.Log( "Clicked" );
 }

In this case you would need a custom style named "FacebookButton". Just increase the count variable so you get a new custom style at the bottom. Set it up the way you want.

For more information take a look at my GUI crash course.

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 cute999 · Oct 22, 2014 at 06:12 AM 0
Share

thanks. I'll take a look on your gui crash course . btw if i must have one guiskin how do i change the texture on click.?

avatar image Bunny83 · Oct 22, 2014 at 01:10 PM 0
Share

@cute999:
I've added some more information to the GUIStyle section.

avatar image
2

Answer by Kiwasi · Oct 22, 2014 at 01:43 AM

Nicely phrased question. I see a few possible solutions to this.

  • Assign the skin after the start button has been rendered. Basically move line 8 and lines 15-19 to the end of you OnGUI function.

  • Assign the skin to null before rendering your start button.

  • Create a separate style for both buttons in your skin.

Its also worth considering moving to 4.6 and using the new UI. Its far simpler to work with then skins.

Comment
Add comment · Show 3 · 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 cute999 · Oct 22, 2014 at 06:09 AM 0
Share

is 4.6 ready for production use?

avatar image cute999 · Oct 22, 2014 at 06:15 AM 0
Share

also Am i doing this right? 'adjustedHeight + 190' in unity editor. the button is perfectly in the left bottom of the screen. going mobile. the button is not showing. I think it's off the screen, I don't know which one is right

avatar image Bunny83 · Oct 22, 2014 at 01:09 PM 0
Share

@cute999: To be honest i don't get what adjustedWidth and adjustedHeigh should actually represent. Since you scale the whole GUI you should do everything related to your nativeWidth and nativeHeight. Your rx and ry values are just scaling factors between the actual resolution and your virtual "native" resolution. Dividing one by the other just gives you some strange product of the two aspect ratios.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Enable and disable a button selection 3 Answers

A node in a childnode? 1 Answer

Unity 4.6 GUI vs Unity 4.5 GUI 1 Answer

Adding a texture to a GUI button 1 Answer

Limit on GUI Components? 0 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