0
0
Unityframework~8 mins

Client-server architecture in Unity - Performance & Optimization

Choose your learning style9 modes available
Performance: Client-server architecture
HIGH IMPACT
This affects how quickly the game communicates with the server and updates the game state, impacting responsiveness and load times.
Fetching player data frequently during gameplay
Unity
void Start() {
  InvokeRepeating(nameof(FetchPlayerDataFromServer), 0f, 5f);
}

IEnumerator FetchPlayerDataFromServer() {
  using (UnityWebRequest request = UnityWebRequest.Get("https://gameapi.com/playerdata")) {
    yield return request.SendWebRequest();
    if (request.result == UnityWebRequest.Result.Success) {
      ProcessData(request.downloadHandler.text);
    }
  }
}
Fetching data at fixed intervals reduces network load and keeps game responsive.
📈 Performance GainReduces network requests from hundreds per second to one every 5 seconds, improving input responsiveness.
Fetching player data frequently during gameplay
Unity
void Update() {
  StartCoroutine(FetchPlayerDataFromServer());
}

IEnumerator FetchPlayerDataFromServer() {
  using (UnityWebRequest request = UnityWebRequest.Get("https://gameapi.com/playerdata")) {
    yield return request.SendWebRequest();
    if (request.result == UnityWebRequest.Result.Success) {
      ProcessData(request.downloadHandler.text);
    }
  }
}
Calling server every frame causes many network requests, blocking game responsiveness and increasing latency.
📉 Performance CostTriggers hundreds of network requests per second, blocking main thread and increasing input delay.
Performance Comparison
PatternNetwork RequestsFrame DropsInput DelayVerdict
Request every frame100+ per secondHighHigh[X] Bad
Request every 5 seconds0.2 per secondLowLow[OK] Good
Rendering Pipeline
Client-server communication affects the game loop by introducing network latency and potential frame delays when waiting for server responses.
Game Loop
Network I/O
Input Processing
⚠️ BottleneckNetwork I/O causing frame stalls
Core Web Vital Affected
INP
This affects how quickly the game communicates with the server and updates the game state, impacting responsiveness and load times.
Optimization Tips
1Avoid making server requests every frame to prevent blocking the main thread.
2Use asynchronous calls and batch data to reduce network overhead.
3Fetch data at reasonable intervals to keep gameplay smooth and responsive.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance issue with making a server request every frame in Unity?
AIt causes too many network requests, blocking the main thread and increasing input delay.
BIt reduces the game's graphics quality.
CIt increases the game's memory usage significantly.
DIt improves the game's responsiveness.
DevTools: Unity Profiler
How to check: Open Unity Profiler, go to Network section, observe frequency and duration of network calls during gameplay.
What to look for: Look for spikes in network activity and frame time increases correlating with network requests.