Enabling canvas lags build, but only the first time on the first load
The Problem
So I've got a branching dialogue system that I'm pretty happy with so far, but the full build of the game lags for about half a second on machines that aren't dedicated gaming computers when the first dialogue is opened but only the first time the dialogue is opened and only the first time the game is loaded. I've tried to work in every optimization I could find for canvas drawing (all but two of the fourteen items starts with the canvas relevant components disabled, so unless two static images and a canvas scalar are destroying performance I'm not sure what's going on) and update functions that might cause expensive calculations have if statements to prevent them doing those calculations when unnecessary
Chart of the dialogue system
Relevant code for dTrigger
void Start()
{
player = FindObjectOfType<IsoPlayerMovement>();
spriteRenderer = GetComponent<SpriteRenderer>();
characters = FindObjectsOfType<DialogueTrigger>();
}
void Update()
{
if(!player.inConversation)
{
distanceToPlayer = Vector3.Distance(player.transform.position, transform.position);
if(distanceToPlayer < talkRange)
{
bool closest = true;
foreach(DialogueTrigger character in characters)
{
if(character.distanceToPlayer < distanceToPlayer)
{
closest = false;
spriteRenderer.color = Color.gray;
}
}
if(closest)
{
spriteRenderer.color = Color.green;
if(Input.GetKeyDown(KeyCode.Space))
{
player.inConversation = true;
dialogueCanvas.GetComponent<Canvas>().enabled = true;
dialogueCanvas.GetComponent<DialogueManager>().SetEnabled();
}
}
}
else
{
spriteRenderer.color = Color.gray;
}
}
}
Have you discovered any more information on this problem? Having a similar issue. First time enabling a world-space canvas produces a considerable lag spike for me. Considering doing some ugly hacky thing and "pre-loading" it when the scene loads but would prefer an actual solution.
In short: I did the ugly hacky thing and preloaded the assets that were causing performance issues
I also followed some of the advice I found here: https://create.unity3d.com/Unity-UI-optimization-tips like putting separate canvases on different chunks of the UI and disabling the canvas instead of the object the canvas is on. I think I turned off all the graphic raycasters I wasn't using. But I'm also using overlay canvases so your mileage may vary. Hope this helps!
Interesting, thank you for responding! In case any other poor souls find their way here, I ended up doing something different. Found buried in another thread, somebody suggested using a canvas group to control the opacity of the canvas and to leave the canvas active. Since the canvas I was having issues with is very $$anonymous$$imal, it wasn't a big deal to leave it active; this was the easiest solution.