• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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 alucardj · May 12, 2014 at 07:36 PM 0
Share

Your ShowLoginForm method has GUI functions, they need to be in OnGUI. Move 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 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

Related Questions

Touch drag 2D object in x axis 1 Answer

2D Animation does not start 0 Answers

Prevent player from moving in a certain direction while moving in another 1 Answer

Detecting when the other player presses a button 1 Answer

How to make realistic 2D balloon physics 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges