[Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(三)----GUI Style与数组的使用

来源:互联网 发布:宜昌血流成河麻将软件 编辑:程序博客网 时间:2024/05/21 18:42

学习Unity脚本推荐:Unity3D官网索引

首先创建脚本HighScoreDisplay.js用来在Inpector面板中显示GUIStyle:

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);}

拖拽到物体上,此时再看Inpector面板:

里面的诸多项目每一个稍加调试便知道作用是什么了。

比如调节TextColor可以改变字体的颜色:

比如导入字体之后可以设置字体:

一般Win7的字体库位置是C:\Windows\Fonts。

下面创建一个数组来存放多个数据:

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);}

但是这样编写运行后发现所有的字都叠在了一起:


所以还需要一个for循环改善一下排版:

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);}}
这样就实现了简单的数组显示:

当然考虑到后面的数据显示,我们最好使用一个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 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);}}

我们可以填写填写数据进行测试,同时添加两个变量实现数据的正确显示:

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);}}

这个时候再运行项目就会发现基本都可以正确的显示出来了: