Recommended for learning unity scripts: index on the unity3d Official Website
First, create the script highscoredisplay. js to display guistyle in the inpector panel:
var boxStartLocation:Vector2;var center = Location();function Update() {center.updateLocation();}// draw a text string to the screenvar textGUIStyle : GUIStyle;function OnGUI() {GUI.Box(Rect(center.offset.x + boxStartLocation.x, center.offset.y + boxStartLocation.y,120,30),"This is a title",textGUIStyle);}
Drag the object to the inpector panel:
For each of the many projects in the project, you will know what the function is.
For example, adjusting textcolor can change the font color:
For example, you can set the font after importing the font:
Generally, the font library of win7 is located at c: \ windows \ fonts.
Create an array to store multiple data:
var boxStartLocation:Vector2;var center = Location();function Update() {center.updateLocation();}// draw a text string to the screenvar textGUIStyle : GUIStyle; // to control the display of textvar highscoreName = new String[10]; // array of names for the highscorehighscoreName[0] = "John"; // Highscore #1 = JohnhighscoreName[1] = "Steve";function OnGUI() {GUI.Box(Rect(center.offset.x + boxStartLocation.x, center.offset.y + boxStartLocation.y,120,30),highscoreName[1],textGUIStyle);}
However, after writing and running the code in this way, all the words are stacked together:
So we need a for loop to improve the layout:
var boxStartLocation:Vector2;var center = Location();function Update() {center.updateLocation();}// draw a text string to the screenvar textGUIStyle : GUIStyle; // to control the display of textvar highscoreName = new String[10]; // array of names for the highscorehighscoreName[0] = "www.DigitalTutors.com"; // Highscore #1highscoreName[1] = "Papa";highscoreName[2] = "Kyle";highscoreName[3] = "Tanya";highscoreName[4] = "Delano";highscoreName[5] = "Justin";highscoreName[6] = "Eddie";highscoreName[7] = "Josh";highscoreName[8] = "Chris";highscoreName[9] = "Steve";var heightOffset: float = 10;function OnGUI() {for( var i:int = 0 ; i < 9 ; i++ ) {GUI.Box(Rect(center.offset.x + boxStartLocation.x,center.offset.y + boxStartLocation.y + i * heightOffset,222,34),highscoreName[i],textGUIStyle);}}
In this way, a simple array display is implemented:
Of course, considering the subsequent data display, we 'd better use a class to encapsulate the data:
class Highscore {var name:String;var rounds:int;var kills:int;}var boxStartLocation:Vector2;var center = Location();function Update() {center.updateLocation();}// draw a text string to the screenvar textGUIStyle : GUIStyle; // to control the display of textvar 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";var heightOffset: float = 10;function OnGUI() {for( var i:int = 0 ; i < 9 ; i++ ) {GUI.Box(Rect(center.offset.x + boxStartLocation.x,center.offset.y + boxStartLocation.y + i * heightOffset,222,34),score[i].name,textGUIStyle);GUI.Box(Rect(center.offset.x + boxStartLocation.x,center.offset.y + boxStartLocation.y + i * heightOffset,222,34),score[i].rounds.ToString(),textGUIStyle);GUI.Box(Rect(center.offset.x + boxStartLocation.x,center.offset.y + boxStartLocation.y + i * heightOffset,222,34),score[i].kills.ToString(),textGUIStyle);}}
You can enter the data for testing and add two variables to display the data correctly:
class Highscore {var name:String;var rounds:int;var kills:int;}var boxStartLocation:Vector2;var center = Location();function Update() {center.updateLocation();}// 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 = 999;score[0].kills = 999;var heightOffset: float = 10;var roundsOffset:float;var killsOffset:float;var numberHighscores:int = 8;function OnGUI() {for( var i:int = 0 ; i < numberHighscores ; i++ ) {GUI.Box(Rect(center.offset.x + boxStartLocation.x,center.offset.y + boxStartLocation.y + i * heightOffset,222,34),score[i].name,textGUIStyle);GUI.Box(Rect(center.offset.x + boxStartLocation.x + roundsOffset,center.offset.y + boxStartLocation.y + i * heightOffset,222,34),score[i].rounds.ToString(),roundsGUIStyle);GUI.Box(Rect(center.offset.x + boxStartLocation.x + killsOffset,center.offset.y + boxStartLocation.y + i * heightOffset,222,34),score[i].kills.ToString(),roundsGUIStyle);}}
After running the project, you will find that the basic information is correctly displayed: