Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: cpt_barricade on March 22, 2014, 12:36:28 PM

Title: Unable to Set Focus on a Button with UIButtons
Post by: cpt_barricade on March 22, 2014, 12:36:28 PM
Hi folks, I was hoping to get some help on using NGUI as a pause menu. Before I jump into the problems I'm having I'll do a quick runthrough of how I've gotten the pause menu into the scene.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PauseGame : MonoBehaviour
  5. {
  6.         // Boolean used to toggle pause and play.
  7.         private bool pauseGame;
  8.         private GameObject pauseScreen;
  9.         void Start ()
  10.         {
  11.                 // By default the game should not be paused.
  12.                 pauseGame = false;
  13.                 // Load the pause prefab into the scene.
  14.                 pauseScreen = (GameObject)Instantiate(Resources.Load("PauseMenu"));
  15.                 // Set the pause screen to invisible until the pause button is pressed.
  16.                 pauseScreen.SetActive(false);
  17.                 // Game proceeds normally when timeScale = 1.
  18.                 Time.timeScale = 1;
  19.  
  20.                 // Disable seeing the cursor on-screen
  21.                 Screen.showCursor = false;
  22.         }
  23.        
  24.         void Update ()
  25.         {
  26.                 if(Input.GetButtonDown("Pause"))
  27.                 {
  28.                         // Either pause or unpause the game.
  29.                         // If the game isn't paused i.e. the player is playing, pause the game.
  30.                         if(pauseGame == false)
  31.                         {
  32.                                 pauseScreen.SetActive(true);
  33.                                 pauseGame = true;
  34.                                 Time.timeScale = 0;
  35.                                 Screen.showCursor = true;
  36.                         }
  37.                         // If game is paused and the pause button is pressed again, unpause.
  38.                         else if(pauseGame == true)
  39.                         {
  40.                                 pauseScreen.SetActive(false);
  41.                                 pauseGame = false;
  42.                                 Time.timeScale = 1;
  43.                                 Screen.showCursor = false;
  44.                         }
  45.                 }
  46.         }
  47. }
  48.  

Basically when the user presses the pause button the pause screen prefab is shown. I'm running into a fairly annoying problem that's affecting controller support, namely:


Any help on this matter would be greatly appreciated. I have the funny feeling this problem is a result of my inexperience with NGUI.
Title: Re: Unable to Set Focus on a Button with UIButtons
Post by: ArenMook on March 22, 2014, 05:30:29 PM
Never use Instantiate() with UI elements.

Use NGUITools.AddChild(parent, prefab). Look inside that function to find out why.

UIButtonKeys has "select on click" option that should be set to something, but since you are instantiating a new object yourself, it's then up to you to actually select something inside of it (like the "OK" button). Note that in the controller menu example both windows are present in the scene -- one is simply disabled. I'd advise you to do the same thing with your pause menu.