[Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(三)----GUI Style与数组的使用
2013-01-25 17:48
1146 查看
学习Unity脚本推荐:Unity3D官网索引
首先创建脚本HighScoreDisplay.js用来在Inpector面板中显示GUIStyle:
拖拽到物体上,此时再看Inpector面板:
里面的诸多项目每一个稍加调试便知道作用是什么了。
比如调节TextColor可以改变字体的颜色:
比如导入字体之后可以设置字体:
一般Win7的字体库位置是C:\Windows\Fonts。
下面创建一个数组来存放多个数据:
但是这样编写运行后发现所有的字都叠在了一起:
所以还需要一个for循环改善一下排版:
当然考虑到后面的数据显示,我们最好使用一个Class把数据封装起来:
我们可以填写填写数据进行测试,同时添加两个变量实现数据的正确显示:
这个时候再运行项目就会发现基本都可以正确的显示出来了:
首先创建脚本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); }
拖拽到物体上,此时再看Inpector面板:
里面的诸多项目每一个稍加调试便知道作用是什么了。
比如调节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); } }
这个时候再运行项目就会发现基本都可以正确的显示出来了:
相关文章推荐
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(二)----使用GUI.Box显示文字
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(四)----使用PlayerPrefs存储数据到本地
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(九)----使用PreviewLabs插件提速与完整的项目源码
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(七)----使用Game ID避免数据重复输入
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(五)----使用TextField 输入并调整排名
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(六)----实现文字闪烁效果
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(一)----初始化显示的高分榜
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(八)----用TouchScreenKeyboard弹出键盘
- [Unity3D]手机3D游戏开发:场景切换与数据存储(PlayerPrefs 类的介绍与使用)
- [Unity3D]手机3D游戏开发:如何使用Unity3D中自带的重力感应
- [Unity3D]手机3D游戏开发:使用UnityRemote开发一个简单的FPS案例
- unity3d 中使用C#脚本 实现数组的倒序存储并输出
- [Unity3D]手机3D游戏开发:FPS射击游戏中瞄准镜CrossHair的制作
- [Unity3D]手机3D游戏开发:关于自定义Joystick的相关设置和脚本源码
- 游戏开发设计模式之原型模式 & unity3d JSON的使用(unity3d 示例实现)
- [Unity3D]手机3D游戏开发:简单的移动和缩放鼠标手势脚本源码
- 游戏开发设计模式之原型模式 & unity3d JSON的使用(unity3d 示例实现)
- [转] DirectX开发的游戏如何使用DirectXTex实现截屏
- unity3d使用GUI实现打字机的文字显示效果
- Unity3D ARPG游戏开发《最初的幻想》之GUI背包系统实现