Unity: Difference between revisions

572 bytes added ,  11 February 2020
No edit summary
Line 28: Line 28:
[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.
=== Instancing ===
If you need multiple instances of the same object, make sure that the material is instanced so that all objects only consume one draw call.<br>
Do not attach scripts to thousands of objects. Instead use one object.<br>
If you need thousands of simple objects, one way is to use a mesh with one vertex per object and expand them in the geometry shader.<br>
You can use one compute shader to animate and move the objects instead of using Unity scripts. The compute shader edits the compute buffer which can be read by your vertex or geometry shader.<br>


==References==
==References==
* [https://docs.unity3d.com/Manual/index.html Unity User Manual]
* [https://docs.unity3d.com/Manual/index.html Unity User Manual]
* [https://www.udemy.com/course/the-ultimate-guide-to-game-development-with-unity/ Basic Game Dev Course (Paid)]
* [https://www.udemy.com/course/the-ultimate-guide-to-game-development-with-unity/ Basic Game Dev Course (Paid)]