Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: outerringz on December 22, 2012, 05:29:58 PM

Title: Replace left mouse click with some other key
Post by: outerringz on December 22, 2012, 05:29:58 PM
I am trying to figure out how to map all UI "use" interaction to a key code instead of a mouse click. I found the post about triggering a button with a shortcut key when it's not focused (http://www.tasharen.com/forum/index.php?topic=1148.0), but I want to change the "use" key for all UI elements in a panel, and still require focus.

My usage scenario is a fps where left mouse click is mapped to Fire1, and I want in-game UI elements that can be interacted with using rays from the UICamera on the Main Camera and triggered with a "use" key code.

I tried to modify the example above by simply changing [!UICamera.inputHasFocus] to [UICamera.inputHasFocus], and placed this script on a button and a checkbox, but it's not working.

Any input you can offer is appreciated.

  1.         void Update ()
  2.         {      
  3.                 if (UICamera.inputHasFocus)
  4.                 {
  5.                         if (Input.GetKeyDown(KeyCode.E))
  6.                         {
  7.                                 SendMessage("OnPress", true, SendMessageOptions.DontRequireReceiver);  
  8.                         }
  9.                        
  10.                         if (Input.GetKeyUp(KeyCode.E))
  11.                         {
  12.                                 SendMessage("OnPress", false, SendMessageOptions.DontRequireReceiver);
  13.                                 SendMessage("OnClick", SendMessageOptions.DontRequireReceiver);
  14.                         }
  15.                 }
  16.         }
  17.  
Title: Re: Replace left mouse click with some other key
Post by: ArenMook on December 23, 2012, 06:56:14 AM
Er... what? NGUI already takes care of sending all those events for you. All you need is a UICamera script on each camera that will see interactable objects, and actually implement your OnPress/OnClick etc functions in a script attached to the said objects.
Title: Re: Replace left mouse click with some other key
Post by: outerringz on December 23, 2012, 08:32:15 AM
By default, NGUI buttons, checkboxes, slider, etc. are all activated with the left mouse click, I want to activate with  keycode.E instead of the left click.
Title: Re: Replace left mouse click with some other key
Post by: ArenMook on December 23, 2012, 06:27:01 PM
Input.GetKeyDown(KeyCode.E) will tell you when the key was pressed. UICamera.hoveredObject will tell you what's under the mouse. UICamera.inputHasFocus will tell you if you are currently typing or not. You should only have one such script in the game, and use UICamera.hoveredObject.SendMessage instead of just SendMessage, so that it goes to the correct object.
Title: Re: Replace left mouse click with some other key
Post by: outerringz on December 23, 2012, 11:02:17 PM
Thanks, UICamera.hoveredObject.SendMessage is what I needed.