GUI
usingUnityEngine;
usingSystem.Collections;
usingSystem.Collections.Generic;
publicclassGameControllerScript:MonoBehaviour{
publicTextureimage;
private string accountText="";
private string passwordText="";
private string account="账号";
private string password="密码";
private Dictionary accountAndpassword;
voidAwake(){
accountAndpassword=newDictionary<string,string>( );
accountAndpassword.Add("523080306","556839");
accountAndpassword.Add("lu523080306","556839.");
accountAndpassword.Add("lu556839","556839.");
accountAndpassword.Add("18625523238","895625.");
}
voidOnGUI( ){
//显示框的宽度和高度
float box_w_h=100;
//输入框宽度
float W=160;
//输入框高度
float H=30;
//头像显示框
GUI.Box(newRect((Screen.width-box_w_h)/2,120,box_w_h,box_w_h),image);
//账号输入框
accountText=GUI.TextField(newRect((Screen.width-W)/2,240,W,H),accountText);
account=GUI.TextField(newRect((Screen.width-W)/2-60,240,50,30),account);
//密码输入框
passwordText=GUI.PasswordField(newRect((Screen.width-W)/2,280,W,H),passwordText,'*');
password=GUI.TextField(newRect((Screen.width-W)/2-60,280,50,30),password);
if(GUI.Button(newRect((Screen.width-W)/2,320,W,H),"登录")){
if(accountAndpassword.ContainsKey(accountText)){
if(passwordText==accountAndpassword[accountText]){
print("登陆成功");
}else{
print("账号或密码错误");
}
}else{
print("账号或密码错误");
}
}
}
}
UGUI
usingUnityEngine;
usingSystem.Collections;
usingUnityEngine.UI;
usingSystem.Collections.Generic;
usingUnityEngine.SceneManagement;
publicclassUGUIContercoller:MonoBehaviour{
publicTextenterText;
publicInputFieldaccount;
publicInputFieldpassword;
privateDictionaryaccountAndpassword;
voidStart(){
accountAndpassword=newDictionary();
accountAndpassword.Add("lu523080306","556839");
accountAndpassword.Add("523080306","556839.");
accountAndpassword.Add("lu18625523238","556839...");
}
voidUpdate(){
}
publicvoidButtonPressed(){
if(accountAndpassword.ContainsKey(account.text)){
if(password.text==accountAndpassword[account.text]){
enterText.text="登陆成功";
//场景切换
//SceneManager.LoadScene(0);
}else{
enterText.text="用户名或者密码错误";
}
}else{
enterText.text="用户名或者密码错误";
}
}
}