Book Image

NGUI for Unity

By : Charles Bernardoff
Book Image

NGUI for Unity

By: Charles Bernardoff

Overview of this book

Table of Contents (14 chapters)

Saving the nickname


In Chapter 2, Creating Widgets, we entered Nickname in the Auto-save Key parameter of the nickname's UIInput component. It works like this: if the user enters a nickname and presses Return, the input's label string value is saved in the PlayerPrefs() method in the Nickname key.

Here's the issue: the nickname is saved if, and only if, the user presses Return. That's a problem—most of the users will enter a name and select their power directly without pressing Return—I'm sure you've done it yourself too.

We need to save the string in the PlayerPrefs() method even when the user clicks on the Play button without pressing Return.

We must add a line at the end of the OnClick() method of our LaunchValidator.cs script, which will save the nickname's input value before the game scene is loaded. Just before the Application.LoadLevel("Game") line, add the following:

//Save the Nickname to PlayerPrefs before launch
PlayerPrefs.SetString("Nickname", nicknameInput.value);

Now the user's...