学习Unity脚本推荐:Unity3D官网索引
游戏中的数据存储是很常见的问题,比如想在高分榜里显示如下的数据:
下面就是HighScore的制作流程。
首先要搭建好显示的框架,包括背景图片和橙色的边框两个部分,
创建一个空的GameObject用来存放这些GUI,并将其命名为HighscoresOBJ,上面拖拽三个脚本文件。
在这三个脚本文件之前,先创建_GUIClasses.js这个基本的脚本提供相关的类和方法:
import System.Collections.Generic;
// TextureGUI Class: create a basic class for creating and placing GUI elements
// texture = the texture to display
// offset = pixel offset from top left corner, can be modified for easy positioning
class TextureGUI {
var texture:Texture; //useful: texture.width, texture.height
var offset:Vector2; // .x and .y
private var originalOffset:Vector2; //store the original to correctly reset anchor point
enum Point { TopLeft, TopRight, BottomLeft, BottomRight, Center} //what part of texture to position around?
var anchorPoint = Point.TopLeft; // Unity default is from top left corner of texture
function setAnchor() { // meant to be run ONCE at Start.
originalOffset = offset;
if (texture) { // check for null texture
switch(anchorPoint) { //depending on where we want to center our offsets
case anchorPoint.TopLeft: // Unity default, do nothing
break;
case anchorPoint.TopRight: // Take the offset and go to the top right corner
offset.x = originalOffset.x - texture.width;
break;
case anchorPoint.BottomLeft: // bottom left corner of texture
offset.y = originalOffset.y - texture.height;
break;
case anchorPoint.BottomRight: //bottom right corner of texture
offset.x = originalOffset.x - texture.width;
offset.y = originalOffset.y - texture.height;
break;
case anchorPoint.Center: //and the center of the texture (useful for screen center textures)
offset.x = originalOffset.x - texture.width/2;
offset.y = originalOffset.y - texture.height/2;
break;
}
}
}
}
//Timer Class:
class TimerGUI extends TextureGUI { // Extend functionality from TextureGUI for a depreciating timer graphic
var textureLEnd:Texture; // left side of full texture (non stretching part)
var offsetLEnd:Vector2; // left side of full texture (non stretching part) start position
var textureCenter:Texture; // center of timer (will be stretched across width)
var offsetCenter:Vector2;
var textureREnd:Texture;
var offsetREnd:Vector2;
var timerPerct:float = 1; // percentage (0 to 1) this stretches the center
var desiredWidth:float = 403; // max width of the timer in pixels
function setTime(newTime:float) {
timerPerct = newTime; // sets the percent based on value
}
}
// SwitchGUI Class: Extends the TextureGUI to be able to load in multiple textures and switch between them
class SwitchGUI extends TextureGUI {
var switchableTextures = new List.();
var currentTexture:int = 0;
function changeTexture(switchTo:int) {
if (switchTo < switchableTextures.Count && switchTo >= 0) {
texture = switchableTextures[switchTo];
currentTexture = switchTo;
} else {
//Debug.Log( this + ": tried to call invalid part of switchTextures array!");
}
}
function up() {
if ((currentTexture+1) < switchableTextures.Count) {
++currentTexture;
texture = switchableTextures[currentTexture];
} else {
//Debug.Log( this + ": at the top!");
}
}
function nextTexture() {
if ((currentTexture+1) < switchableTextures.Count) { // if we are at the end of the array
++currentTexture;
texture = switchableTextures[currentTexture];
} else {// loop to the beginning
currentTexture = 0;
texture = switchableTextures[currentTexture];
}
}
function down() {
if ((currentTexture-1) >= 0) {
--currentTexture;
texture = switchableTextures[currentTexture];
} else {
//Debug.Log( this + ": at the bottom!");
}
}
}
// Location class:
class Location {
enum Point { TopLeft, TopRight, BottomLeft, BottomRight, Center}
var pointLocation = Point.TopLeft;
var offset:Vector2;
function updateLocation() {
switch(pointLocation) {
case pointLocation.TopLeft:
offset = Vector2(0,0);
break;
case pointLocation.TopRight:
offset = Vector2(Screen.width,0);
break;
case pointLocation.BottomLeft:
offset = Vector2(0,Screen.height);
break;
case pointLocation.BottomRight:
offset = Vector2(Screen.width,Screen.height);
break;
case pointLocation.Center:
offset = Vector2(Screen.width/2,Screen.height/2);
break;
}
}
}
第一个脚本DisplayTextureFullScreen.js用来实现图片的全屏显示:
@script ExecuteInEditMode()
var graphic = TextureGUI(); //(28,23);
var GUIColor:Color;
function OnGUI() {
GUI.color = GUIColor;
if (graphic.texture) {
GUI.DrawTexture(Rect(graphic.offset.x,graphic.offset.y,
Screen.width,Screen.height),
graphic.texture,ScaleMode.StretchToFill,true);
}
}
function AlphaUp(change:float) {
GUIColor.a += change;
}
function setStartColor(color:Color) {
GUIColor = color;
}
function setDelay(delay:float) {
if (GUIColor.a > .5) {
GUIColor.a += delay;
} else {
GUIColor.a -= delay;
}
}
function AlphaDown(change:float) {
GUIColor.a -= change;
}
其中,@script ExecuteInEditMode()这句话的意思是在编辑模式里运行。
脚本默认在play点击是才运行,这样添加之后便可以在Game界面直接看到运行效果。
具体解释:Unity Script Reference传送门
第二个脚本DisplayTexture.js是用来显示高分榜的橙色外框的:
var graphic = TextureGUI();
var startPoint = Location();
var GUIColor:Color = Color.white;
var noGuiStyle : GUIStyle;
function Start() {
graphic.setAnchor();
}
function Update() {
if (graphic.texture){
startPoint.updateLocation();
}
}
function OnGUI() {
if (graphic.texture){
GUI.color = GUIColor;
GUI.Box(Rect(graphic.offset.x+startPoint.offset.x,
graphic.offset.y+startPoint.offset.y,
graphic.texture.width,graphic.texture.height),
graphic.texture,noGuiStyle);
}
}
var levelToLoad:String;
var graphic = TextureGUI(); //(28,23);
var startPoint = Location();
var GUIColor:Color = Color.white;
var noGuiStyle : GUIStyle;
function Start() {
graphic.setAnchor();
}
function Update() {
if (graphic.texture){
startPoint.updateLocation();
}
}
function OnGUI() {
GUI.color = GUIColor;
if (GUI.Button(Rect(graphic.offset.x+startPoint.offset.x,graphic.offset.y+startPoint.offset.y,graphic.texture.width,graphic.texture.height),graphic.texture,noGuiStyle)) {
PlayerPrefs.Save();
Application.LoadLevel(levelToLoad);
}
}
这样再运行项目,就可以看到一个简单框架已经搭好了: