Author Topic: [SOLVED] UICamera.inputHasFocus problem since 3.5.6 update  (Read 4847 times)

Highstorm

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
[SOLVED] UICamera.inputHasFocus problem since 3.5.6 update
« on: April 21, 2014, 12:10:37 PM »
Hi ArenMook,

The 3.5.6 update featured this fix, presumably in response to this thread:
Quote
- FIX: UICamera's 'inputHasFocus' flag is now set when selection changes rather than every frame.

Previously I was using UICamera.inputHasFocus to prevent custom key bindings from firing while typing in an input field with a simple check:
  1. if( !UICamera.inputHasFocus ) {
  2. //Custom key coding
  3. }

This does not work since the update, I assume because it is no longer checked every frame. The fix log states that the flag is set when the "selection changes"; does that mean when you click into an input field and/or out of it as well? Because in all my tests, I can't seem to get inputHasFocus to set true at all.

With both 3.5.6 and the latest 3.5.8, I set up a new project with just NGUI, and added your "Control - Simple Input Field", along with a test script to return the status of UICamera.inputHasFocus. Clicking into the input, typing, clicking outside - none of these seem to set the bool true;

Am I just using this wrong since the update? If so, I would greatly appreciate some insight into its proper usage.

Thanks.
« Last Edit: April 23, 2014, 10:13:56 AM by Highstorm »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UICamera.inputHasFocus problem since 3.5.6 update
« Reply #1 on: April 22, 2014, 04:25:03 AM »
inputHasFocus is set on line 452 of UICamera.cs -- in the ChangeSelection() function, which is a co-routine (and is the only way to change the input focus to another object). When you change the selection via code the selection change is not immediate, and is actually delayed until the end of frame. Perhaps this is what you're running into?

Highstorm

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UICamera.inputHasFocus problem since 3.5.6 update
« Reply #2 on: April 22, 2014, 11:24:18 AM »
Hi Aren, thanks for the reply.

Either I'm not understanding what you mean (I am still quite new to coding, apologies), or I did not explain myself clear enough.

What it boils down to is this: I used to use the inputHasFocus bool to determine when a UIInput component was actively being typed in. What can I use post 3.5.6 to do the same?

The following is a simple project setup to demonstrate the issue:


InputTest.cs
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class InputTest : MonoBehaviour {
  5.        
  6.         bool swapColor;
  7.        
  8.         void Update ()
  9.         {
  10.                 if( !UICamera.inputHasFocus && Input.GetKeyDown (KeyCode.A) )
  11.                 {
  12.                         swapColor = !swapColor;
  13.                 }      
  14.         }
  15.        
  16.         void OnGUI () {
  17.                
  18.                 if( swapColor )
  19.                 {
  20.                         GUI.color = Color.red;
  21.                 }
  22.                 else GUI.color = Color.white;
  23.                
  24.                 GUI.Button (new Rect((Screen.width * 0.5f) - 100, (Screen.height * 0.5f)-15, 200, 30), "Press A");
  25.         }
  26. }

When you press "A", the Unity button will toggle its color red and back.
  • In NGUI 3.5.5, typing "aaaaa" into the input field leaves the button black. This is the desired result.
  • In NGUI 3.5.6 and on (3.5.8 in my tests), typing "aaaaa" into the input field toggles the button color with each press of the key.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UICamera.inputHasFocus problem since 3.5.6 update
« Reply #3 on: April 23, 2014, 07:32:36 AM »
Thanks, that code helped track down the issue. Seems it can be fixed by changing UICamera's line 1396 from:
  1. selectedObject = null;
to:
  1. selectedObject = currentTouch.pressed;
...not sure why it was ever set to null like that.

Highstorm

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: UICamera.inputHasFocus problem since 3.5.6 update
« Reply #4 on: April 23, 2014, 10:11:13 AM »
Perfect! Thanks, Aren!