Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 hollym16 · Oct 13, 2014 at 11:27 AM · cameraguitexturebuttonguitexture

Making GUI Buttons on a GUI Texture

I've got the following script on my Camera:

 using UnityEngine;
 using System.Collections;
 
 public class GUIDisplay : MonoBehaviour 
 {
     public Texture2D background;
     public GUISkin mySkin;
 
     void OnGUI()
     {
         GUI.skin = mySkin;
         GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),background);
         
                 }
             }

This allows me to assign a texture in the Inspector that adjusts to the screen size.

The texture I'm using is like a HUD and has text on it that I want to use as buttons. Is it best to use GUI.Button and try and mathematically line it up with the text, or is there another way?

I'm thinking that that way may be unreliable if the texture is adjusting to different screen sizes; the buttons would slide out of line right?

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

1 Reply

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

Answer by Srki94 · Oct 13, 2014 at 01:28 PM

I wouldn't suggest using fixed values. Instead of X pixels from edge, use percentage.

That way no matter what size of, for example, window you are using, all controls will stay on the same positions.

I'm thinking that that way may be unreliable if the texture is adjusting to different screen sizes; the buttons would slide out of line right?

Kind of. If you place button at 500 px from X and it looks good, then you resize your texture, button will be at same 500px position.

Here is a snippet of code that I used once to keep all components inside window at same position, no matter what size window is :

 GUI.Label(Rect(60.0/100*prozor.width, 9.6/100*prozor.height, 80, 50), "This is an label!");


Where "prozor" is actual GUI Window (read : Rect that Window uses). In your case you would probably want to use texture W & H instead.

This will allow you to keep all components at same percentage of window or texture, no matter what size that is. Also, if you resize that window or texture anytime at runtime, all components will follow.

Good luck !

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 hollym16 · Oct 13, 2014 at 02:17 PM 0
Share

Ah brilliant! Thanks so much for the help :)

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI.DrawTexture on GUI.Button press 1 Answer

Animate GUI Elements 1 Answer

How to create a button that doesnt render in the main camera? 2 Answers

My GUI elements are not rendering on top of all cameras. Any ideas why? 1 Answer

Assiging an Image to a Button 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