Search Unity

Please Help to convert this Javascript to C#

Discussion in 'Scripting' started by T.A.G., Jan 7, 2014.

  1. T.A.G.

    T.A.G.

    Joined:
    Sep 17, 2013
    Posts:
    38
    Code (csharp):
    1. var theString : String = "";
    2. var theSkin : GUISkin;
    3. var The : GameObject;
    4.  
    5. function OnGUI() {
    6.  
    7. GUI.skin = theSkin;
    8.  
    9. GUI.TextField(Rect(100, 200, 200, 20), theString, 3);
    10. Word1();
    11. }
    12.  
    13.  
    14. static var lastChar : String = "";
    15.  
    16. function Word1() {
    17.     if (Event.current.isKey) {
    18.         if (lastChar != Event.current.character) {
    19.             lastChar = Event.current.character.ToString();
    20.  
    21.             switch (lastChar) {
    22.                 case ("a"): theString += "a"; break;
    23.                 case ("b"): theString += "b"; break;
    24.                 case ("c"): theString += "c"; break;
    25.                 case ("d"): theString += "d"; break;
    26.                 case ("e"): theString += "e"; break;
    27.                 case ("f"): theString += "f"; break;
    28.                 case ("g"): theString += "g"; break;
    29.                 case ("h"): theString += "h"; break;
    30.                 case ("i"): theString += "i"; break;
    31.                 case ("j"): theString += "j"; break;
    32.                 case ("k"): theString += "k"; break;
    33.                 case ("l"): theString += "l"; break;
    34.                 case ("m"): theString += "m"; break;
    35.                 case ("n"): theString += "n"; break;
    36.                 case ("o"): theString += "o"; break;
    37.                 case ("p"): theString += "p"; break;
    38.                 case ("q"): theString += "q"; break;
    39.                 case ("r"): theString += "r"; break;
    40.                 case ("s"): theString += "s"; break;
    41.                 case ("t"): theString += "t"; break;
    42.                 case ("u"): theString += "u"; break;
    43.                 case ("v"): theString += "v"; break;
    44.                 case ("w"): theString += "w"; break;
    45.                 case ("x"): theString += "x"; break;
    46.                 case ("y"): theString += "y"; break;
    47.                 case ("z"): theString += "z"; break;
    48.                 case ("A"): theString += "A"; break;
    49.                 case ("B"): theString += "B"; break;
    50.                 case ("C"): theString += "C"; break;
    51.                 case ("D"): theString += "D"; break;
    52.                 case ("E"): theString += "E"; break;
    53.                 case ("F"): theString += "F"; break;
    54.                 case ("G"): theString += "G"; break;
    55.                 case ("H"): theString += "H"; break;
    56.                 case ("I"): theString += "I"; break;
    57.                 case ("J"): theString += "J"; break;
    58.                 case ("K"): theString += "K"; break;
    59.                 case ("L"): theString += "L"; break;
    60.                 case ("M"): theString += "M"; break;
    61.                 case ("N"): theString += "N"; break;
    62.                 case ("O"): theString += "O"; break;
    63.                 case ("P"): theString += "P"; break;
    64.                 case ("Q"): theString += "Q"; break;
    65.                 case ("R"): theString += "R"; break;
    66.                 case ("S"): theString += "S"; break;
    67.                 case ("T"): theString += "T"; break;
    68.                 case ("U"): theString += "U"; break;
    69.                 case ("V"): theString += "V"; break;
    70.                 case ("W"): theString += "W"; break;
    71.                 case ("X"): theString += "X"; break;
    72.                 case ("Y"): theString += "Y"; break;
    73.                 case ("Z"): theString += "Z"; break;
    74.  
    75.                 default: break;
    76.             }
    77.  
    78.             var compString : String = "The";
    79.             var compString2 : String = "a";
    80.  
    81.             if (theString == compString) {
    82.                 The.GetComponent("TheGreen").enabled = true;
    83.                 }
    84.  
    85.             if (theString == compString2) {
    86.                 The.GetComponent("TheRed").enabled = true;
    87.             }
    88.  
    89.             if (Input.GetKeyDown("backspace")){
    90.                            // this is where I would like to delete one character at a time but dont know what script to use      
    91.  
    92.  
    93.              The.GetComponent("TheClear").enabled = true;
    94.             }
    95.  
    96.         }
    97.     }
    98. }
     
  2. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Nevermind the fact that that switch statement is completely useless :)
     
  4. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    Though, you might want to change that huge (and very repetitive) switch statement to something more sensible. Perhaps something like this:

    Code (csharp):
    1. string validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    2. if (validChars.Contains(lastChar))
    3. {
    4.    theString += lastChar;
    5. }