PERFORMANCE_OPTIMIZATION_GUIDE
Performance Optimization Guide
Unity Tower Defense - Complete Reference
📚 Table of Contents
1. Introduction
1.1 Document Purpose
1.2 Project Context
1.3 Optimization Philosophy
Core Principles
2. Profiling Methodology
2.1 Unity Profiler
Accessing the Profiler
Key Modules
Profiling Workflow
CPU Profiler Example
Performance Markers
2.2 Frame Debugger
Accessing Frame Debugger
What It Shows
Usage Workflow
Frame Debugger Example
Identifying SRP Batcher Compatibility
2.3 Stats Window
Enabling Stats
Key Metrics
Stats Window Caveats
2.4 Profiling Best Practices
1. Profile in Build, Not Editor
2. Test on Target Hardware
3. Profile Worst-Case Scenarios
4. Compare Apples to Apples
5. Record Multiple Samples
3. CPU Optimization Techniques
3.1 Batch Processing (Eliminating Update())
The Problem
The Solution: System-Based Batch Processing
Performance Impact
Approach
Frame Time
Overhead
Speedup
Implementation Steps
3.2 Eliminating LINQ in Hot Paths
The Problem
The Solution: Manual Iteration
Performance Impact
Approach
Time/Frame
Allocations
GC Impact
When LINQ Is Acceptable
3.3 Coroutines for Infrequent Tasks
The Problem
The Solution: Coroutine with Delay
Performance Impact
Scan Rate
Physics Queries
CPU Time
FPS Impact
When to Use Coroutines
3.4 Object Pooling
The Problem
The Solution: Object Pool
Critical: OnEnable vs Start
Performance Impact
Approach
Spawn Time
Destroy Time
GC/min
Total Overhead
Pre-Warming Pools
3.5 Component Caching
The Problem
The Solution: Cache in Awake
Best Practices
4. GPU Optimization Techniques
4.1 Shadow Optimization
The Problem
The Solution: Disable Unnecessary Shadows
Which Objects Should Cast Shadows?
4.2 Static Batching
The Problem
The Solution: Static Batching
4.3 SRP Batcher (URP)
What is SRP Batcher?
Enabling SRP Batcher
Material Compatibility
Performance Impact
Stats Window Caveat
4.4 GPU Instancing
What is GPU Instancing?
Enabling GPU Instancing
Requirements
Per-Instance Properties (Advanced)
Performance Impact
4.5 Dynamic Batching
What is Dynamic Batching?
Enabling Dynamic Batching
When to Use
Performance Impact
5. Memory Optimization
5.1 Garbage Collection (GC)
Understanding GC
Identifying GC Issues
Reducing GC Pressure
GC Performance Impact
5.2 Texture Memory
Texture Compression
Mipmaps
Texture Atlasing
6. Common Performance Pitfalls
6.1 Update() Overuse
6.2 FindObjectOfType in Update
6.3 Camera.main in Hot Paths
6.4 SendMessage / BroadcastMessage
6.5 Empty Update Methods
6.6 Physics.OverlapSphere Every Frame
7. Troubleshooting Guide
7.1 Low FPS Despite Optimizations
7.2 Frame Spikes
7.3 SRP Batcher Not Working
7.4 High Batch Count
7.5 Memory Leaks
8. Best Practices
8.1 Profiling Workflow
8.2 Optimization Priority
8.3 When to Stop Optimizing
9. Tools Reference
9.1 Unity Profiler
9.2 Frame Debugger
9.3 Stats Window
9.4 Memory Profiler Package
10. Case Studies
10.1 Tower Defense Optimization
10.2 Key Takeaways
📝 Document Revision History
Version
Date
Changes
Last updated