Event delegates

Many NGUI components broadcast events, for which you can set an event delegate—also known as a callback method—executed when the event is triggered. We did it through the Inspector view by assigning the Notify and Method fields when buttons were clicked.

For any type of tween, you can set a specific event delegate for when the tween is finished. We'll see how to do this through code. Before we continue, we must create our callback first. Let's create a callback that loads a new scene.

The callback

Open our MenuManager.cs script, and add this static LoadGameScene() callback method:

public static void LoadGameScene()
{
//Load the Game scene now
Application.LoadLevel("Game");
}

Save the script. The preceding code requests to load the Game scene. To ensure Unity finds our scenes at runtime, we'll need to create the Game scene and add both Menu and Game scenes to the build settings:

  1. Navigate to File | Build Settings.
  2. Click on the Add Current button (don't close the window now).
  3. In Unity, navigate to File | New Scene.
  4. Navigate to File | Save Scene as…
  5. Save the scene as Game.unity.
  6. Click on the Add Current button of the Build Settings window and close it.
  7. Navigate to File | Open Scene and re-open our Menu.unity scene.

Ok, now that both scenes have been added to the build settings, we are ready to link our callback to our event.

Linking a callback to an event

Now that our LoadGameScene() callback method is written, we must link it to our event. We have two solutions. First, we'll see how to assign it using code exclusively, and then we'll create a more flexible system using NGUI's Notify and Method fields.

Code

In order to set a callback for a specific event, a generic solution exists for all NGUI events you might encounter: the EventDelegate.Set() method. You can also add multiple callbacks to an event using EventDelegate.Add().

Add this line at the end of the OnClick() method of DisappearOnClick.cs:

// Set the tween's onFinished event to our LoadGameScene callback
EventDelegate.Set(tween.onFinished, MenuManager.LoadGameScene);

Instead of the preceding line, we can also use the tween-specific SetOnFinished() convenience method to do this. We'll get the exact same result with fewer words:

// Another way to assign our method to the onFinished event
tween.SetOnFinished(MenuManager.LoadGameScene);

Great. If you hit Unity's play button and click on our main menu's Play button, you'll see that our Game scene is loaded as soon as the tween has finished!

Note

It is possible to remove the link of an existing event delegate to a callback by calling EventDelegate.Remove(eventDelegate, callback);.

Now, let's see how to link an event delegate to a callback using the Inspector view.

Inspector

Now that we have seen how to set event delegates through code, let's see how we can create a variable to let us choose which method to call within the Inspector view, like this:

Inspector

The method to call when the target disappears can be set any time without editing the code

The On Disappear variable shown in the preceding screenshot is of the type EventDelegate. We can declare it right now with the following line as a global variable for our DisappearOnClick.cs script:

// Declare an event delegate variable to be set in Inspector
public EventDelegate onDisappear;

Now, let's change the OnClick() method's last line to make sure the tween's onFinished event calls the defined onDisappear callback:

// Set the tween's onFinished event to the selected callback
tween.SetOnFinished(onDisappear);

Ok. Great. Save the script and go to Unity. Select our main menu's Play button: a new On Disappear field has appeared.

Drag UI Root—which holds our MenuManager.cs script—in the Notify field. Now, try to select our MenuManager | LoadGameScene method. Surprisingly, it doesn't appear, and you can only select the script's Exit method… why is that?

That is simply because our LoadGameScene() method is currently static. If we want it to be available in the Inspector view, we need to remove its static property:

  1. Open our MenuManager.cs script.
  2. Remove the static keyword from our LoadGameScene() method.

Save the script and return to Unity. You can now select it in the drop-down list:

Inspector

Great! We have set our callback through the Inspector view; the Game scene will be loaded when the menu disappears.

Now that we have learned how to assign event delegates to callback methods through code and the Inspector view, let's see how to assign keyboard keys to user interface elements.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.102.50