[Unity3d] mobile 3D Game Development: how to store and display the highest score (5) -- use textfield to input and adjust the ranking

Source: Internet
Author: User
Tags rounds

Recommended for learning unity scripts: index on the unity3d Official Website


The local storage with the highest score can be achieved in the previous chapter. The next task is to insert the highest score and reduce the remaining scores in the ranking.

The modification in the code is very simple. You can use the for loop to adjust it in sequence:

function AddNewHighscore() {var curRounds = PlayerPrefs.GetInt("highscoreCurrentRounds");var curKills = PlayerPrefs.GetInt("highscoreCurrentKills");var newHighscorePosition: int;// check if the current score are good / above the highscores in the playersPrefs// are we assuming the local and playerprefs data is the same? - Yesfor( var i:int = 0 ; i < numberHighscores ; i++ ) { // for the number of highscoresif (curRounds > score[i].rounds) {// new score is higher!newHighscorePosition = i;break;} else if (curRounds == score[i].rounds && curKills > score[i].kills) {// new score is equal in rounds and kills are greaternewHighscorePosition = i;break;} // if the rounds are equal and kills are less} // if the rounds are lessDebug.Log (newHighscorePosition);//if it is calculate what the # of the new houigschore would be// 1-8// new highscore 4// 1,2,3,4,5,6,7,8// 1,2,3,4,4,5,6,7,8// 1,2,3,4,5,6,7,8// 4 -> 5// ? 5 -> 6// 8 -> 9// 7 -> 8// 6 -> 7if (newHighscorePosition < numberHighscores) {for(i = numberHighscores - 1 ; i >= newHighscorePosition ; i-- ) { // for the number of highscores//8if (i == newHighscorePosition) {// highscore enter codePlayerPrefs.SetString("highscore" + i + "name" , score[i].name); // ask the player for namescore[i].name = score[i].name;PlayerPrefs.SetInt("highscore" + i + "rounds" , curRounds);score[i].rounds = curRounds;PlayerPrefs.SetInt("highscore" + i + "kills" , curKills);score[i].kills = curKills;} else {PlayerPrefs.SetString("highscore" + i + "name" , score[i-1].name);score[i].name = score[i-1].name;PlayerPrefs.SetInt("highscore" + i + "rounds" , score[i-1].rounds);score[i].rounds = score[i-1].rounds;PlayerPrefs.SetInt("highscore" + i + "kills" , score[i-1].kills);score[i].kills = score[i-1].kills;}}}// add it into our data and save it.}

The following question is how the user enters the user name, because the user name with the latest score cannot be correctly displayed:


Textfield in GUI is used here, which is similar to textfield in xcode.

We can search for textfield data on the official website: unity3d index on the official website.

The overload functions officially provided are as follows:

static function TextField (position : Rect, text : String) : Stringstatic function TextField (position : Rect, text : String, maxLength : int) : Stringstatic function TextField (position : Rect, text : String, style : GUIStyle) : Stringstatic function TextField (position : Rect, text : String, maxLength : int, style : GUIStyle) : String

Script case

var stringToEdit : String = "Hello World";function OnGUI () {    // Make a text field that modifies stringToEdit.    stringToEdit = GUI.TextField (Rect (10, 10, 200, 20), stringToEdit, 25);}

We can add the following code to the script:

You can find that there is an additional text box that can be entered after the game is running:

The next step is to establish a connection between textfield and the ranking data so that the data in the input box can be displayed on the ranking.

Add two lines in the Code:

When you run the game again, you will find that the display has changed:

The following figure shows how to hide this ugly input box.

Here we will introduce a new concept: focuscontrol

In short, it is a method for moving the keyboard focus.

The official website provides the following simple cases:

// When pressed the button, selects the "username" Textfield.var username : String = "username";var pwd : String = "a pwd";function OnGUI () {    // Set the internal name of the textfield    GUI.SetNextControlName ("MyTextField");        // Make the actual text field.    username = GUI.TextField (Rect (10,10,100,20), username);    pwd = GUI.TextField (Rect (10,40,100,20), pwd);    // If the user presses this button, keyboard focus will move.    if (GUI.Button (Rect (10,70,80,20), "Move Focus"))        GUI.FocusControl ("MyTextField");    }

Make the following changes in the ongui function:

When you run the project again, the text input box is highlighted, indicating that the focus is obtained.

That is to say, you can directly enter the following information without any clicks:

The next task is to dynamically display text input boxes.

Make the following changes to the Code to hide the text box:

If you run it again, you will find that the input box is no longer available, but you can still enter your name:

The complete script is as follows:

// this is called after a player dies OR from the main menu// Display the highscore// track the follow: Player Name, highscore number of the rounds survived and kills //Upon entering the High Score menu, test if we have a high score and if we do, ask the// player to enter a name and add it to the High Score save data./*class Highscore { // highscore class to hold information specific to one highscorevar name:String; // player name entered at time of highscorevar rounds:int; // number of rounds won in this highscorevar kills:int; // number of kills in this highscore}*/var boxStartLocation:Vector2; // offset of main GUI elementvar center = Location(); // handy class for calculating the center of the screenfunction Start() {Debug.Log("The value of key 'highscoreCurrentRounds' is: " + PlayerPrefs.GetInt("highscoreCurrentRounds"));Debug.Log("The value of key 'highscoreCurrentKills' is: " + PlayerPrefs.GetInt("highscoreCurrentKills"));AddNewHighscore();}function Update() {center.updateLocation(); // every frame, check if the screen has changed and re-calculate the screen center value.//SyncPlayerPrefs();//Start();}// draw a text string to the screenvar textGUIStyle : GUIStyle; // to control the display of textvar roundsGUIStyle : GUIStyle; // to control the display of text for killsvar score = new Highscore[10]; // array of names for the highscorescore[0].name = "www.DigitalTutors.com"; // Highscore #1score[1].name = "Papa";score[2].name = "Kyle";score[3].name = "Tanya";score[4].name = "Delano";score[5].name = "Justin";score[6].name = "Eddie";score[7].name = "Josh";score[8].name = "Chris";score[9].name = "Steve";score[0].rounds = 99;score[0].kills = 999;score[1].rounds = 1;score[1].kills = 9;var heightOffset: float = 10; // how much space to place between each highscore levelvar roundsOffset:float; // horizontal offset for rounds columnvar killsOffset:float; // horizontal offset for kills columnvar numberHighscores:int = 8; // controls how many loops to run to displayvar newName:String = "-type your name-";function OnGUI() {GUI.SetNextControlName ("NameTextField");newName = GUI.TextField(Rect(-100,-100,1,1),newName,30);GUI.FocusControl ("NameTextField");PlayerPrefs.SetString("highscore" + newHighscorePosition + "name" , newName); // ask the player for namescore[newHighscorePosition].name = newName;for( var i:int = 0 ; i < numberHighscores ; i++ ) { // for the number of highscores to displayGUI.Box(Rect(center.offset.x + boxStartLocation.x, // draw a box with the namecenter.offset.y + boxStartLocation.y + i * heightOffset,222,34),score[i].name,textGUIStyle); // using textGUIStyleGUI.Box(Rect(center.offset.x + boxStartLocation.x + roundsOffset, // draw a box with the roundscenter.offset.y + boxStartLocation.y + i * heightOffset,222,34),score[i].rounds.ToString(),roundsGUIStyle); // using roundsGUIStyleGUI.Box(Rect(center.offset.x + boxStartLocation.x + killsOffset, // draw a box with the killscenter.offset.y + boxStartLocation.y + i * heightOffset,222,34),score[i].kills.ToString(),roundsGUIStyle); // using roundsGUIStyle}}// Highscore naming scheme// key starts : "highscore"// past that, whatever the setting needs to be// a specific high score would look like this:// highscore4namevar newHighscorePosition: int;function AddNewHighscore() {var curRounds = PlayerPrefs.GetInt("highscoreCurrentRounds");var curKills = PlayerPrefs.GetInt("highscoreCurrentKills");// check if the current score are good / above the highscores in the playersPrefs// are we assuming the local and playerprefs data is the same? - Yesfor( var i:int = 0 ; i < numberHighscores ; i++ ) { // for the number of highscoresif (curRounds > score[i].rounds) {// new score is higher!newHighscorePosition = i;break;} else if (curRounds == score[i].rounds && curKills > score[i].kills) {// new score is equal in rounds and kills are greaternewHighscorePosition = i;break;} // if the rounds are equal and kills are less} // if the rounds are lessDebug.Log (newHighscorePosition);//if it is calculate what the # of the new houigschore would be// 1-8// new highscore 4// 1,2,3,4,5,6,7,8// 1,2,3,4,4,5,6,7,8// 1,2,3,4,5,6,7,8// 4 -> 5// ? 5 -> 6// 8 -> 9// 7 -> 8// 6 -> 7if (newHighscorePosition < numberHighscores) {for(i = numberHighscores - 1 ; i >= newHighscorePosition ; i-- ) { // for the number of highscores//8if (i == newHighscorePosition) {// highscore enter codePlayerPrefs.SetString("highscore" + i + "name" , score[i].name); // ask the player for namescore[i].name = score[i].name;PlayerPrefs.SetInt("highscore" + i + "rounds" , curRounds);score[i].rounds = curRounds;PlayerPrefs.SetInt("highscore" + i + "kills" , curKills);score[i].kills = curKills;} else {PlayerPrefs.SetString("highscore" + i + "name" , score[i-1].name);score[i].name = score[i-1].name;PlayerPrefs.SetInt("highscore" + i + "rounds" , score[i-1].rounds);score[i].rounds = score[i-1].rounds;PlayerPrefs.SetInt("highscore" + i + "kills" , score[i-1].kills);score[i].kills = score[i-1].kills;}}}// add it into our data and save it.}function SyncPlayerPrefs() {// our assumption is that the data in player prefs should over-ride the local data// need this to see if we have player prefs keys and if we do, copy into our local// if the key does not exist, we need to populate it with the local datafor( var i:int = 0 ; i < numberHighscores ; i++ ) { // for the number of highscoresif (PlayerPrefs.HasKey("highscore" + i + "name" )) {//copy Player preferences value to local highscore class arrayscore[i].name = PlayerPrefs.GetString("highscore" + i + "name");} else {//set that player prefs to local dataPlayerPrefs.SetString("highscore" + i + "name" , score[i].name);}if (PlayerPrefs.HasKey("highscore" + i + "rounds" )) {//copy Player preferences value to local highscore class arrayscore[i].rounds = PlayerPrefs.GetInt("highscore" + i + "rounds");} else {//set that player prefs to local dataPlayerPrefs.SetInt("highscore" + i + "rounds" , score[i].rounds);}if (PlayerPrefs.HasKey("highscore" + i + "kills" )) {//copy Player preferences value to local highscore class arrayscore[i].kills = PlayerPrefs.GetInt("highscore" + i + "kills");} else {//set that player prefs to local dataPlayerPrefs.SetInt("highscore" + i + "kills" , score[i].kills);}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.