So I’m currently creating a game with procedural world generation. Therefore I am loading and unloading big chunks of world data quite often.
Step 1: Upon loading a chunk, it gets a thread worker which reads its data from a file in a seperate thread.
This worker also prepares “proto-objects” that contain most of the data that is necessary to instantiate my GameObjects later. This includes things like a Color array to set a texture and a Vector2 array to set a collider.
Step 2: Once the thread is done, it puts the finished proto-objects into a ConcurrentQueue where they are dequeued in the main thread and used to initialize the GameObjects. This includes adding the script and a SpriteRenderer as components and initializing a Texture2D with the createUninitialized flag set to true.
Step 3: The Creation of the collider as well as the application of the Color array to the texture and subsequent Apply() call are handled later (which works fine and is outside of the scope of this post)
Now heres the problem:
Step 2 usually takes about 0.5-5ms.
But every so often it spikes to taking 50-200ms.
As you can see, this happens in more or less regular intervals. I first suspected the garbage collector clears to have something to do with it because you can see it lining up with this spike here. This does not seem to be the case with the others though.
It seems to be texture size independent because in both cases highlighted here, the texture has a size of 160x160, which is the case most of the time.
Also just for clarification, theres only one object spawned in each screenshot.
Anyways, here’s the code that gets profiled in the highlighted “Create Objects” part:
public static PhixelObject Create(Chunk chunk, Vector3Int chunkPos, List<Vector2> verticies, List<Phixel> perimeterPhixels, Phixel[,] phixelMatrix, GameObject gameObject = null)
{
//Create create
PhixelObject phixelObject = Create<PhixelObject>(gameObject);
//this just creates a new GameObject and adds the script as a component
//Set Transform
phixelObject.transform.localScale = new Vector3(Constants.PIXEL_PER_METER, Constants.PIXEL_PER_METER, 1);
phixelObject.transform.parent = chunk.transform;
phixelObject.transform.localPosition = new Vector3(chunkPos.x - Constants.CHUNK_SIZE / 2, chunkPos.y - Constants.CHUNK_SIZE / 2, -chunkPos.z);
//Add Sprite Renderer
phixelObject.textureRenderer = phixelObject.AddComponent<SpriteRenderer>();
//"create Texture_x" + phixelObject.dims.x.ToString() + "_y"+ phixelObject.dims.y.ToString()
phixelObject.tex = new Texture2D(phixelObject.dims.x, phixelObject.dims.y, TextureFormat.RGBA32, -1, false, true);
return phixelObject;
}
(I replaced the manual Profile.BeginSample() calls with comments for readability and removed parts that are not profiled)
Well these spikes currently result in stutters that make the whole thing pretty unplayable, which means I’ve been searching for their cause for a few days now, not the most experienced game dev though so maybe you guys know something I don’t
So thanks in advance for any help!
PS.: I put this in the graphics discussions because I thought the Texture2D might be the culprit, upon looking at it again though I’m not so sure about that anymore so feel free to move if thats possible