• 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 w$$anonymous$$ch 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 t$$anonymous$$s 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 ();
     }
 
 }

T$$anonymous$$s 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

1 Reply

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

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

Try t$$anonymous$$s OnGUI function, you just use your booleans to verify w$$anonymous$$ch form to show. T$$anonymous$$s 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

How to execute action with two keys when they are alredy tied to other actions? 2 Answers

Using UI Button to trigger button down?? 0 Answers

How to make binary array toggle,How to make binary array switch/toggle? 0 Answers

How to play animations on entering a trigger and pressing a button? (2D) 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