Author Topic: .cs questions ngui  (Read 2006 times)

droptopgames

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
.cs questions ngui
« on: February 09, 2014, 03:42:57 PM »
Hello, i watched the tutorial video, still didn't really see my answer. I have created, and bunch already with ngui, but, integration on forms type gui has got me a little stumped. I have a basic login username and password and login button, ive created the ngui for login system. Original code was off unity gui, so just wondering what i need to add and remove to get this working. Just need to know per say how to get login input working with .cs then i'll have a structure to follow.

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. public class LoginUmaUI : MonoBehaviour
  7. {
  8.         public GUISkin skin;
  9.         public GameObject soundMenu;
  10.         public GameObject musicObject;
  11.  
  12.         enum LoginState
  13.         {
  14.                 Login,
  15.                 Register,
  16.                 Authenticating,
  17.                 CharacterSelect,
  18.                 CharacterCreate
  19.         }
  20.        
  21.         enum CreationState
  22.         {
  23.                 Body,
  24.                 Head,
  25.                 Face,
  26.                 Hair
  27.         }
  28.        
  29.         #region fields
  30.         LoginState loginState;
  31.         CreationState creationState;
  32.        
  33.         // Login fields
  34.         string username = "";
  35.         string password = "";
  36.        
  37.         // Registration fields
  38.         string password2 = "";
  39.         string email = "";
  40.         string email2 = "";
  41.        
  42.         // Character select fields
  43.         CharacterEntry characterSelected = null;
  44.         string characterName = "";
  45.         string race = "Human";
  46.         string gender = "Male";
  47.        
  48.         string dialogMessage = "";
  49.         string errorMessage = "";
  50.        
  51.         #endregion fields
  52.  
  53.         // Use this for initialization
  54.         void Start ()
  55.         {
  56.                 loginState = LoginState.Login;
  57.                 EventSystem.RegisterEvent("LOGIN_RESPONSE", this);
  58.                 EventSystem.RegisterEvent("REGISTER_RESPONSE", this);
  59.  
  60.                 // Play music
  61.                 SoundSystem.LoadSoundSettings();
  62.                 SoundSystem.PlayMusic(musicObject.GetComponent<AudioSource>());
  63.         }
  64.        
  65.         // Update is called once per frame
  66.         void Update ()
  67.         {
  68.        
  69.         }
  70.        
  71.         void OnGUI ()
  72.         {
  73.                 GUI.skin = skin;
  74.                
  75.                 if (errorMessage != "" || dialogMessage != "") {
  76.                         GUI.enabled = false;
  77.                 }
  78.                
  79.                 if (loginState == LoginState.Login || loginState == LoginState.Authenticating) {
  80.                         DrawLoginUI();
  81.                         //DrawSoundMenuButton();
  82.                 } else if (loginState == LoginState.Register) {
  83.                         DrawRegisterUI();
  84.                 }
  85.                
  86.                 GUI.enabled = true;
  87.                
  88.                 if (errorMessage != "") {
  89.                         DrawErrorUI();
  90.                 } else if (dialogMessage != "") {
  91.                         DrawDialogUI();
  92.                 }
  93.         }
  94.        
  95.         public void OnEvent(EventData eData) {
  96.                 if (eData.eventType == "LOGIN_RESPONSE") {
  97.                         dialogMessage = "";
  98.                         if (eData.eventArgs[0] == "Success") {
  99.                                 Application.LoadLevel("UMA Character Scene");
  100.                         } else {
  101.                                 errorMessage = eData.eventArgs[0];
  102.                         }
  103.                 } else if (eData.eventType == "REGISTER_RESPONSE") {
  104.                         dialogMessage = "";
  105.                         if (eData.eventArgs[0] == "Success") {
  106.                                 loginState = LoginState.Login;
  107.                                 errorMessage = "Account created. You can now log in";
  108.                         } else {
  109.                                 errorMessage = eData.eventArgs[0];
  110.                         }
  111.                 }
  112.         }
  113.        
  114.         Rect CreateCenteredRect (int width, int height)
  115.         {
  116.                 int left = (Screen.width - width) / 2;
  117.                 int top = (Screen.height - height) / 2;
  118.                 Rect centeredRect = new Rect (left, top, width, height);
  119.                 return centeredRect;
  120.         }
  121.        
  122.         void DrawLoginUI ()
  123.         {
  124.                 GUILayout.BeginArea(CreateCenteredRect (220, 180), skin.GetStyle("Window"));
  125.                 GUILayout.Label ("Username:");
  126.                 username = GUILayout.TextField (username);
  127.                 GUILayout.Label("Password:");
  128.                 password = GUILayout.PasswordField(password, '*');
  129.                 GUILayout.BeginHorizontal();
  130.                 if (GUILayout.Button("Login")) {
  131.                         dialogMessage = "Logging in...";
  132.                         // Verify username and password are entered, then pass them to the client
  133.                         // Also convert password to md5
  134.                         Client.Instance.Login(username, Encryption.Md5Sum(password));
  135.                 }
  136.  
  137.  
  138.                 GUILayout.EndHorizontal();
  139.                 GUILayout.EndArea();
  140.         }
  141.        
  142.        
  143.  
  144.        
  145.        
  146.         private bool ValidateEmail (string email)
  147.         {
  148.                 Regex regex = new Regex (@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
  149.                 Match match = regex.Match (email);
  150.                 if (match.Success)
  151.                         return true;
  152.                 else
  153.                         return false;
  154.         }
  155.         #endregion
  156.        
  157.         void ShowDialog(string message, bool showButton) {
  158.                 if (showButton) {
  159.                         errorMessage = message;
  160.                         dialogMessage = "";
  161.                 } else {
  162.                         dialogMessage = message;
  163.                         errorMessage = "";
  164.                 }
  165.                 /*dialogUI.SetActive(true);
  166.                 dialogString.text = message;
  167.                 dialogButton.SetActive(showButton);*/
  168.         }
  169. }

Im sure all the GUILayout stuff needs to go

  1. GUILayout.BeginArea(CreateCenteredRect (220, 180), skin.GetStyle("Window"));
  2.                 GUILayout.Label ("Username:");
  3.                 username = GUILayout.TextField (username);
  4.                 GUILayout.Label("Password:");
  5.                 password = GUILayout.PasswordField(password, '*');
  6.                 GUILayout.BeginHorizontal();
  7.                 if (GUILayout.Button("Login")) {
  8.                         dialogMessage = "Logging in...";
  9.                         // Verify username and password are entered, then pass them to the client
  10.                         // Also convert password to md5
  11.                         Client.Instance.Login(username, Encryption.Md5Sum(password));
  12.  


Kept really only the important parts of cs in pursuit of and quick explanation.

So do i just need the Ngui input fields to send there username and password strings over to my .cs?

What is needed for me to get my username and password strings from ngui over to my .cs source code file

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: .cs questions ngui
« Reply #1 on: February 09, 2014, 04:51:33 PM »
On the input field there is an On Submit field you can set in inspector. Set it to some function you want to call, such as the one you created earlier:
  1. public void LogMeIn ()
  2. {
  3.     Debug.Log(UIInput.current.value);
  4. }
If you have both username and password, it makes sense to set the On Submit function on the password field.

If you need to retrieve another input's text value, you should reference the input fields in your script like so:
  1. public class LoginUmaUI : MonoBehaviour
  2. {
  3.     public UIInput username;
  4.     public UIInput password;
This way you have a reference to them in your On Submit function:
  1. public void LogMeIn ()
  2. {
  3.     Client.Instance.Login(username.value, Encryption.Md5Sum(password.value));
  4. }

droptopgames

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: .cs questions ngui
« Reply #2 on: February 09, 2014, 08:31:03 PM »
 Thanks so much your pointers were exactly what i needed to fix this problem, been using unity gui, was hard to just do away with so much, but so much better now with ngui ;D

Also does same principle apply if i want to Login from button after i enter username and pw? just use button onSubmit?
« Last Edit: February 09, 2014, 08:38:27 PM by droptopgames »

droptopgames

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: .cs questions ngui
« Reply #3 on: February 09, 2014, 10:03:12 PM »
Yup, thanks great system