On my 2D platformer for android I have a huge performance spike when I touch the screen for the first time. After that a small lag occures once and than the game is running smoothly. No error messages.
I already tried to asign graphic raycaster just to certain buttons which helped a bit. But the small lag is still present. In the profiler the performance spike is under the scripts and is marked as the EventSystem.Update(). here is the screenshot from the
On button click I use the following code as the UI Manager:
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 --- */
}
I wish to get rid of the initial one time lag when I touch the screen for the first time.
