Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Change Script JS to C#

Discussion in 'Scripting' started by EdgarCarrera, Aug 4, 2014.

  1. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    can u help me to swap this script to C#

    Code (CSharp):
    1. #pragma strict
    2. var username = "";
    3. var password = "";
    4. var repassword = "";
    5. var email = "";
    6. var message = "";
    7. var islogin = true;
    8. var isregister = false;
    9. var hdskin : GUISkin;
    10. var loguser = "";
    11. var logpass = "";
    12. var logmessage = "";
    13. var level2load = "";
    14. var isSuccess = false;
    15. var mainDomainName = "";
    16. var generatednumber :int = 0;
    17. var numberinput:int = 0;
    18. var loginPHPScriptName = "login.php";
    19. var registerPHPScriptName = "register.php";
    20. var HofBox = 25;
    21.  
    22. function OnGUI(){
    23.     GUI.skin = hdskin;
    24.     if(islogin == false && isregister == true){
    25.         GUI.Box(Rect((Screen.width)/2-250,20,500,480),"");
    26.         GUI.Label(Rect((Screen.width)/2-100,30,200,50),"Please fill out the fields");
    27.         GUI.Label(Rect((Screen.width)/2-230, 130,200,50),"Username:");
    28.         username = GUI.TextField(Rect((Screen.width)/2-20,130,200,HofBox),username,45);
    29.         GUI.Label(Rect((Screen.width)/2-230,190,200,50),"Password:");
    30.         password = GUI.PasswordField(Rect ((Screen.width)/2-20,190,200,HofBox),password,"*"[0],45);
    31.         GUI.Label(Rect((Screen.width)/2-230,250,200,50),"Re-Enter pass:");
    32.         repassword = GUI.PasswordField(Rect((Screen.width)/2-20,250,200,HofBox),repassword,"*"[0],45);
    33.         GUI.Label(Rect((Screen.width)/2-230,310,200,50),"Email:");
    34.         email = GUI.TextField(Rect ((Screen.width)/2-20,310,200,HofBox),email,45);
    35.         GUI.Label(Rect((Screen.width)/2-230,360,150,50),"Please verify:");
    36.         GUI.Label(Rect((Screen.width)/2-80,360,100,HofBox),generatednumber.ToString());
    37.         numberinput = int.Parse(GUI.TextField(Rect((Screen.width)/2+80,360,100,HofBox),numberinput.ToString()));
    38.         if(GUI.Button(Rect((Screen.width)/2-230,420,200,HofBox),"Register!")){
    39.             message = "";  
    40.             if(username == "" || email == "" || password == "" || numberinput == 0){
    41.                 message += "Please fill in the empty fields. \n";
    42.             }else{
    43.                 if(password == repassword){
    44.                     if(numberinput == generatednumber){
    45.                         doRegister();
    46.                     }else{
    47.                         message += "The number you inputed is not the same number.";
    48.                     }
    49.                    
    50.                 }else{
    51.                     message += "The passwords do not match. \n";
    52.                 }
    53.             }  
    54.         }
    55.         if(GUI.Button(Rect((Screen.width)/2-20,420,200,HofBox),"Return to login")){
    56.             islogin = true;
    57.             isregister = false;
    58.             username = ""; //cleaning our variables
    59.             password = "";
    60.             repassword = "";
    61.             email = "";
    62.             numberinput = 0;
    63.             message = "";
    64.             loguser = "";
    65.             logpass = "";
    66.             logmessage = "";
    67.         }
    68.         if(message != ""){
    69.             GUI.Label(Rect((Screen.width)/2-230,460,460,50), message);
    70.         }
    71.     }else if(islogin == true && isregister == false){
    72.             GUI.Box(Rect((Screen.width)/2-250,20,500,450),"");
    73.         GUI.Label(Rect((Screen.width)/2-100,30,200,50),"Please login to play");
    74.         GUI.Label(Rect((Screen.width)/2-230, 130,200,50),"Username:");
    75.         loguser = GUI.TextField(Rect((Screen.width)/2-20,130,200,HofBox),loguser,45);
    76.         GUI.Label(Rect((Screen.width)/2-230,230,200,50),"Password:");
    77.         logpass = GUI.PasswordField(Rect ((Screen.width)/2-20,230,200,HofBox),logpass,"*"[0],45);
    78.         if(GUI.Button(Rect((Screen.width)/2-230,330,200,HofBox),"Login")){
    79.             if(loguser == "" || logpass == ""){
    80.                 logmessage += "Please enter the necessary data! \n";
    81.             } else{
    82.                 logmessage = "";
    83.                 doLogin();
    84.             }
    85.         }
    86.         if(GUI.Button(Rect((Screen.width)/2-20,330,200,HofBox),"Register")){
    87.             islogin = false;
    88.             isregister = true;
    89.             generatednumber=Random.Range(1000,99999);
    90.             username = ""; //cleaning our variables
    91.             password = "";
    92.             repassword = "";
    93.             email = "";
    94.             numberinput = 0;
    95.             message = "";
    96.             loguser = "";
    97.             logpass = "";
    98.             logmessage = "";
    99.         }
    100.         if(logmessage != ""){
    101.             GUI.Label(Rect((Screen.width)/2-230,360,460,50),logmessage);
    102.         }
    103.     }
    104. }
    105. function doRegister(){
    106.     var form = new WWWForm();
    107.     form.AddField("user" , username);
    108.     form.AddField("pass" , password);
    109.     form.AddField("email" , email);          
    110.     var w = new WWW(mainDomainName + "/"+ registerPHPScriptName, form);          
    111.         yield w;
    112.     if(w.error == null){
    113.         message += w.text;
    114.     }else{
    115.         message += "Error : " + w.error + "\n";
    116.     }
    117. }
    118. function doLogin(){
    119.     var logform = new WWWForm();
    120.     logform.AddField("username" , loguser);
    121.     logform.AddField("password" , logpass);
    122.     var logw = new WWW(mainDomainName + "/"+ loginPHPScriptName, logform);
    123.         yield logw;
    124.     if(logw.error == null){
    125.         logmessage += logw.text;
    126.     }else{
    127.         logmessage +="Error : " + logw.error + "\n";  
    128.     }
    129.             if(logmessage == "Login success! Please wait while the game loads..."){
    130.                 isSuccess = true;
    131.             }
    132. }
    133. public function Update(){
    134.     if(isSuccess == true){
    135.         Application.LoadLevel(level2load);
    136.     }
    137. }
     
  2. WhoRainZone1

    WhoRainZone1

    Joined:
    Oct 6, 2013
    Posts:
    15
    I can help, but not do your work unfortunately. First you have to check which type those variables are and declare them in the way C# wants it.

    For example - 'username' is pretty sure going to be a string, same for the next 4 following variables:
    Code (csharp):
    1. string username = "";
    2. string password = "";
    3. ...
    But if you look at the next variables, we have 'islogin' which is a bool, and 'hdskin', a GUISkin. It would look like this:
    Code (csharp):
    1. bool islogin = true;
    2. GUISkin hdskin = new GUISkin();
    3. ...
    The next obvious step would be to make your functions like C# wants them to be. You have to replace 'function FunctionName()' with 'void FunctionName()', where void is our return type - in this case nothing. All Unity functions are using void as their return type, because they have no return value.

    When you've got that, it should be fixed already.. if you still get errors, you should be able to interpret them.

    If you then have any more errors, tell us where that error appears (you should be able to fix it yourself, as it'll be simple syntax mismatching, but you never know :))

    Cheers,
    WRZ
     
  3. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255

    I try and this is my code now i C# but dont work.. don load form. only show form but i press buttons and no works.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Connection : MonoBehaviour {
    5.    
    6.     public string username = "";
    7.     public string password= "";
    8.     public string repassword= "";
    9.     public string email= "";
    10.     public string message= "";
    11.     public bool islogin = true;
    12.     public bool isregister = false;
    13.     public GUISkin hdskin;
    14.     public  string loguser= "";
    15.     public string logpass= "";
    16.     public string logmessage= "";
    17.     public string level2load= "";
    18.     public  bool isSuccess = false;
    19.     public string mainDomainName= "";
    20.     public int generatednumber = 0;
    21.     public int numberinput = 0;
    22.     public string loginPHPScriptName= "login.php";
    23.     public string registerPHPScriptName= "register.php";
    24.     public int HofBox = 25;
    25.    
    26.     void  OnGUI (){
    27.         GUI.skin = hdskin;
    28.         if(islogin == false && isregister == true){
    29.             GUI.Box( new Rect((Screen.width)/2-250,20,500,480),"");
    30.             GUI.Label( new Rect((Screen.width)/2-100,30,200,50),"Please fill out the fields");
    31.             GUI.Label( new Rect((Screen.width)/2-230, 130,200,50),"Username:");
    32.             username = GUI.TextField( new Rect((Screen.width)/2-20,130,200,HofBox),username,45);
    33.             GUI.Label( new Rect((Screen.width)/2-230,190,200,50),"Password:");
    34.             password = GUI.PasswordField( new Rect((Screen.width)/2-20,190,200,HofBox),password,"*"[0],45);
    35.             GUI.Label( new Rect((Screen.width)/2-230,250,200,50),"Re-Enter pass:");
    36.             repassword = GUI.PasswordField( new Rect((Screen.width)/2-20,250,200,HofBox),repassword,"*"[0],45);
    37.             GUI.Label( new Rect((Screen.width)/2-230,310,200,50),"Email:");
    38.             email = GUI.TextField( new Rect((Screen.width)/2-20,310,200,HofBox),email,45);
    39.             GUI.Label( new Rect((Screen.width)/2-230,360,150,50),"Please verify:");
    40.             GUI.Label( new Rect((Screen.width)/2-80,360,100,HofBox),generatednumber.ToString());
    41.             numberinput = int.Parse(GUI.TextField( new Rect((Screen.width)/2+80,360,100,HofBox),numberinput.ToString()));
    42.             if(GUI.Button( new Rect((Screen.width)/2-230,420,200,HofBox),"Register!")){
    43.                 message = "";  
    44.                 if(username == "" || email == "" || password == "" || numberinput == 0){
    45.                     message += "Please fill in the empty fields. \n";
    46.                 }else{
    47.                     if(password == repassword){
    48.                         if(numberinput == generatednumber){
    49.  
    50.                             StopCoroutine("doRegister");
    51.                         }else{
    52.                             message += "The number you inputed is not the same number.";
    53.                         }
    54.                        
    55.                     }else{
    56.                         message += "The passwords do not match. \n";
    57.                     }
    58.                 }  
    59.             }
    60.             if(GUI.Button( new Rect((Screen.width)/2-20,420,200,HofBox),"Return to login")){
    61.                 islogin = true;
    62.                 isregister = false;
    63.                 username = ""; //cleaning our variables
    64.                 password = "";
    65.                 repassword = "";
    66.                 email = "";
    67.                 numberinput = 0;
    68.                 message = "";
    69.                 loguser = "";
    70.                 logpass = "";
    71.                 logmessage = "";
    72.             }
    73.             if(message != ""){
    74.                 GUI.Label( new Rect((Screen.width)/2-230,460,460,50), message);
    75.             }
    76.         }else if(islogin == true && isregister == false){
    77.             GUI.Box( new Rect((Screen.width)/2-250,20,500,450),"");
    78.             GUI.Label( new Rect((Screen.width)/2-100,30,200,50),"Please login to play");
    79.             GUI.Label( new Rect((Screen.width)/2-230, 130,200,50),"Username:");
    80.             loguser = GUI.TextField( new Rect((Screen.width)/2-20,130,200,HofBox),loguser,45);
    81.             GUI.Label( new Rect((Screen.width)/2-230,230,200,50),"Password:");
    82.             logpass = GUI.PasswordField( new Rect((Screen.width)/2-20,230,200,HofBox),logpass,"*"[0],45);
    83.             if(GUI.Button( new Rect((Screen.width)/2-230,330,200,HofBox),"Login")){
    84.                 if(loguser == "" || logpass == ""){
    85.                     logmessage += "Please enter the necessary data! \n";
    86.                 } else{
    87.                     logmessage = "";
    88.                     StopCoroutine("doLogin");
    89.                 }
    90.             }
    91.             if(GUI.Button( new Rect((Screen.width)/2-20,330,200,HofBox),"Register")){
    92.                 islogin = false;
    93.                 isregister = true;
    94.                 generatednumber=Random.Range(1000,99999);
    95.                 username = ""; //cleaning our variables
    96.                 password = "";
    97.                 repassword = "";
    98.                 email = "";
    99.                 numberinput = 0;
    100.                 message = "";
    101.                 loguser = "";
    102.                 logpass = "";
    103.                 logmessage = "";
    104.             }
    105.             if(logmessage != ""){
    106.                 GUI.Label( new Rect((Screen.width)/2-230,360,460,50),logmessage);
    107.             }
    108.         }
    109.     }
    110.     IEnumerator doRegister(){
    111.         WWWForm form = new WWWForm();
    112.         form.AddField("user" , username);
    113.         form.AddField("pass" , password);
    114.         form.AddField("email" , email);          
    115.         var w = new WWW(mainDomainName + "/"+ registerPHPScriptName, form);          
    116.         yield return w;
    117.         if(w.error == null){
    118.             message += w.text;
    119.         }else{
    120.             message += "Error : " + w.error + "\n";
    121.         }
    122.     }
    123.     public IEnumerator doLogin(){
    124.         WWWForm logform = new WWWForm();
    125.         logform.AddField("username" , loguser);
    126.         logform.AddField("password" , logpass);
    127.         var logw = new WWW(mainDomainName + "/"+ loginPHPScriptName, logform);
    128.         yield return logw;
    129.         if(logw.error == null){
    130.             logmessage += logw.text;
    131.         }else{
    132.             logmessage +="Error : " + logw.error + "\n";  
    133.         }
    134.         if(logmessage == "Login success! Please wait while the game loads..."){
    135.             isSuccess = true;
    136.         }
    137.     }
    138.     public void  Update (){
    139.         if(isSuccess == true){
    140.             Application.LoadLevel(level2load);
    141.         }
    142.     }
    143. }
     
  4. WhoRainZone1

    WhoRainZone1

    Joined:
    Oct 6, 2013
    Posts:
    15
  5. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    153
    keep in mind that in c# the default access modifier is private as opposed to public in js, so to expose fields to the inspector make them explicitly public, otherwise leave them private!!

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. namespace Assets.Scripts
    5. {
    6.   public class foo : MonoBehaviour
    7.   {
    8.     public string username = "";
    9.     public string password = "";
    10.     public string repassword = "";
    11.     public string email = "";
    12.     public string message = "";
    13.     public bool islogin = true;
    14.     public bool isregister = false;
    15.     public GUISkin hdskin;
    16.     string loguser = "";
    17.     string logpass = "";
    18.     string logmessage = "";
    19.     string level2load = "";
    20.     bool isSuccess = false;
    21.     string mainDomainName = "";
    22.     int generatednumber = 0;
    23.     int numberinput = 0;
    24.     string loginPHPScriptName = "login.php";
    25.     string registerPHPScriptName = "register.php";
    26.     int HofBox = 25;
    27.  
    28.     void OnGUI()
    29.     {
    30.       GUI.skin = hdskin;
    31.       if (islogin == false && isregister == true)
    32.       {
    33.         GUI.Box(new Rect((Screen.width) / 2 - 250, 20, 500, 480), "");
    34.         GUI.Label(new Rect((Screen.width) / 2 - 100, 30, 200, 50), "Please fill out the fields");
    35.         GUI.Label(new Rect((Screen.width) / 2 - 230, 130, 200, 50), "Username:");
    36.         username = GUI.TextField(new Rect((Screen.width) / 2 - 20, 130, 200, HofBox), username, 45);
    37.         GUI.Label(new Rect((Screen.width) / 2 - 230, 190, 200, 50), "Password:");
    38.         password = GUI.PasswordField(new Rect((Screen.width) / 2 - 20, 190, 200, HofBox), password, "*"[0], 45);
    39.         GUI.Label(new Rect((Screen.width) / 2 - 230, 250, 200, 50), "Re-Enter pass:");
    40.         repassword = GUI.PasswordField(new Rect((Screen.width) / 2 - 20, 250, 200, HofBox), repassword, "*"[0], 45);
    41.         GUI.Label(new Rect((Screen.width) / 2 - 230, 310, 200, 50), "Email:");
    42.         email = GUI.TextField(new Rect((Screen.width) / 2 - 20, 310, 200, HofBox), email, 45);
    43.         GUI.Label(new Rect((Screen.width) / 2 - 230, 360, 150, 50), "Please verify:");
    44.         GUI.Label(new Rect((Screen.width) / 2 - 80, 360, 100, HofBox), generatednumber.ToString());
    45.         numberinput = int.Parse(GUI.TextField(new Rect((Screen.width) / 2 + 80, 360, 100, HofBox), numberinput.ToString()));
    46.         if (GUI.Button(new Rect((Screen.width) / 2 - 230, 420, 200, HofBox), "Register!"))
    47.         {
    48.           message = "";
    49.           if (username == "" || email == "" || password == "" || numberinput == 0)
    50.           {
    51.             message += "Please fill in the empty fields. \n";
    52.           }
    53.           else
    54.           {
    55.             if (password == repassword)
    56.             {
    57.               if (numberinput == generatednumber)
    58.               {
    59.                 doRegister();
    60.               }
    61.               else
    62.               {
    63.                 message += "The number you inputed is not the same number.";
    64.               }
    65.  
    66.             }
    67.             else
    68.             {
    69.               message += "The passwords do not match. \n";
    70.             }
    71.           }
    72.         }
    73.         if (GUI.Button(new Rect((Screen.width) / 2 - 20, 420, 200, HofBox), "Return to login"))
    74.         {
    75.           islogin = true;
    76.           isregister = false;
    77.           username = ""; //cleaning our variables
    78.           password = "";
    79.           repassword = "";
    80.           email = "";
    81.           numberinput = 0;
    82.           message = "";
    83.           loguser = "";
    84.           logpass = "";
    85.           logmessage = "";
    86.         }
    87.         if (message != "")
    88.         {
    89.           GUI.Label(new Rect((Screen.width) / 2 - 230, 460, 460, 50), message);
    90.         }
    91.       }
    92.       else if (islogin == true && isregister == false)
    93.       {
    94.         GUI.Box(new Rect((Screen.width) / 2 - 250, 20, 500, 450), "");
    95.         GUI.Label(new Rect((Screen.width) / 2 - 100, 30, 200, 50), "Please login to play");
    96.         GUI.Label(new Rect((Screen.width) / 2 - 230, 130, 200, 50), "Username:");
    97.         loguser = GUI.TextField(new Rect((Screen.width) / 2 - 20, 130, 200, HofBox), loguser, 45);
    98.         GUI.Label(new Rect((Screen.width) / 2 - 230, 230, 200, 50), "Password:");
    99.         logpass = GUI.PasswordField(new Rect((Screen.width) / 2 - 20, 230, 200, HofBox), logpass, "*"[0], 45);
    100.         if (GUI.Button(new Rect((Screen.width) / 2 - 230, 330, 200, HofBox), "Login"))
    101.         {
    102.           if (loguser == "" || logpass == "")
    103.           {
    104.             logmessage += "Please enter the necessary data! \n";
    105.           }
    106.           else
    107.           {
    108.             logmessage = "";
    109.             doLogin();
    110.           }
    111.         }
    112.         if (GUI.Button(new Rect((Screen.width) / 2 - 20, 330, 200, HofBox), "Register"))
    113.         {
    114.           islogin = false;
    115.           isregister = true;
    116.           generatednumber = UnityEngine.Random.Range(1000, 99999);
    117.           username = ""; //cleaning our variables
    118.           password = "";
    119.           repassword = "";
    120.           email = "";
    121.           numberinput = 0;
    122.           message = "";
    123.           loguser = "";
    124.           logpass = "";
    125.           logmessage = "";
    126.         }
    127.         if (logmessage != "")
    128.         {
    129.           GUI.Label(new Rect((Screen.width) / 2 - 230, 360, 460, 50), logmessage);
    130.         }
    131.       }
    132.     }
    133.  
    134.    
    135.  
    136.     IEnumerator doRegister()
    137.     {
    138.       var form = new WWWForm();
    139.       form.AddField("user", username);
    140.       form.AddField("pass", password);
    141.       form.AddField("email", email);
    142.       var w = new WWW(mainDomainName + "/" + registerPHPScriptName, form);
    143.       yield return w;
    144.       if (w.error == null)
    145.       {
    146.         message += w.text;
    147.       }
    148.       else
    149.       {
    150.         message += "Error : " + w.error + "\n";
    151.       }
    152.     }
    153.  
    154.     IEnumerator doLogin()
    155.     {
    156.       var logform = new WWWForm();
    157.       logform.AddField("username", loguser);
    158.       logform.AddField("password", logpass);
    159.       var logw = new WWW(mainDomainName + "/" + loginPHPScriptName, logform);
    160.       yield return logw;
    161.       if (logw.error == null)
    162.       {
    163.         logmessage += logw.text;
    164.       }
    165.       else
    166.       {
    167.         logmessage += "Error : " + logw.error + "\n";
    168.       }
    169.       if (logmessage == "Login success! Please wait while the game loads...")
    170.       {
    171.         isSuccess = true;
    172.       }
    173.     }
    174.  
    175.     public void Update()
    176.     {
    177.       if (isSuccess == true)
    178.       {
    179.         Application.LoadLevel(level2load);
    180.       }
    181.     }
    182.  
    183.   }
    184. }
    185.