So I’m currently creating a game with procedural world generation. Therefore I am loading and unloading big chunks of world data quite often. The process goes as follows:
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.
Screenshot Profiler case 1
But every so often it spikes to taking 50-200ms.
Screenshot profiler case 2
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 …
Also it seems to be texture size independent because in both cases highlighted here, the texture has a size of 160×160, which is the case most of the time…
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 as said, it can’t be different texture sizes since these are the same most of the time.
I havent completely ruled out GC issues either but I’ve changed classes to structs and reference type arrays to value type arrays to make it a lot easier to collect stuff.
In general it just seem really weird to me that the same Texture2D initialization for example takes 0.05ms 20 times in a row, before taking 40ms once or twice and then going back to 0.05ms.
So I hope anyone might know whats going on here,
or at least can give me some tips for finding the culprit.
Thanks in advance for any help! 🙂
Obarmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.