用Unity在Android中建立可以輸入使用者名稱的方法

views

目標功能

  • 使用者可以輸入自己名字
  • 輸入結束後顯示訊息 (只要退出手機的瑩幕鍵盤就會自動顯示)
  • 密碼功能

JS程式碼(Touch Screen Keyboard 手機的瑩幕鍵盤)

#pragma strict
var nameString : String = "enter your name here";
private var keypadOnMobile : TouchScreenKeyboard;

function OnGUI() 
{
    if (GUI.Button(Rect(0, 10, 200, 32), nameString))
    {
        keypadOnMobile = TouchScreenKeyboard.Open(nameString, TouchScreenKeyboardType.NamePhonePad);
    }
   
     if (keypadOnMobile)
     {
        nameString = keypadOnMobile.text; 
     }
     
     if (nameString=="Marc")
     if (keypadOnMobile.done)
     {
       GUI.Label(Rect(Screen.width/2,110,128,32),String.Format("Hi, {0}",nameString));
     }
     
     if (nameString!="Marc")
     if (keypadOnMobile.done)  
     {
       GUI.Label(Rect(Screen.width/2,110,128,32),String.Format("You idiot!"));
     }    
}
									

也可用GUI.Button做Log in功能

function OnGUI()
{
  stringToEdit = GUI.TextField (Rect(10,20,200,30), stringToEdit,25);
  
  if(GUI.Button (Rect(10,50,128,32),"OK"))
  {
    if (stringToEdit=="Marc") enableText=true;
  }
  
  if(enableText==true) 
  {
    GUI.Label(Rect(Screen.width/2,Screen.height/2,128,32),String.Format("Hi, {0}",stringToEdit));
  }
}
									

更多TouchScreenKeyboard相關參數

註:此Log in功能只是local功能 無上傳到server / database的功能

註:TouchScreenKeyboard相關功能只能在手機上正常使用

註:可加入PlayerPrefs就可做出儲存功能

iOS應該也可用 但沒測過

This entry was posted in GUI, Unity Android, Unity小技巧 and tagged by Mr.M. Bookmark the permalink.


Leave a Reply

Your email address will not be published. Required fields are marked *