[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);}}
这个时候再运行项目就会发现基本都可以正确的显示出来了:
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(三)----GUI Style与数组的使用
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(二)----使用GUI.Box显示文字
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(一)----初始化显示的高分榜
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(六)----实现文字闪烁效果
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(八)----用TouchScreenKeyboard弹出键盘
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(九)----使用PreviewLabs插件提速与完整的项目源码
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(四)----使用PlayerPrefs存储数据到本地
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(五)----使用TextField 输入并调整排名
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(七)----使用Game ID避免数据重复输入
- [Unity3D]手机3D游戏开发:场景切换与数据存储(PlayerPrefs 类的介绍与使用)
- [Unity3D]手机3D游戏开发:如何使用Unity3D中自带的重力感应
- Unity3D游戏开发之GUI的使用
- [Unity3D]手机3D游戏开发:使用UnityRemote开发一个简单的FPS案例
- 强大的游戏开发工具Unity3D推出2D开发工具,unity将混合3D与2D开发
- [Unity3D]手机3D游戏开发:FPS射击游戏中瞄准镜CrossHair的制作
- Unity3D 与udk 3D游戏动漫引擎的开发特点
- Unity3D 与udk 3D游戏动漫引擎的开发特点
- android游戏开发框架libgdx的使用(三)--中文显示与汉字绘制
- 封装自己的Flex工具_Tree带checkbox渲染器
- Hoj 2979 Escape from Pyramids
- Yii图片生成水印
- Hadoop学习笔记
- js 字符串 匹配问题
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(三)----GUI Style与数组的使用
- 《Python3程序开发指南》02:文件的写入(简单)
- fd_set
- 深入理解管理“八心”系列-期望之心
- HDU2700:Parity
- JAX-WS(java WebService编程)第一篇
- ios开发之ASIHTTPRequest-Cookie的使用
- php-cgi(php-fpm) CPU 100% 与 file_get_contents 函数的关系
- 有想像力的程序员才有未来(二)