Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Highstorm on April 21, 2014, 12:10:37 PM

Title: [SOLVED] UICamera.inputHasFocus problem since 3.5.6 update
Post by: Highstorm on April 21, 2014, 12:10:37 PM
Hi ArenMook,

The 3.5.6 update featured this fix, presumably in response to this thread (http://www.tasharen.com/forum/index.php?topic=8875.msg41805#msg41805):
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.
Title: Re: UICamera.inputHasFocus problem since 3.5.6 update
Post by: ArenMook 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?
Title: Re: UICamera.inputHasFocus problem since 3.5.6 update
Post by: Highstorm 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:
(http://blacktidestudios.com/images/KaGe/InputTestProj.png)

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.
Title: Re: UICamera.inputHasFocus problem since 3.5.6 update
Post by: ArenMook 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.
Title: Re: UICamera.inputHasFocus problem since 3.5.6 update
Post by: Highstorm on April 23, 2014, 10:11:13 AM
Perfect! Thanks, Aren!