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

标签 c# android performance unity3d

在我的 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/

相关文章:

asp.net-mvc - MiniProfiler偶尔显示Asp.Net Mvc慢速调用操作

c# - 用 C# 测量其他进程的执行时间,奇怪的结果

c# - 从 Java 调用 C# 代码?

c# - 将 MDF 与 Entity Framework 核心结合使用

c# - Oxyplot 不在 WPF View 中显示绘图

javascript - 智能手机屏幕尺寸是多少//智能手机预装了哪些编码语言?

java - 如何让 OkHttpClient 遵守 REST API 速率限制?

android - 在整个应用程序中全局广播和接收连接更改的最佳做法是什么?

javascript - react : useCallback - useCallback with empty dependency array VS not using useCallback at all

c# - 我可以检查事务性 msmq 队列中的项目吗