Search Unity

buttongetkey

Discussion in 'Immediate Mode GUI (IMGUI)' started by Rbert, Sep 16, 2014.

  1. Rbert

    Rbert

    Joined:
    Jul 13, 2014
    Posts:
    28
    can you help me to code this is getkey ..because i want to use keyboard..my game is like a 4 pics 1 word..
    Code (CSharp):
    1.  
    2. public class play : MonoBehaviour {
    3.     public Texture[] icon;
    4.     public string answer;
    5.     public string x = "";
    6.     public ArrayList Let = new ArrayList ();
    7.     public ArrayList letters = new ArrayList ();
    8.     public string l = "QWERTYUIOPASDFGHJKLZXCVBNM";
    9.     public string c = "";
    10.     public string[] files;
    11.     public ArrayList exampleFiles;
    12.     public string whatToGuess;
    13.     public ArrayList isAvailable;
    14.     public ArrayList toBeRemoved;
    15.     //file
    16.     public string word;
    17.     public string tagalog;
    18.     public string english;
    19.     public bool hasRun = false;
    20.     public bool[] completed = new bool[5];
    21.     public bool fin;
    22.     public bool nahulaan=false;
    23.     void OnGUI () {
    24.        
    25.         try
    26.         {
    27.             if(GUI.Button(new Rect(500,420,50,20), "Next")) {
    28.                 Application.LoadLevel(0);
    29.             }
    30.             if (GUI.Button (new Rect (100,50,130,130),new GUIContent (icon[0])))
    31.             {
    32.                
    33.             }
    34.             if (GUI.Button (new Rect (250,50,130,130),new GUIContent (icon[1])))
    35.             {
    36.                
    37.             }
    38.             if (GUI.Button (new Rect (400,50,130,130),new GUIContent (icon[2])))
    39.             {
    40.                
    41.             }
    42.             if (GUI.Button (new Rect (550,50,130,130),new GUIContent (icon[3])))
    43.             {
    44.                
    45.             }
    46.            
    47.            
    48.             if(fin==false)
    49.             {
    50.                 for (int i=0; i<6; i++) {
    51.                     if (isAvailable [i] == "1") {
    52.                         if ((GUI.Button (new Rect (300 + ((i % 6) * 40), 330, 30, 30), "" + letters [i])) && (Let.Count < answer.Length)) {
    53.                             Let.Add (letters [i]);
    54.                             isAvailable [i] = 0;
    55.                             toBeRemoved.Add (i);
    56.                         }
    57.                     }
    58.                     else
    59.                     {
    60.                         GUI.Box (new Rect (300 + ((i % 6) * 40), 330, 30, 30), "");
    61.                     }
    62.                 }
    63.                
    64.                 for (int i=6; i<12; i++) {
    65.                     if (isAvailable [i] == "1") {
    66.                         if ((GUI.Button (new Rect (300 + ((i % 6) * 40), 370, 30, 30), "" + letters [i])) && (Let.Count < answer.Length)) {
    67.                             Let.Add (letters [i]);
    68.                             isAvailable [i] = 0;
    69.                             toBeRemoved.Add (i);
    70.                         }
    71.                     }
    72.                     else
    73.                     {
    74.                         GUI.Box (new Rect (300 + ((i % 6) * 40), 370, 30, 30), "");
    75.                     }
    76.                    
    77.                 }
    78.                 for (int i=0; i<answer.Length; i++) {
    79.                     if (i < 6) {
    80.                         GUI.Box (new Rect (300 + ((i % 6) * 40), 280, 30, 30), "");
    81.                     } else if ((i >= 6) && (i < 12)) {
    82.                         GUI.Box (new Rect (300 + ((i % 6) * 40), 310, 30, 30), "");
    83.                     }
    84.                 }
    85.                 if ((GUI.Button (new Rect (540, 280, 30, 30), "<"))&&(nahulaan==false)) {
    86.                     if (Let.Count > 0) {
    87.                         Let.RemoveAt (Let.Count - 1);
    88.                         isAvailable [(int)toBeRemoved [toBeRemoved.Count - 1]] = "1";
    89.                         toBeRemoved.RemoveAt (toBeRemoved.Count - 1);
    90.                     }                  
    91.                 }
    92.                
    93.                 x = "";
    94.                 for (int i=0; i<Let.Count; i++) {
    95.                     if (i < 6) {
    96.                         GUI.Box (new Rect (300 + ((i % 6) * 40), 280, 30, 30), "" + Let [i]);
    97.                     } else if ((i >= 6) && (i < 12)) {
    98.                         GUI.Box (new Rect (300 + ((i % 6) * 40), 310, 30, 30), "" + Let [i]);
    99.                     }
    100.                    
    101.                     x = x + Let [i];
    102.                 }
    103.                 if ((x == answer)&&(nahulaan==false)) {
    104.                    
    105.                     string _strDBName = "URI=file:"+Application.dataPath+"/dbfile";
    106.                    
    107.                     IDbConnection _connection = new SqliteConnection(_strDBName);
    108.                     IDbCommand _command = _connection .CreateCommand();
    109.                     string sql;
    110.                    
    111.                     _connection.Open();
    112.                    
    113.                     sql = "CREATE TABLE IF NOT EXISTS words (word VARCHAR(20), tagalog VARCHAR(50), english VARCHAR(50))";
    114.                     _command.CommandText = sql;
    115.                     _command.ExecuteNonQuery();
    116.                    
    117.                     _command.CommandText = "INSERT INTO answered VALUES ('"+answer+"')";
    118.                     _connection.Open ();
    119.                     _command.ExecuteNonQuery();
    120.                     _connection.Close();
    121.                     nahulaan = true;
    122.                     x = "";  
    123.                 }
    124.                 if(nahulaan==true)
    125.                 {
    126.                     //DITO MO ILAGAY CODES PAG TUMAMA!!
    127.                     GUI.Box (new Rect (600, 280, 100, 30), "CORRECT!");
    128.                     GUI.Box (new Rect (600, 320, 200, 50), tagalog);
    129.                     GUI.Box (new Rect (600, 420, 200, 50), english);
    130.                 }
    131.             }
    132.            
    133.            
    134.         }
    135.         catch(IOException errr)
    136.         {
    137.             Debug.Log(errr.Message);
    138.         }
    139.        
    140.        
    141.        
    142.     }
    143.    
    144.     // Use this for initialization
    145.     void Start ()
    146.     {
    147.         completed = new bool[5];
    148.        
    149.         isAvailable = new ArrayList ();
    150.         toBeRemoved = new ArrayList ();
    151.         exampleFiles = new ArrayList ();
    152.        
    153.         string _strDBName = "URI=file:"+Application.dataPath+"/dbfile";
    154.        
    155.         IDbConnection _connection = new SqliteConnection(_strDBName);
    156.         IDbCommand _command = _connection .CreateCommand();
    157.         string sql;
    158.  
    159.         _connection.Open();
    160.        
    161.         sql = "CREATE TABLE IF NOT EXISTS words (word VARCHAR(20), tagalog VARCHAR(50), english VARCHAR(50))";
    162.         _command.CommandText = sql;
    163.         _command.ExecuteNonQuery();
    164.        
    165.         _command.CommandText = "SELECT * FROM words WHERE word not in (select word from answered) limit 1;";
    166.         _connection.Open ();
    167.         IDataReader _reader = _command.ExecuteReader ();
    168.         while (_reader.Read())
    169.         {
    170.             word = _reader.GetString(0);
    171.             tagalog = _reader.GetString(1);
    172.             english = _reader.GetString(2);
    173.         }          
    174.         _connection.Close ();
    175.        
    176.         for (int i=0; i<12; i++)
    177.             isAvailable.Add ("1");
    178.        
    179.         ArrayList lstExampleFiles = new ArrayList();
    180.         lstExampleFiles.AddRange( exampleFiles );
    181.         int d2 = Random.Range (0, lstExampleFiles.Count);
    182.         Debug.Log (word);
    183.         answer = word;
    184.         c = answer;
    185.         for (int i=0; i<c.Length; i++) {
    186.             letters.Add(c.Substring(i,1));
    187.         }
    188.         int rr = 0;
    189.         for (int i=letters.Count; i<12; i++) {
    190.             rr = Random.Range(0,l.Length);
    191.             letters.Add(l.Substring(rr,1));
    192.         }
    193.         letters = ShuffleList (letters);      
    194.         icon = new Texture[4];
    195.         for(int i=0; i<4;i++)
    196.         {
    197.             icon[i]= (Texture)Resources.Load(answer+"/"+(i+1));
    198.         }
    199.     }
    200.    
    201.     // Update is called once per frame
    202.     void Update () {
    203.        
    204.     }
    205.    
    206.     private ArrayList ShuffleList(ArrayList inputList)
    207.     {
    208.         ArrayList randomList = new ArrayList();
    209.        
    210.        
    211.         int randomIndex = 0;
    212.         while (inputList.Count > 0)
    213.         {
    214.             randomIndex = Random.Range (0, inputList.Count-1); //Choose a random object in the list
    215.             randomList.Add(inputList[randomIndex]); //add it to the new, random list
    216.             inputList.RemoveAt(randomIndex); //remove to avoid duplicates
    217.         }
    218.        
    219.         return randomList; //return the new random list
    220.     }
    221. }