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 GeneralBrae · May 12, 2014 at 07:19 PM · 2dinputinterfaceunitygui

Making dynamic login screen

Hi,

I'm a complete newcomer to unity, but I'm not a bad programmer and I've done a fair bit of work with 3D modelling so you don't need to be too gentle :)

As an 'intro' project, I am making a little client for a javascript based webgame. I want to make a login screen, where it shows a background image and has 2 buttons on it, Login and Sign Up. When one of these is clicked, I want the buttons to disappear and a form to appear, either to sign up or to login.

I have the buttons and background set up, but could someone point me towards a way to switch the components nicely? At the moment I have the following code which makes the buttons vanish, but no way to create the new form. I get the feeling I should be making prepared sections that I can swap in and out, but not really any idea where to start.

 public class GUITest : MonoBehaviour {
 public Texture btnTexture;
 private bool showButtons = true;
 private bool showLogin = false;

 // Use this for initialization
 void Start () {
 
 }

 void OnGUI () {
     if (!btnTexture) {
         Debug.LogError("Please assign a texture on the inspector");
         return;
     }

     if (showButtons) {

         // Make the first button. 
         if (GUI.Button (new Rect ((Screen.width / 2) - (btnTexture.width / 2), (Screen.height / 2) - 100, btnTexture.width, btnTexture.height), btnTexture, GUIStyle.none)) {
             Debug.Log ("Clicked the login button");
             showButtons = false;
             showLogin = true;
         }
     
         // Make the second button.
         if (GUI.Button (new Rect ((Screen.width / 2) - (btnTexture.width / 2), (Screen.height / 2) - 40, btnTexture.width, btnTexture.height), btnTexture, GUIStyle.none)) {
             Debug.Log ("Clicked the sign up button");
             showButtons = false;
         }
     }
 }
 
 // Update is called once per frame
 void Update () {
     if (showLogin) {
         ShowLoginForm ();
     }
 
 }

This is to show the login form. Doesn't work at the moment as apparently I can only call GUI elements from onGUI, but here it is to give an idea of what I'm aiming at.

 void ShowLoginForm() {
     Debug.Log ("Creating form");
     int centreX = Screen.width/2;
     int centreY = Screen.height/2;
     GUI.Box(new Rect(centreX-150, centreY-100, 300, 400), "Login");
     Debug.Log ("Created box");
     GUI.Label (new Rect (centreX - 80, (centreY - 100 + 25), 160, 50), "Username");
 }

Ideally I would be looking to add transition effects to the change from buttons to form but that can come later.

Thanks!

Comment
Add comment · Show 1
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 AlucardJay · May 12, 2014 at 07:36 PM 0
Share

Your ShowLoginForm method has GUI functions, they need to be in OnGUI. $$anonymous$$ove the condition in your Update to OnGUI (and remove the Debugs, they will print too many times in OnGUI!).

1 Reply

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

Answer by Vitor_r · May 12, 2014 at 08:43 PM

Try this OnGUI function, you just use your booleans to verify which form to show. This can become problematic if u put a lot more "forms".

 void OnGUI () {
 if (!btnTexture) {
     Debug.LogError("Please assign a texture on the inspector");
         return;
     }
  
     if (showButtons) {
         // Make the first button.
         if (GUI.Button (new Rect ((Screen.width / 2) - (btnTexture.width / 2), (Screen.height / 2) - 100, btnTexture.width, btnTexture.height), btnTexture, GUIStyle.none)) {
             Debug.Log ("Clicked the login button");
             showButtons = false;
             showLogin = true;
         }
      
         // Make the second button.
         if (GUI.Button (new Rect ((Screen.width / 2) - (btnTexture.width / 2), (Screen.height / 2) - 40, btnTexture.width, btnTexture.height), btnTexture, GUIStyle.none)) {
             Debug.Log ("Clicked the sign up button");
             showButtons = false;
         }
     }else if(showLogin){
         Debug.Log ("Creating form");
         int centreX = Screen.width/2;
         int centreY = Screen.height/2;
         GUI.Box(new Rect(centreX-150, centreY-100, 300, 400), "Login");
         Debug.Log ("Created box");
         GUI.Label (new Rect (centreX - 80, (centreY - 100 + 25), 160, 50), "Username");
     }
 }

Also, follow the advice of alucardj and remove the "Debug.Log()" :)

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 GeneralBrae · May 12, 2014 at 09:40 PM 0
Share

Thanks very much :)

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

22 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

Related Questions

Divide screen in half for two buttons 1 Answer

2D rotation around hinge joint with joystick input 0 Answers

How to play animation after pressing return 3 times ? 1 Answer

New input system controller stops detecting on fully pressed trigger? 0 Answers

Bad result of changing RectTransform values (Anchor Preset) programmatically 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