Performance: Touch input basics
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 |
Input.touchCountInput.touchPhase is not a property but an enum for touch state; Input.GetTouchCount() does not exist; Input.touchPosition gives position, not count.Input.GetTouch(index) to get a touch at a specific index.Input.touch(0) and Input.touchCount(0) are invalid; GetTouchCount(0) does not exist.if (Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began) {
Debug.Log("Touch started");
}
}TouchPhase.Began, which means the touch just started.if (Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
if (touch.phase = TouchPhase.Began) {
Debug.Log("Touch started");
}
}Update()?Input.touchCount > 0 before accessing touches to avoid errors.touch.phase == TouchPhase.Moved correctly detects finger movement and prints the position.Pass the test and this pattern moves unknown