Unity: Difference between revisions

No change in size ,  20 August 2019
No edit summary
Line 22: Line 22:
-->
-->


== Optimization ===
== Optimization ==
=== Loading Files ===
=== Loading Files ===
All loading of streaming assets should be done in a background thread. See [https://wiki.davidl.me/view/C_Sharp#Multithreading this] for more details on how to use threading in C#.<br>
All loading of streaming assets should be done in a background thread. See [https://wiki.davidl.me/view/C_Sharp#Multithreading C# Multithreading] for more details on multithreading.<br>
I've found that Unity's job system doesn't perform as well as C#'s ThreadPool when stressed with thousands of small tasks so I recommend using C# APIs over Unity APIs whenever possible.<br>
I've found that Unity's job system doesn't perform as well as C#'s ThreadPool when stressed with thousands of small tasks so I recommend using C# APIs over Unity APIs whenever possible.<br>
[https://wiki.davidl.me/view/C_Sharp#Multithreading Ciela Spike's Thread Ninja] may be used to run coroutines in the background when you only need a single, but complex, task.<br>
[https://wiki.davidl.me/view/C_Sharp#Multithreading Ciela Spike's Thread Ninja] may be used to run coroutines in the background when you only need a single, but complex, task.<br>
Much of Unity's API such as <code>new Mesh()</code> and <code>new Texture2D()</code> cannot be called from background threads. I suggest caching textures as <code>byte[]</code> or <code>Color32[]</code> which can then be loaded with [https://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html <code>Texture2D.LoadRawImageData()</code>]. Similarly, you can cache a Mesh by caching a struct of vertices, triangles, normals, and tangents.
Much of Unity's API such as <code>new Mesh()</code> and <code>new Texture2D()</code> cannot be called from background threads. I suggest caching textures as <code>byte[]</code> or <code>Color32[]</code> which can then be loaded with [https://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html <code>Texture2D.LoadRawImageData()</code>]. Similarly, you can cache a Mesh by caching a struct of vertices, triangles, normals, and tangents.