0
0
Unityframework~8 mins

Touch input basics in Unity - Performance & Optimization

Choose your learning style9 modes available
Performance: Touch input basics
MEDIUM IMPACT
This concept affects how quickly and smoothly a game or app responds to user touch inputs, impacting input responsiveness and frame rate.
Handling touch input in a Unity game
Unity
void Update() {
  for (int i = 0; i < Input.touchCount; i++) {
    Touch touch = Input.GetTouch(i);
    if (touch.phase == TouchPhase.Began) {
      StartCoroutine(ProcessTouchAsync(touch));
    }
  }
}

IEnumerator ProcessTouchAsync(Touch touch) {
  // Spread heavy work over multiple frames
  for (int j = 0; j < 1000; j++) {
    // Simulate work
    if (j % 100 == 0) yield return null;
  }
}
Spreading heavy processing over multiple frames keeps input responsive and maintains frame rate.
📈 Performance GainAvoids blocking main thread, keeps frame time under 16ms, improving input responsiveness.
Handling touch input in a Unity game
Unity
void Update() {
  for (int i = 0; i < Input.touchCount; i++) {
    Touch touch = Input.GetTouch(i);
    if (touch.phase == TouchPhase.Began) {
      // Heavy processing here
      ProcessTouch(touch);
    }
  }
}

void ProcessTouch(Touch touch) {
  // Complex calculations or expensive operations
  for (int j = 0; j < 1000; j++) {
    // Simulate heavy work
  }
}
Heavy processing inside the Update loop for every touch causes frame drops and input lag.
📉 Performance CostBlocks rendering for 10-30ms per frame, causing dropped frames and poor responsiveness.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Heavy processing in UpdateN/AN/ABlocks frame rendering causing lag[X] Bad
Async processing with coroutinesN/AN/AKeeps frame rendering smooth[OK] Good
Rendering Pipeline
Touch input is processed during the game loop before rendering each frame. Heavy input processing can delay frame updates, causing input lag and frame drops.
Input Processing
Game Logic Update
Rendering
⚠️ BottleneckGame Logic Update when heavy processing blocks the main thread
Core Web Vital Affected
INP
This concept affects how quickly and smoothly a game or app responds to user touch inputs, impacting input responsiveness and frame rate.
Optimization Tips
1Avoid heavy processing directly inside Update during touch input.
2Use coroutines to spread expensive work over multiple frames.
3Monitor frame time in Unity Profiler to detect input lag.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of doing heavy calculations directly inside the Update method when processing touch input?
AIt can block the main thread and cause frame drops.
BIt increases the app bundle size significantly.
CIt causes the app to use more memory.
DIt improves input responsiveness.
DevTools: Unity Profiler
How to check: Open Unity Profiler, run the game, and look at the CPU Usage timeline during touch input to see if Update spikes.
What to look for: High spikes in Update or main thread indicate blocking input processing causing frame drops.