Unity: Difference between revisions

24 bytes removed ,  4 September 2020
no edit summary
No edit summary
No edit summary
Line 1: Line 1:
__FORCETOC__
Unity is a game development framework and game engine. It has a shallow learning curve and is very capable, supporting most popular platforms and features including AR and VR platforms.
Unity is a game development framework and game engine. It has a shallow learning curve and is very capable, supporting most popular platforms and features including AR and VR platforms.


== Shaders ==  
==Shaders==  
Unity shaders are written in [[HLSL]].
Unity shaders are written in [[HLSL]].
Unity supports the standard vertex, geometry, fragment shader pipeline.<br>
Unity supports the standard vertex, geometry, fragment shader pipeline.<br>
Line 10: Line 8:
Results from compute shaders can be used on the graphical shaders without being copied back to the CPU.<br>
Results from compute shaders can be used on the graphical shaders without being copied back to the CPU.<br>


=== Compute Shaders ===
===Compute Shaders===
To use a compute shader, add a <code>ComputeShader</code> reference to your C# script.
To use a compute shader, add a <code>ComputeShader</code> reference to your C# script.
To copy data to and from the GPU, use a <code>ComputeBuffer</code>.
To copy data to and from the GPU, use a <code>ComputeBuffer</code>.
Line 21: Line 19:
-->
-->


== 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 C# Multithreading] for more details on multithreading.<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>
Line 28: Line 26:
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 ===
===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>
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>
See [https://docs.unity3d.com/Manual/GPUInstancing.html Unity: GPUInstancing].<br>
See [https://docs.unity3d.com/Manual/GPUInstancing.html Unity: GPUInstancing].<br>