Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Android上第一次触屏时,EventSystem.Update()的性能就大幅提升_C#_Android_Performance_Unity3d - Fatal编程技术网

C# Android上第一次触屏时,EventSystem.Update()的性能就大幅提升

C# Android上第一次触屏时,EventSystem.Update()的性能就大幅提升,c#,android,performance,unity3d,C#,Android,Performance,Unity3d,在我的android 2D平台上,当我第一次触摸屏幕时,我会有一个巨大的性能峰值。在此之后,一个小的滞后发生了一次,然后游戏就顺利运行了。没有错误消息 我已经试着将graphic raycaster添加到某些按钮上,这有点帮助。但小的滞后仍然存在。在探查器中,性能峰值位于脚本下,并标记为EventSystem.Update()。以下是来自 单击按钮时,我使用以下代码作为UI管理器: using System.Collections; using System.Collections.Gener

在我的android 2D平台上,当我第一次触摸屏幕时,我会有一个巨大的性能峰值。在此之后,一个小的滞后发生了一次,然后游戏就顺利运行了。没有错误消息

我已经试着将graphic raycaster添加到某些按钮上,这有点帮助。但小的滞后仍然存在。在探查器中,性能峰值位于脚本下,并标记为EventSystem.Update()。以下是来自

单击按钮时,我使用以下代码作为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 --- */
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEditor;
使用UnityEngine.UI;
使用UnityEngine;
公共类UIManager:MonoBehavior
{
公共游戏对象菜单;
公共游戏对象选项;
公共游戏对象暂停;
公共动画师;
公共动画师动画师选项;
公共动画师;
公共兽皮;
公共广播节目;
公共默认值()
{
menu=GameObject.Find(“主菜单”);
选项=游戏对象。查找(“选项菜单”);
pause=GameObject.Find(“pauseMenu”);
animatorMain=menu.GetComponent();
animatorOptions=options.GetComponent();
animatorPause=pause.GetComponent();
隐藏=真实;
show=false;
}
/*---退出应用程序--*/
公共无效出口()
{
Application.Quit();
}
/*---退出应用程序--*/
/*---主菜单--*/
公共无效OpenMainMenu()
{
UIDefaults();
animatorOptions.SetBool(“选项bool”,隐藏);
开始例行程序(“WaitMain菜单”);
}
//第28行.协同程序
IEnumerator WaitMain菜单()
{
//等待选项菜单动画结束。
收益率返回新的WaitForSeconds(1.15f);
SetMainMenuActive();
}
//第35行。启动主菜单动画的功能。
void SetMainMenuActive()
{
menu.GetComponent().enabled=true;
options.GetComponent().enabled=false;
pause.GetComponent().enabled=false;
animatorMain.SetBool(“MainBool”,show);
动画选项.SetBool(“开始选项”,显示);
animatorOptions.SetBool(“选项bool”,show);
}
/*---主菜单--*/
/*---选项菜单--*/
公共void OpenSettings()
{
UIDefaults();
animatorMain.SetBool(“MainBool”,隐藏);
开始例行程序(“等待选项”);
}
//第66行.协同程序
IEnumerator WaitOptions()
{
//等待主菜单动画结束。
收益率返回新的WaitForSeconds(1.15f);
SetOptionsActive();
}
//第73行。启动选项菜单动画的功能。
void setoptions活动()
{
menu.GetComponent().enabled=false;
options.GetComponent().enabled=true;
pause.GetComponent().enabled=false;
动画选项.SetBool(“开始选项”,隐藏);
}
/*---选项菜单--*/
}

当我第一次触摸屏幕时,我希望消除最初的一个时间延迟。

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


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

我有一个类似的问题,对我来说,这是一个对DateTime的调用。现在,在方法的某个深处,速度很慢! 看看这个:
我用Time.Time替换了代码,现在已经修复了

@JozefBenko我只编辑了这篇文章。这个答案的作者是AriBaaGames;)@阿里巴游戏。我只有一个小的后续问题。如何在inspector中设置引用。链接会很有帮助。当我做研究时,我发现,正如你提到的,GameObject.Find()和协同程序对性能有影响,我正试图找到一种替代方法。好吧,我希望你的建议能解决我的问题,如果不是完全的,至少是部分的。不是这个答案的海报,但你基本上在脚本中创建了一个公共变量,然后访问它。在unity中,您应该在inspector中为这个变量找到一个空槽。只需将所需组件连接到的任何游戏对象拖放到此插槽上,并将其分配。另一方面:
GetComponent
也是一个非常昂贵的操作。如果可能的话,你应该避免。同样的原则也适用于那里。