2 回答
TA貢獻1834條經驗 獲得超8個贊
GetComponent<>()是一項資源密集型任務,并且您不必要地調用其中的 3 個,您還每幀添加一個事件偵聽器。
您應該做的是:閱讀 Update、Awake、Start 所做的事情,然后刪除該GetComponent<>()部分并改用屬性或字段,并且不要每幀都添加事件偵聽器。
InputField emailInputField;
InputField passwordInputField;
Button loginButton;
// Setting up the Scene
void Awake()
{
emailInputField = email.GetComponent<Inputfield>();
passwordInputField = password.GetComponent<InputField>();
loginButton = login.GetComponent<Button>();
loginButton.onClick.AddListener(ValidateLogin);
}
// Start is called before the first frame update
void Start()
{
Screen.orientation = ScreenOrientation.Portrait;
}
// Update is called once per frame
void Update()
{
//get values from inputfields
emailString = emailInputField.text;
passwordString = passwordInputField.text;
}
private void ValidateLogin()
{
if (emailString.Trim() == "aa" && passwordString.Trim() == "aa")
{
print("login succeeded!");
SceneManager.LoadScene(1);
}
else
{
print("wrong credentials");
}
}
- 2 回答
- 0 關注
- 382 瀏覽
添加回答
舉報
