学习Unity脚本推荐:Unity3D官网索引
首先创建脚本HighScoreDisplay.js用来在Inpector面板中显示GUIStyle:
var boxStartLocation:Vector2; var center = Location(); function Update() { center.updateLocation(); } // draw a text string to the screen var textGUIStyle : GUIStyle; function OnGUI() { GUI.Box(Rect(center.offset.x + boxStartLocation.x, center.offset.y + boxStartLocation.y,120,30),"This is a title",textGUIStyle); }
里面的诸多项目每一个稍加调试便知道作用是什么了。
比如调节TextColor可以改变字体的颜色:
比如导入字体之后可以设置字体:
一般Win7的字体库位置是C:\Windows\Fonts。
下面创建一个数组来存放多个数据:
var boxStartLocation:Vector2; var center = Location(); function Update() { center.updateLocation(); } // draw a text string to the screen var textGUIStyle : GUIStyle; // to control the display of text var highscoreName = new String[10]; // array of names for the highscore highscoreName[0] = "John"; // Highscore #1 = John highscoreName[1] = "Steve"; function OnGUI() { GUI.Box(Rect(center.offset.x + boxStartLocation.x, center.offset.y + boxStartLocation.y,120,30),highscoreName[1],textGUIStyle); }
所以还需要一个for循环改善一下排版:
var boxStartLocation:Vector2; var center = Location(); function Update() { center.updateLocation(); } // draw a text string to the screen var textGUIStyle : GUIStyle; // to control the display of text var highscoreName = new String[10]; // array of names for the highscore highscoreName[0] = "www.DigitalTutors.com"; // Highscore #1 highscoreName[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); } }这样就实现了简单的数组显示:
当然考虑到后面的数据显示,我们最好使用一个Class把数据封装起来:
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 screen var textGUIStyle : GUIStyle; // to control the display of text var score = new Highscore[10]; // array of names for the highscore score[0].name = "www.DigitalTutors.com"; // Highscore #1 score[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); } }
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 screen var textGUIStyle : GUIStyle; // to control the display of text var roundsGUIStyle : GUIStyle; // to control the display of text for kills var score = new Highscore[10]; // array of names for the highscore score[0].name = "www.DigitalTutors.com"; // Highscore #1 score[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); } }