Author Topic: How can i convert this code into Ngui? Login System GUI to NGUI  (Read 8594 times)

Unity3dNoob

  • Guest
How can i convert this code into Ngui? Login System GUI to NGUI
« on: September 23, 2013, 02:42:11 AM »
I found a example of a login, is code in C# the problem is that is using GUILayout, and i want to replace it with UILabel, how do i call it? i Already made the NGUI Inputs that has it owns UILabels as "InpUsername", "InpName", "InpPassword", "InpRepass"

  1.  UILabel LUsername = GetComponent<UILabel>(); user = LUsername.text(user);

I get error mgs: (20,42): error CS1955: The member `UILabel.text' cannot be used as method or delegate




This the C# code.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class database : MonoBehaviour
  5. {
  6.     public static string user = "", name = "";
  7.     private string password = "", rePass = "", message = "";
  8.  
  9.     private bool register = false;
  10.  
  11.     private void OnGUI()
  12.     {
  13.         if (message != "")
  14.             GUILayout.Box(message);
  15.  
  16.         if (register)
  17.         {
  18.  
  19.             UILabel LUsername = GetComponent<UILabel>();
  20.             user = LUsername.text(user);
  21.  
  22.             GUILayout.Label("Name");
  23.             name = GUILayout.TextField(name);
  24.             GUILayout.Label("password");
  25.             password = GUILayout.PasswordField(password, "*"[0]);
  26.             GUILayout.Label("Re-password");
  27.             rePass = GUILayout.PasswordField(rePass, "*"[0]);
  28.  
  29.             GUILayout.BeginHorizontal();
  30.  
  31.             if (GUILayout.Button("Back"))
  32.                 register = false;
  33.  
  34.             if (GUILayout.Button("Register"))
  35.             {
  36.                 message = "";
  37.  
  38.                 if (user == "" || name == "" || password == "")
  39.                     message += "Please enter all the fields \n";
  40.                 else
  41.                 {
  42.                     if (password == rePass)
  43.                     {
  44.                         WWWForm form = new WWWForm();
  45.                                                 //form.AddField("email", email);
  46.                         form.AddField("user", user);
  47.                         form.AddField("name", name);
  48.                         form.AddField("password", password);
  49.                         WWW w = new WWW("http://", form);
  50.                         StartCoroutine(registerFunc(w));
  51.                     }
  52.                     else
  53.                         message += "Your Password does not match \n";
  54.                 }
  55.             }
  56.  
  57.             GUILayout.EndHorizontal();
  58.         }
  59.         else
  60.         {
  61.             GUILayout.Label("User:");
  62.             user = GUILayout.TextField(user);
  63.             GUILayout.Label("Password:");
  64.             password = GUILayout.PasswordField(password, "*"[0]);
  65.  
  66.             GUILayout.BeginHorizontal();
  67.  
  68.             if (GUILayout.Button("Login"))
  69.             {
  70.                 message = "";
  71.  
  72.                 if (user == "" || password == "")
  73.                     message += "Please enter all the fields \n";
  74.                 else
  75.                 {
  76.                     WWWForm form = new WWWForm();
  77.                     form.AddField("user", user);
  78.                     form.AddField("password", password);
  79.                     WWW w = new WWW("http://", form);
  80.                     StartCoroutine(login(w));
  81.                 }
  82.             }
  83.  
  84.             if (GUILayout.Button("Register"))
  85.                 register = true;
  86.  
  87.             GUILayout.EndHorizontal();
  88.         }
  89.     }
  90.  
  91.     IEnumerator login(WWW w)
  92.     {
  93.         yield return w;
  94.         if (w.error == null)
  95.         {
  96.             if (w.text == "login-SUCCESS")
  97.             {
  98.                 print("WOOOOOOOOOOOOOOO!");
  99.             }
  100.             else
  101.                 message += w.text;
  102.         }
  103.         else
  104.         {
  105.             message += "ERROR: " + w.error + "\n";
  106.         }
  107.     }
  108.  
  109.     IEnumerator registerFunc(WWW w)
  110.     {
  111.         yield return w;
  112.         if (w.error == null)
  113.         {
  114.             message += w.text;
  115.         }
  116.         else
  117.         {
  118.             message += "ERROR: " + w.error + "\n";
  119.         }
  120.     }
  121. }
  122.  
  123.  
« Last Edit: September 23, 2013, 07:28:30 AM by Unity3dNoob »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How can i convert this code into Ngui? Login System GUI to NGUI
« Reply #1 on: September 23, 2013, 07:24:37 AM »
So what are you trying to figure out then, if you've already created the layout? GUILayout lets you create a layout, and you've already done that. What's your question?

Unity3dNoob

  • Guest
Re: How can i convert this code into Ngui? Login System GUI to NGUI
« Reply #2 on: September 23, 2013, 07:31:53 AM »
Im not trying to use the Unity Gui, Im trying to Use NGUI, so instead of GUIlayout, i want to use NGUI Inputs, sothe script instead of grabbing the text from the GUILayout i want the script to get text from the UIInputs  UILabels

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How can i convert this code into Ngui? Login System GUI to NGUI
« Reply #3 on: September 23, 2013, 08:13:22 AM »
  1. using UnityEngine;
  2.  
  3. public class MyCustomScript : Monobehaviour
  4. {
  5.     public UIInput nameInput;
  6.     public UIInput passInput;
  7.     public UIInput passConfirmInput;
  8.     public UIButton loginButton;
  9.  
  10.     void Start()
  11.     {
  12.         EventDelegate.Set(loginButton.onClick, DoLogin);
  13.     }
  14.  
  15.     void DoLogin ()
  16.     {
  17.         Debug.Log("Logging in as " + nameInput.value + ", pass " + passInput.value);
  18.     }
  19. }

Unity3dNoob

  • Guest
Re: How can i convert this code into Ngui? Login System GUI to NGUI
« Reply #4 on: September 23, 2013, 08:47:40 AM »
Thank you so much for your help,  but i get two error msgs.


(13,9): error CS0103: The name `EventDelegate' does not exist in the current context

(18,50): error CS1061: Type `UIInput' does not contain a definition for `value' and no extension method `value' of type `UIInput' could be found (are you missing a using directive or an assembly reference?)

Do i need to write a script for EventDelegate?
« Last Edit: September 23, 2013, 09:25:38 AM by Unity3dNoob »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How can i convert this code into Ngui? Login System GUI to NGUI
« Reply #5 on: September 23, 2013, 04:17:05 PM »
You need to update to NGUI 3.0.

Unity3dNoob

  • Guest
Re: How can i convert this code into Ngui? Login System GUI to NGUI
« Reply #6 on: September 25, 2013, 09:29:18 PM »
Ok i upgraded, now i still can't use the script below with the script you gave, i want to type using the uiinput but in to make my script works that is the gui layou

I get error trying to use this code


using UnityEngine;

public class RegisterUI : MonoBehaviour
{
    public static string user = "", name = "";
    private string password = "", rePass = "", message = "";

    private bool register = false;

    public UIInput nameInput;
   public UIInput usernameInput;
    public UIInput passInput;
    public UIInput passConfirmInput;
    public UIButton loginButton;

    void Start()
    {
      if (message != "")
            message = UIInput.nameInput(message);

        if (register)
        {
         user = UIInput.nameInput(user);
         name = UIInput.nameInput(name);
         password = UIInput.nameInput(password);
         rePass = UIInput.nameInput(rePass);
         
          //if (GUILayout.Button("Register"))
            if (UIButton("Register"))
            {
                message = "";

                if (user == "" || name == "" || password == "")
                    message += "Please enter all the fields \n";
                else
                {
                    if (password == rePass)
                    {
                        WWWForm form = new WWWForm();
                  //form.AddField("email", email);
                        form.AddField("user", user);
                        form.AddField("name", name);
                        form.AddField("password", password);
                        WWW w = new WWW("http:", form);
                        StartCoroutine(registerFunc(w));
                    }
                    else
                        message += "Your Password does not match \n";
                }
       
   

    IEnumerator login(WWW w)
    {
        yield return w;
        if (w.error == null)
        {
            if (w.text == "login-SUCCESS")
            {
                print("WOOOOOOOOOOOOOOO!");
            }
            else
                message += w.text;
        }
        else
        {
            message += "ERROR: " + w.error + "\n";
        }
    }

 
            {

            if (UIButton("Login"))
            {
                message = "";

                if (user == "" || password == "")
                    message += "Please enter all the fields \n";
                else
                {
                    WWWForm form = new WWWForm();
                    form.AddField("user", user);
                    form.AddField("password", password);
                    WWW w = new WWW("http", form);
                    StartCoroutine(login(w));
                }
            }

            if (GUILayout.Button("Register"))
                register = true;
      
      }
       // EventDelegate.Set(loginButton.onClick, DoLogin);
    }

   // void DoLogin ()
  //  {
  //      Debug.Log("Logging in as " + usernameInput.value + ", pass " + passInput.value);
  //  }
« Last Edit: September 25, 2013, 09:46:15 PM by Unity3dNoob »

Unity3dNoob

  • Guest
Re: How can i convert this code into Ngui? Login System GUI to NGUI
« Reply #7 on: September 25, 2013, 11:14:55 PM »
I mean how can i rewrite the first login so my UIinput links to the script replace the GUIlayout Textfields?

valjohn

  • Guest
Re: How can i convert this code into Ngui? Login System GUI to NGUI
« Reply #8 on: October 15, 2013, 07:05:46 AM »
  1. using UnityEngine;
  2.  
  3. public class MyCustomScript : Monobehaviour
  4. {
  5.     public UIInput nameInput;
  6.     public UIInput passInput;
  7.     public UIInput passConfirmInput;
  8.     public UIButton loginButton;
  9.  
  10.     void Start()
  11.     {
  12.         EventDelegate.Set(loginButton.onClick, DoLogin);
  13.     }
  14.  
  15.     void DoLogin ()
  16.     {
  17.         Debug.Log("Logging in as " + nameInput.value + ", pass " + passInput.value);
  18.     }
  19. }




sir if this script would run. where will i be able to attach this script?

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: How can i convert this code into Ngui? Login System GUI to NGUI
« Reply #9 on: October 15, 2013, 08:40:24 AM »
Uhm, let me take a wild guess here and say:

1.  You need to learn how to code
2.  You need to learn how to post
3.  I don't think anyone is going to just outright give you a custom solution, and if they do... good for them I suppose.
4.  I get about 5 lines into that rewrite you did and see:

  1. if (message != "")
  2.             message = UIInput.nameInput(message);

Uhh... huh?  Message is initialized as "" - oh wait, see point 6 below, I get it now.

5.  If you found a login script on the web and it was written in UnityGUI/OnGUI/Whatever and want to use NGUI, then you're probably going to have rewrite most of it, and actually create the NGUI elements in the Unity Editor.
6.  Due to most of that code being originally made for UnityGUI, which updates like 4 times per frame, the whole logic behind it is different.  I think you renamed 'void OnGUI()' to 'void Start()'
7.  Once again, just read points 1-6.

If you want an easy, out of the box solution, either buy an asset, or you're going to have to take the time/effort to create it.  I could be wrong.

Thanks,
Alex

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How can i convert this code into Ngui? Login System GUI to NGUI
« Reply #10 on: October 16, 2013, 02:30:23 AM »
@valjohn: That script can go anywhere. Ideally some manager game object.

@Unity3dNoob: You need to learn the basics of both Unity and NGUI. Start by watching tutorials. Learn how to work with components. I can't teach people how to code here.