Author Topic: Using CancelKey to "back out" of submenu  (Read 5245 times)

getluky

  • Guest
Using CancelKey to "back out" of submenu
« on: July 20, 2012, 02:17:57 PM »
Since OnKey is sent only to the currently selected UIWidget in the scene, is there an efficient way to set up a listener for CancelKey to trigger an animation on an entire submenu? For example, most games that support joysticks allow you to "back out" of a current menu or scene quickly by pressing a cancel button. I'd like to support this, but aside from writing a custom script and attaching it to *every* UIWidget in the scene that might be selectable, I'm not sure how.

It would be great if this common use case for controllers was supported within the NGUI Controller Example as well, where in the Options the cancel key would appropriately cancel the selection of the drop-down menus if one was active, and otherwise it would animate out the Options Menu and bring the main menu back.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using CancelKey to "back out" of submenu
« Reply #1 on: July 20, 2012, 03:43:03 PM »
Nothing is preventing you from having a script that has this inside:

  1. if (Input.GetKeyDown(KeyCode.Escape)) CloseYourWindow();

getluky

  • Guest
Re: Using CancelKey to "back out" of submenu
« Reply #2 on: July 20, 2012, 05:00:17 PM »
Ah! Obvious in hindsight. Thanks again.

getluky

  • Guest
Re: Using CancelKey to "back out" of submenu
« Reply #3 on: July 20, 2012, 09:38:35 PM »
For future reference, here is what I ended up with after a couple of approaches. This takes advantage of existing wiring for back or cancel buttons, so you just put it on a panel that would be visible when the CancelKey would need to be processed, and point the target parameter at an existing back/cancel button.

It also supports specifying selection exceptions, so that if your panel contains UIWidgets that also receive cancel key presses (such as popup lists), it won't trigger this simulated OnClick if any of them are selected.

  1.  
  2. using UnityEngine;
  3. using System.Collections;
  4. [AddComponentMenu("NGUI/Extensions/Cancel Key Simulate OnClick")]
  5. public class CancelKeySimulateOnClick : MonoBehaviour {
  6.        
  7.         public GameObject target;
  8.        
  9.         public UICamera camera;
  10.         public GameObject [] selectionExceptions;
  11.        
  12.         // Update is called once per frame
  13.         void Update () {
  14.                 if (target != null && Input.GetKeyUp(camera.cancelKey0) || Input.GetKeyUp(camera.cancelKey1)) {
  15.                         if (selectionExceptions.Length > 0) {
  16.                                 foreach (GameObject selection in selectionExceptions) {
  17.                                         if (selection == UICamera.selectedObject) {
  18.                                                 return;
  19.                                         }
  20.                                 }
  21.                         }
  22.                         target.SendMessage("OnClick", SendMessageOptions.DontRequireReceiver);
  23.                 }
  24.         }
  25. }
  26.  

gekido

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: Using CancelKey to "back out" of submenu
« Reply #4 on: June 01, 2014, 11:41:30 PM »
Thanx @getlucky - this works well! Was just trying to sort out a solution for the same issue, and stumbled upon your script. Thanx for posting!