Unity Rendering — 6 — Optimization

Samuel Asher Rivello
5 min readSep 29, 2024

--

I am Samuel Asher Rivello, a professional game developer with 25 years of game development experience — over a decade with the Unity and C#. Unity is a powerful and popular suite of tools (Project IDE, Code IDE, and run-time).

Unity: The Engine That Powers Creativity

Unity has long been a cornerstone of the game development industry, providing developers with a versatile and powerful engine capable of delivering experiences across multiple platforms. With a user-friendly interface, a massive asset store, and a thriving community, Unity has enabled indie developers and large studios alike to create stunning games without the need for massive budgets or teams.

Cover Image

Unity 6 is Cool!

Unity 6 builds upon the strengths of its predecessors while introducing groundbreaking features that elevate the game development process. One of the most notable enhancements is the improved performance architecture, allowing for more complex and visually impressive games. Unity 6 also offers deeper integration with modern hardware, making it easier to optimize games for cutting-edge platforms like VR and AR. Additionally, the new scripting features and graphical improvements make Unity 6 a must-have for developers looking to push the boundaries of what’s possible in gaming.

Unity 6: New GPU-Resident Drawer

In Unity 6, one of the standout features in the graphics domain is the GPU-Resident Drawer. This system, introduced with the Universal Render Pipeline (URP) in version 17.0, allows for more efficient rendering of large numbers of objects by keeping their data on the GPU, reducing the need for frequent data transfers between the CPU and GPU.

Why It’s Cool
The GPU-Resident Drawer significantly optimizes performance by ensuring that the rendering workload remains on the GPU as much as possible. This is particularly advantageous in scenes with many dynamic objects or complex environments, where frequent CPU-GPU communication can become a bottleneck. By keeping draw data on the GPU, Unity 6 enables smoother rendering and allows for more complex scenes without compromising performance.

Unity 6 Project Settings Window

The GPU-Resident Drawer is part of the Universal Render Pipeline (URP) and is designed to be setup here.

Optimization For Unity: The Theory

Optimizing a game isn’t just about making it run faster — it’s about ensuring the best experience for players on a wide range of devices. Below, we’ll explore three key areas of optimization theory: graphics, scripting, and memory management.

Graphics Optimization

Graphics optimization is all about balancing visual fidelity with performance. The goal is to reduce the workload on the GPU while maintaining the game’s visual appeal. Techniques like Level of Detail (LOD), texture atlases, and occlusion culling are essential in minimizing draw calls and ensuring smooth frame rates.

Scripting Optimization

Scripting optimization focuses on making your code run efficiently. By avoiding unnecessary calculations, minimizing garbage collection, and structuring code to reduce CPU overhead, you can ensure that your game’s logic doesn’t become a bottleneck. One key strategy is to avoid placing heavy computations in the `Update` method and instead use custom tick systems that run less frequently.

Memory Management

Memory management is crucial for preventing your game from running out of resources, especially on devices with limited RAM. Efficient memory management involves reducing memory allocations, managing object lifecycles carefully, and using pooling techniques to avoid frequent garbage collection. Optimizing memory usage ensures that your game can handle complex scenes and long play sessions without crashing or slowing down.

Optimization for Unity: Practical Tips

Now that we’ve covered the theory, let’s dive into practical tips for optimizing your Unity project. Each of the following sections includes a real-world tip, along with a screenshot or code sample to help you implement it.

Graphics Optimization in Practice

Tip: Implement Occlusion Culling
Occlusion culling is a powerful technique that prevents the engine from rendering objects that are not visible to the camera. This reduces the number of draw calls, improving performance significantly in complex scenes.

When improving efficiency through culling or other means, use the Unity Profiler Window to further study the performance.

  • Window > Analysis > Profiler. For a detailed overview of the window, see the Profiler window documentation.
Unity 6 Profiler Window

Scripting Optimization in Practice

Tip: Use Object Pooling
Object pooling is an effective way to reduce the overhead associated with instantiating and destroying objects. Instead of creating new objects on demand, you recycle existing ones from a pool, minimizing memory allocations and garbage collection spikes.

Here’s a basic object pooling implementation in C#:

using UnityEngine.Pool;
public class BulletPool : MonoBehaviour
{
public GameObject bulletPrefab;
private ObjectPool<GameObject> pool;
void Start()
{
pool = new ObjectPool<GameObject>(
createFunc: () => Instantiate(bulletPrefab),
actionOnGet: obj => obj.SetActive(true),
actionOnRelease: obj => obj.SetActive(false),
actionOnDestroy: obj => Destroy(obj)
);
}
public GameObject GetBullet() => pool.Get();
public void ReleaseBullet(GameObject bullet) => pool.Release(bullet);
}

This script demonstrates how to create and manage a pool of bullets, reusing them efficiently during gameplay.

Memory Management in Practice

Tip: Optimize Memory Usage with Profiling Tools
Memory leaks and excessive garbage collection can cripple your game’s performance. Unity’s Profiler is an invaluable tool for tracking memory usage and identifying areas for improvement.

Unity 6 Memory Profiler Window

Use the Memory Profiler to monitor allocations and pinpoint objects that aren’t being cleaned up properly. By addressing these issues early, you can prevent memory-related slowdowns and crashes in your game.

This article gives a balanced overview of Unity 6, theoretical concepts for optimization, and practical tips with real-world applications. It’s designed to inform and empower developers who want to optimize their Unity projects effectively.

Resources

🦜 Contact

  • Samuel Asher Rivello has over 20 years of game dev XP. He is available for remote, contract hire as a game developer and game dev educator.
  • Contact Sam today to say hi and discuss your projects!

📜 Articles

🛜 Downloads

--

--

Samuel Asher Rivello
Samuel Asher Rivello

Written by Samuel Asher Rivello

Game Developer & Instructor - Unity Certified. 20+ years of game dev XP. Say Hi: ) http://www.SamuelAsherRivello.com

No responses yet