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 关注
- 315 浏览
添加回答
举报