gpt4 book ai didi

c# - EventSystem.Update() 在 Android 上的第一次屏幕触摸时出现巨大的性能峰值

转载 作者:行者123 更新时间:2023-12-04 23:56:40 25 4
gpt4 key购买 nike

在我的 Android 2D 平台游戏上,当我第一次触摸屏幕时,我的性能出现了巨大的峰值。之后会出现一次小延迟,然后游戏就可以顺利运行了。没有错误消息。

我已经尝试将图形光线转换器分配给某些有所帮助的按钮。但小滞后仍然存在。在探查器中,性能峰值位于脚本下方并标记为 EventSystem.Update()。这是来自

的屏幕截图

Unity Profiler

在单击按钮时,我使用以下代码作为 UI 管理器:

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.UI;
using UnityEngine;

public class UIManager : MonoBehaviour
{
public GameObject menu;
public GameObject options;
public GameObject pause;
public Animator animatorMain;
public Animator animatorOptions;
public Animator animatorPause;
public bool hide;
public bool show;

public void UIDefaults()
{
menu = GameObject.Find("MainMenu");
options = GameObject.Find("optionsMenu");
pause = GameObject.Find("pauseMenu");

animatorMain = menu.GetComponent<Animator>();
animatorOptions = options.GetComponent<Animator>();
animatorPause = pause.GetComponent<Animator>();

hide = true;
show = false;
}

/* --- EXIT APPLICATION --- */
public void Exit()
{
Application.Quit();
}
/* --- EXIT APPLICATION --- */

/* --- MAIN MENU --- */
public void OpenMainMenu()
{
UIDefaults();
animatorOptions.SetBool("OptionsBool", hide);
StartCoroutine("WaitMainMenu");
}

// line 28. Coroutine
IEnumerator WaitMainMenu()
{
// wait for Options menu animation to end.
yield return new WaitForSeconds(1.15f);

SetMainMenuActive();
}

// line 35. function to Start the Main menu animation.
void SetMainMenuActive()
{
menu.GetComponent<Canvas>().enabled = true;
options.GetComponent<Canvas>().enabled = false;
pause.GetComponent<Canvas>().enabled = false;

animatorMain.SetBool("MainBool", show);
animatorOptions.SetBool("StartOptions", show);
animatorOptions.SetBool("OptionsBool", show);
}
/* --- MAIN MENU --- */


/* --- OPTIONS MENU --- */
public void OpenSettings()
{
UIDefaults();
animatorMain.SetBool("MainBool", hide);
StartCoroutine("WaitOptions");
}

// line 66. Coroutine
IEnumerator WaitOptions()
{
// wait for Main menu animation to end.
yield return new WaitForSeconds(1.15f);

SetOptionsActive();
}

// line 73. function to Start the Options menu animation.
void SetOptionsActive()
{
menu.GetComponent<Canvas>().enabled = false;
options.GetComponent<Canvas>().enabled = true;
pause.GetComponent<Canvas>().enabled = false;

animatorOptions.SetBool("StartOptions", hide);
}
/* --- OPTIONS MENU --- */
}

我希望在第一次触摸屏幕时摆脱最初的一次延迟。

最佳答案

部分原因可能是您正在使用 GameObject.Find() 来获取对菜单内容的引用。 GameObject.Find 回调引擎,可能会导致性能下降和分析器中出现峰值。

在检查器中设置这些引用,而不是在触发触摸事件时查找它们。希望这可以帮助! :)

关于c# - EventSystem.Update() 在 Android 上的第一次屏幕触摸时出现巨大的性能峰值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56447517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com