0
0
Unityframework~8 mins

Public vs SerializeField in Unity - Performance Comparison

Choose your learning style9 modes available
Performance: Public vs SerializeField
MEDIUM IMPACT
This concept affects the memory usage and serialization performance during game loading and editor interaction in Unity.
Exposing variables to the Unity Inspector for editing while controlling access
Unity
[SerializeField] private int health = 100;
Keeps the field private to protect encapsulation but still allows Unity to serialize it for the Inspector, reducing accidental external access and controlling serialization scope.
📈 Performance GainReduces unnecessary public exposure and keeps serialized data focused, improving maintainability without runtime cost
Exposing variables to the Unity Inspector for editing while controlling access
Unity
public int health = 100;
Making fields public exposes them to all scripts, increasing coupling and risking unintended changes; also, Unity serializes all public fields which can increase serialization overhead.
📉 Performance CostIncreases serialized data size and can cause unnecessary memory usage during serialization
Performance Comparison
PatternSerialized Data SizeMemory UsageEncapsulationVerdict
Public fieldsLarger (all public fields serialized)Higher (more data loaded)Low (fields accessible everywhere)[X] Bad
[SerializeField] private fieldsSmaller (only marked fields serialized)Lower (less data loaded)High (fields encapsulated)[OK] Good
Rendering Pipeline
Unity serialization happens during scene loading and editor serialization. Public fields are automatically serialized, while private fields require [SerializeField] to be included. This affects memory layout and load times.
Serialization
Memory Allocation
Editor UI Rendering
⚠️ BottleneckSerialization stage during scene load and editor save
Optimization Tips
1Use [SerializeField] on private fields to control serialization and Inspector visibility.
2Avoid making fields public unless external access is necessary to reduce serialized data size.
3Keep serialized data minimal to improve scene load times and memory usage.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using [SerializeField] on private fields instead of making them public?
AIt reduces the serialized data size by only serializing necessary fields.
BIt makes the game run faster by optimizing CPU instructions.
CIt decreases the number of draw calls in rendering.
DIt automatically compresses textures to save memory.
DevTools: Profiler
How to check: Open Unity Profiler, record during scene load or serialization events, and inspect memory and serialization time.
What to look for: Look for excessive memory allocation or long serialization times indicating too many public fields serialized.