Last active
August 17, 2024 10:29
-
-
Save unitycoder/73106c4500ffd871795211a837beb4a1 to your computer and use it in GitHub Desktop.
Optimization Tips c# Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
access internal classes, by making your assemble definition named as: | |
Unity.InternalAPIEditorBridgeDev.001 | |
Unity.InternalAPIEditorBridgeDev.002.. | |
Get full internal access without reflection to any Unity package. Create an assembly reference file and point it to the assembly you want internal access to, now add your scripts into the same folder. The scripts will be compiled as part of the target assembly! | |
https://twitter.com/KarlJamesJones/status/1523992385071501313 | |
textmeshpro not working, missing files, reinstall doesnt help | |
- check if some package has TMPRO included, remove that first, then install full TMPRO | |
point light cookie | |
- must have alpha (or from grayscale) enabled | |
- need to adjust intensity once! to update light cookie | |
manually maximize window | |
https://forum.unity.com/threads/windowed-mode-goes-under-windows-taskbar.1161893/#post-7451738 | |
TRS translation, rotation, scale | |
https://forum.unity.com/threads/changing-local-mesh-vertices-into-world-space-coordinates.1160849/#post-7444358 | |
"If you are encoding bytes yourself, try using sbyte[] instead of byte[] (store the same values, for example cast sbyte* to byte* in unsafe block). | |
The performance problem comes from the fact that byte in C# is unsigned, but in Java it is signed. " | |
https://forum.unity.com/threads/performance-issues-to-send-byte-from-c-to-java-all-scenarios-tested-are-there-others.991742/#post-6440735 | |
Understand mobile devices are always Vsynced. That means they always wait for the screen refresh to happen before rendering the next frame. The above example may be taking just longer than 16.6ms to render, or the target frame rate is set to 30, it’ll wait until 33.3ms have passed before starting to render again. If it takes just longer than 33.3ms it’ll wait until 66.6ms have passed. | |
https://forum.unity.com/threads/mobile-graphics-optimization-unplayable-game.987153/#post-6411705 | |
if slow builds: disable mesh compression, happens on every build? | |
https://www.reddit.com/r/Unity3D/comments/e9gz6h/my_own_unity_report_profiler_mobile_how_i/ | |
burst intrinsics | |
https://www.youtube.com/watch?v=BpwvXkoFcp8 | |
https://docs.unity3d.com/Packages/[email protected]/manual/index.html#unityburstintrinsics | |
calculate FPS | |
1000/targetFPS = time per frame (ms) | |
internal timer (double): | |
InputState.currentTime | |
string concat it faster | |
https://forum.unity.com/threads/concatenating-strings-speed-test-which-is-faster.924905/ | |
profiler tips, vsync is forced in mobile hw | |
https://www.youtube.com/watch?v=uXRURWwabF4 | |
"This is why a cheaper alternative to a super high memory GPU would be to use two GPUs, one for the editor and one dedicated to light baking." | |
https://forum.unity.com/threads/progressive-gpu-lightmapper-preview.561103/page-12#post-5866954 | |
gpu baking | |
- prioritize view option : not recommend using it for long bakes (high sample count). It has some overhead in the scheduling we do internally and make the overall bake longer | |
- antialiasing sample count: if you are not using hard shadows on any of your lights I would also recommend reducing the antialisaing sample count for GI more aggressively. In fact, 1spp should be fine in terms of quality if you don't have any hard shadows or if your texel resolution is high enough. Reducing it will reduce in-flight baking memory and thus memory bandwidth on your GPU | |
https://forum.unity.com/threads/gpu-lightmapper-hardly-uses-gpu.870109/#post-5846482 | |
big structs cause string memcopy! | |
https://forum.unity.com/threads/string-memcpy-in-profiler.119166/ | |
ar foundation has fast device camera access | |
https://youtu.be/1a_ik-uXWo4?t=457 | |
faster c# array access | |
https://twitter.com/SergioPedri/status/1242844697208541189 | |
We have two modes for particle rendering: | |
dead particles get culled in vertex shader. This is the behavior you're seeing. This is optimal when particles have few vertices and when the alive count is close to the capacity. This is the default mode. | |
Indirect mode, dead particles are culled in compute shader. This is less sensitive to alive/capacity ratio but adds some overhead. Use this when your alive particles count / capacity ratio is low (and particularly if you use mesh particles with many vertices). This mode is forced when GPU sorting is used. | |
https://forum.unity.com/threads/solved-mesh-output-particles-not-cleared-after-particle-is-dead.848650/#post-5601301 | |
#DOTS #unitytips: The size of data really matters. Converting floats to halfs, ints to bytes etc in my structs made it A LOT faster, especially set/copy jobs. But excessive math operations in half will actually REDUCE perf. Conclusion: Use half for storage, floats for calculation | |
https://twitter.com/Nothke/status/1235183611214692353 | |
faster editor compile times | |
https://forum.unity.com/threads/unity-assembly-definition-files-slower-not-faster-how-to-compile-faster.511199/#post-4509778 | |
unity set audio rate lower than 8khz, | |
edit meta file! | |
tip, generated audioclip doesnt show audiodata in editor! | |
unitywebrequest dont use string url | |
https://forum.unity.com/threads/initial-unitywebrequest-performance-spike.821061/#post-5449386 | |
Yes the mipmap streaming should mean only the lower mips are loaded at first. The 'Max Level Reduction' value specifies how many mips at not loaded at the start. Once it calculates the highest visible mip level for a texture it will then load that desired mip level, if its not already loaded (including all the mips below that level). | |
There are some requirements for a texture to be streamable which could effect the result. | |
Are your textures Read/Write enabled. If they are then all mips are loaded for the CPU side copy. | |
Crunch compressed textures are not mip streamable so will load in full. | |
https://forum.unity.com/threads/mipmap-streaming-asset-bundles-is-the-whole-texture-loaded-from-disk.821199/#post-5437719 | |
sprite.create slow? | |
dont generate tight sprite, use rectangle, so texture doesnt need to be read | |
https://forum.unity.com/threads/any-way-to-speed-up-sprite-create.529525/#post-5427861 | |
2D urp, | |
too many render texture switches | |
https://forum.unity.com/threads/urp-optimization.818568/#post-5424936 | |
branch prediction and other code quality stuff | |
https://macton.smugmug.com/Other/2008-07-15-by-Eye-Fi/n-xmKDH/i-Dd6xtNh | |
comparing of atomics and nativearrays | |
https://forum.unity.com/threads/very-cool-stuff-in-com-unity-media-utilities.777740/#post-5177366 | |
unity random uses xor-shift | |
threadsafe no alloc queue | |
https://forum.unity.com/threads/thread-safe-queue-with-no-allocations.308842/#post-2686958 | |
https://forum.unity.com/threads/trygetlatestimage-performance.726833/#post-4854233 | |
background thread to process pixels from ar texture | |
// slow to multiply or add vectors, new vector gets created | |
Vector3 a,p,c; | |
float, b | |
p = a*b+c; | |
// faster | |
p.x = a.x*b+c.x; | |
p.y = a.y*b+c.y; | |
p.z = a.z*b+c.z; | |
iterating mesh.vertices[] is much*1000 slower than putting verts in array first (vers = mesh.vertices) and iterating that. | |
note: slow down in one project, if enabled [x] instancing, in sprite shader! 30fps>15fps | |
getting batching in physics, | |
"move ALL objects first, before calling any physics stuff like raycast" | |
https://old.reddit.com/r/Unity3D/comments/a5d85a/unity_physics_best_practice_batching_transform/ | |
https://docs.unity3d.com/uploads/ExpertGuides/Analyzing_your_game_performance_using_Event_Tracing_for_Windows.pdf | |
gc alloc check | |
https://docs.unity3d.com/2018.3/Documentation/ScriptReference/TestTools.Constraints.AllocatingGCMemoryConstraint.html | |
https://lemire.me/blog/2017/09/18/visiting-all-values-in-an-array-exactly-once-in-random-order/ | |
// Vector2 vs Vector3 distance | |
https://forum.unity.com/threads/vector2-vs-vector3-distance.544206/ | |
public static class VectorHelper | |
{ | |
public static float DistanceVector2(Vector2 a, Vector2 b) | |
{ | |
float x = a.x - b.x; | |
float y = a.y - b.y; | |
return Mathf.Sqrt(x * x + y * y); | |
} | |
} | |
// can set enum type to byte! | |
public enum Type : byte { Air, Filled }; | |
https://forum.unity3d.com/threads/i-need-help-optimizing-a-procedurally-generated-chunk.487836/#post-3183778 | |
Structs are value-types like ushorts and enums. There are no extra references. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://youtu.be/2A9hAhAQ7V4?t=19866