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 kmccmk9 · Sep 19, 2012 at 11:13 PM · scalesizebuttonsdynamicwindow

How can I dynamically changed where buttons are placed?

Hello, my buttons are using a custom style. I have been able to place them exactly where I want using code. However, when I changed the game window size the buttons of course were in the wrong spots and not scaled correctly. Is there any simple way to dynamically change the placement and scale of the buttons without using a bunch of if statements with different bounding rectangles for each? Thanks for any help.

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 chrisall76 · Sep 20, 2012 at 12:10 AM

Here's what I use, scales the GUI depending on the size of the window

 private var scale : Vector3;
 var originalWidth = 854.0;  // define here the original resolution
 var originalHeight = 480.0; // you used to create the GUI contents 

 function OnGUI (){
     scale.x = Screen.width/originalWidth; // calculate hor scale
     scale.y = Screen.height/originalHeight; // calculate vert scale
     scale.z = 1;
     var svMat = GUI.matrix; // save current matrix
     GUI.matrix = Matrix4x4.TRS(Vector3(0,0,0), Quaternion.identity, scale); // substitute matrix - only scale is altered from standard 
     
     //code here
     
     GUI.matrix = svMat; // restore matrix
 }
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 kmccmk9 · Sep 20, 2012 at 12:58 AM 0
Share

Thanks for the response. However I can't get my code to look like yours. If you could, check my reply for more information.

avatar image Bunny83 · Sep 27, 2012 at 12:41 AM 0
Share

This is the C# version of his answer:

     // [...]
     private Vector3 scale;
     public float originalWidth = 854.0f;
     public float originalHeight = 480.0f;
     
     void OnGUI ()
     {
         scale.x = Screen.width / originalWidth;
         scale.y = Screen.height / originalHeight;
         scale.z = 1.0f;
         var sv$$anonymous$$at = GUI.matrix;
         GUI.matrix = $$anonymous$$atrix4x4.TRS(Vector3(0,0,0), Quaternion.identity, scale);
     
         //code here
     
         GUI.matrix = sv$$anonymous$$at; // restore matrix
     }
 

Btw: yes, sv$$anonymous$$at is of type $$anonymous$$atrix4x4, however C# also supports type inference ;)

avatar image kmccmk9 · Sep 28, 2012 at 12:04 AM 0
Share

Okay if I post a link to my code, can you tell me why it doesn't work?

avatar image
0

Answer by kmccmk9 · Sep 20, 2012 at 12:57 AM

Okay thanks for the response. So here is my code as it stands. How would I adapt my code to work like yours. I understand your code I'm just having a hard time making mine look like yours.

 using UnityEngine;
 using System.Collections;
 
 public class MainMenuGUI : MonoBehaviour {
 
     //Variables
     public GUIStyle PlayButton;
     public GUIStyle OptionsButton;
     public GUIStyle InstructionsButton;
     public GUIStyle ExitButton;
     public Texture Background;
     bool doWindow0 = false;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         doWindow0 = false;
     }
     
     void OnGUI () {
         //Draw Background
         GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),Background,ScaleMode.StretchToFill, false, 0.0f);
         //Draw PlayButton
         if (GUI.Button(new Rect(30,180,235,590),"",PlayButton))
         {
             Application.LoadLevel("LoginScreen");
         }
         //Draw OptionsButton
         GUI.Button(new Rect(260,330,220,440),"",OptionsButton);
         //Draw InstructionsButton
         if (GUI.Button(new Rect(1160,330,145,440),"",InstructionsButton))
         {
             doWindow0 = true;
         }
         //Draw ExitButton
         if (GUI.Button(new Rect(1320,180,265,590),"",ExitButton))
         {
             Application.Quit();
         }
         if (doWindow0 == true)
             GUI.Window (0, new Rect(Screen.width/2-240,Screen.height/2-100,800,120), DoMyWindow, "Instructions:");
     }
     
     void DoMyWindow (int windowID) {
         GUI.Label (new Rect (10, 20, 800, 40), "Welcome to RedLightLife! Use WASD keys to move your character and use KL to rotate the camera. Use the J key to interact with people and objects. To have your character jump use the SPACEBAR. To pause your game, press the P key.");
         if (GUI.Button (new Rect (10,80,100,20), "Close"))
             doWindow0 = false;
     }
 }
Comment
Add comment · Show 5 · 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 chrisall76 · Sep 21, 2012 at 12:19 AM 0
Share

Sadly I don't know about converting UnityScript to C#

avatar image kmccmk9 · Sep 21, 2012 at 12:23 AM 0
Share

Ok okay. Thanks for the help. I will do my best to take your pseudo code and turn it into c#

avatar image Bunny83 · Sep 21, 2012 at 03:31 AM 0
Share

@kmccmk9: You did not post a "reply", you posted an answer. This is not a forum, this is a Q&A site. Answers should answer the question. when you want to improve your question, edit it.

Also chrisall76 didn't posted pseudo code. This is valid UnityScript code. Unityscript is Unity's Javascript-like language which can also be used besides C# and boo.

avatar image kmccmk9 · Sep 21, 2012 at 08:47 PM 0
Share

Oh okay thanks for the clarification.

avatar image kmccmk9 · Sep 26, 2012 at 10:25 PM 0
Share

What type should sv$$anonymous$$at be? $$anonymous$$atrix4x4?

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

11 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

Related Questions

use iPhoneUtils.PlayMovie. Change size? 0 Answers

Can you scale a Mesh independently of it's Collider? 1 Answer

Difference between sizes 1 Answer

Max scale in unity3d 2 Answers

How do you Scale Game Objects with the Screen? 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