亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Unity 場景加載時間非常長

Unity 場景加載時間非常長

C#
阿波羅的戰車 2022-10-23 15:20:06
我對 Unity 還很陌生,所以對 IDE 沒有太多經驗。我正在開發一個非?;镜膽贸绦?、一個登錄和一個帶有一些基本 UI 元素的儀表板。我遇到的問題是當我嘗試切換場景時。因此,從 LoginScene 到 Dashboard 場景最多可能需要 20 秒。腳本甚至不需要運行太多邏輯。在我看來,這太長了,有人知道如何優化我的代碼,或者至少知道我做錯了什么?這是檢查正確用戶和更改場景的代碼。// Start is called before the first frame updatevoid Start(){    Screen.orientation = ScreenOrientation.Portrait;}// Update is called once per framevoid Update(){    //get values from inputfields    emailString = email.GetComponent<InputField>().text;    passwordString = password.GetComponent<InputField>().text;    btnLogin = login.GetComponent<Button>();    btnLogin.onClick.AddListener(ValidateLogin);}private void ValidateLogin(){    if (emailString.Trim() == "aa" && passwordString.Trim() == "aa")    {        print("login succeeded!");        SceneManager.LoadScene(1);    }    else    {        print("wrong credentials");    }}順便說一句:數字 1 是對我的下一個場景的引用,即儀表板場景。
查看完整描述

2 回答

?
MMMHUHU

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");

    }


}


查看完整回答
反對 回復 2022-10-23
?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

轉換我的評論:Listener 被添加到 中Update(),而不是Start(). 因此,它被分配到每一幀。



查看完整回答
反對 回復 2022-10-23
  • 2 回答
  • 0 關注
  • 382 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號