Bird
Raised Fist0
Unityframework~8 mins

Touch input basics in Unity - Performance & Optimization

Choose your learning style10 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.

Practice

(1/5)
1. Which Unity property tells you how many fingers are currently touching the screen?
easy
A. Input.touchPhase
B. Input.touchCount
C. Input.GetTouchCount()
D. Input.touchPosition

Solution

  1. Step 1: Understand the purpose of Input.touchCount

    This property returns the number of fingers currently touching the screen in Unity.
  2. Step 2: Compare with other options

    Input.touchPhase is not a property but an enum for touch state; Input.GetTouchCount() does not exist; Input.touchPosition gives position, not count.
  3. Final Answer:

    Input.touchCount -> Option B
  4. Quick Check:

    Number of touches = Input.touchCount [OK]
Hint: Remember: touchCount counts fingers on screen [OK]
Common Mistakes:
  • Confusing touchCount with touchPhase
  • Using non-existent methods like GetTouchCount()
  • Mixing position with count
2. Which of the following is the correct way to get the first touch input in Unity?
easy
A. Input.touch(0)
B. Input.GetTouchCount(0)
C. Input.GetTouch(0)
D. Input.touchCount(0)

Solution

  1. Step 1: Recall the method to access touches

    Unity uses Input.GetTouch(index) to get a touch at a specific index.
  2. Step 2: Check syntax correctness

    Input.touch(0) and Input.touchCount(0) are invalid; GetTouchCount(0) does not exist.
  3. Final Answer:

    Input.GetTouch(0) -> Option C
  4. Quick Check:

    First touch = Input.GetTouch(0) [OK]
Hint: Use GetTouch(index) to access specific touch [OK]
Common Mistakes:
  • Using touch as a method instead of GetTouch
  • Trying to call touchCount as a method
  • Confusing method names
3. What will be the output of this code snippet if the user just started touching the screen?
if (Input.touchCount > 0) {
    Touch touch = Input.GetTouch(0);
    if (touch.phase == TouchPhase.Began) {
        Debug.Log("Touch started");
    }
}
medium
A. Touch ended
B. Touch moved
C. No output
D. Touch started

Solution

  1. Step 1: Understand the touch.phase check

    The code checks if the first touch's phase is TouchPhase.Began, which means the touch just started.
  2. Step 2: Match the condition with output

    If the user just started touching, the condition is true and "Touch started" is logged.
  3. Final Answer:

    Touch started -> Option D
  4. Quick Check:

    touch.phase == Began means "Touch started" [OK]
Hint: TouchPhase.Began means finger just touched screen [OK]
Common Mistakes:
  • Confusing Began with Moved or Ended
  • Ignoring touchCount check
  • Assuming output without touch
4. Identify the error in this Unity C# code snippet for detecting a touch:
if (Input.touchCount > 0) {
    Touch touch = Input.GetTouch(0);
    if (touch.phase = TouchPhase.Began) {
        Debug.Log("Touch started");
    }
}
medium
A. Using assignment (=) instead of comparison (==) in if condition
B. Incorrect method name GetTouch instead of GetTouches
C. Missing check for touchCount == 1
D. TouchPhase.Began is not a valid enum

Solution

  1. Step 1: Check the if condition syntax

    The condition uses a single equals sign (=), which assigns instead of compares.
  2. Step 2: Understand correct comparison operator

    To compare values, double equals (==) must be used in C#.
  3. Final Answer:

    Using assignment (=) instead of comparison (==) in if condition -> Option A
  4. Quick Check:

    Use == to compare, not = [OK]
Hint: Use == for comparison, = is assignment [OK]
Common Mistakes:
  • Using = instead of == in conditions
  • Thinking GetTouch is wrong method
  • Unnecessary touchCount == 1 check
  • Misunderstanding TouchPhase enum
5. You want to detect when the user moves their finger on the screen and print the new position. Which code snippet correctly does this inside Update()?
hard
A.
if (Input.touchCount > 0) {
    Touch touch = Input.GetTouch(0);
    if (touch.phase == TouchPhase.Moved) {
        Debug.Log($"Moved to: {touch.position}");
    }
}
B.
if (Input.touchCount > 0) {
    Touch touch = Input.GetTouch(0);
    if (touch.phase == TouchPhase.Ended) {
        Debug.Log($"Moved to: {touch.position}");
    }
}
C.
if (Input.touchCount == 0) {
    Touch touch = Input.GetTouch(0);
    Debug.Log($"Moved to: {touch.position}");
}
D.
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved) {
    Debug.Log($"Moved to: {touch.position}");
}

Solution

  1. Step 1: Check for active touches

    We must ensure Input.touchCount > 0 before accessing touches to avoid errors.
  2. Step 2: Detect the move phase and print position

    Using touch.phase == TouchPhase.Moved correctly detects finger movement and prints the position.
  3. Final Answer:

    Code snippet D correctly detects movement and prints position -> Option A
  4. Quick Check:

    Check touchCount > 0 and phase == Moved [OK]
Hint: Always check touchCount > 0 before GetTouch [OK]
Common Mistakes:
  • Checking phase == Ended instead of Moved
  • Accessing GetTouch(0) when touchCount == 0
  • Not checking touchCount before GetTouch
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard