Search Unity

Bug Updates takes longer in build than in editor ?

Discussion in 'Scripting' started by ClementSXD, May 26, 2023.

  1. ClementSXD

    ClementSXD

    Joined:
    Aug 29, 2022
    Posts:
    9
    Hello,

    I have a script that spawner 15000 cubes rotating around a target. Their distance to the target shrinks or expands depending on a sine wave.

    Depending on their distance to the target, their color changes by evaluating a gradient (close: red; far: blue).
    In the editor, my code takes ~3ms to execute.
    In a build, my code takes between 14 to 30ms.

    Thee build is compiled in master Mode with IL2CPP, .Net Framework and Faster Runtime enabled. The Project uses the build-in render pipeline, and uses a MaterialPropertyBlock to change the colors of each renderer.

    Here is the code:

    Code (CSharp):
    1.  
    2. public class AgentColorManager : MonoBehaviour
    3.     {
    4.         private MaterialPropertyBlock _mpb;
    5.         private static readonly int _colorPropID = Shader.PropertyToID("_Color");
    6.  
    7.         private void Start()
    8.         {
    9.             this._mpb = new MaterialPropertyBlock();
    10.         }
    11.  
    12.         void Update()
    13.         {
    14.             float sin = Mathf.Sin(Time.time * AgentHolder.PulseFrequency);
    15.             this._mpb.SetColor(AgentColorManager._colorPropID, AgentHolder.Gradient.Evaluate((sin + 1f) / 2f));
    16.  
    17.             for (int i = 0; i < AgentHolder.NbAgents; i++)
    18.             {
    19.                 // Couleur
    20.  
    21.                 AgentHolder.Renderers[i].SetPropertyBlock(this._mpb);
    22.             }
    23.         }
    24.     }
    Here is the Profiling info (Standalone Process) for the test in the editor:
    3ms.jpg

    And here is the one in a standalone build:
    30ms.jpg

    I have no idea what causes these spikes. I had another version of this script where the references to the renderers were stored in a array of structs with some other data in it, instead of directly inside an array of renderers, and the Update actually had more work to do as it evaluated the gradient foreach cube. But suprisingly, it ran smoother in build than this version.

    Any help would be appreciated.