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.
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;
}
}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
}
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy processing in Update | N/A | N/A | Blocks frame rendering causing lag | [X] Bad |
| Async processing with coroutines | N/A | N/A | Keeps frame rendering smooth | [OK] Good |