經過前五節簡單介紹後今天就來利用這些東西寫個小遊戲吧
1 首先我們場景上先佈置出如下圖的布局
2 創二個空物件取名為01.UI(放介面用的)及02.OBS(放遊戲出現的物件用的)
01.UI下面分別創三個GUIText的GameObject(選單有)布局如以下三張顯示的Inspector
02.OBS下面創一個空物件(空的就對了)要製作一個球的發射器用
3 再來在資料夾Scripts中(沒有自行創建)寫下四張C#程式碼如下圖及程式碼再下面
4 因為遊戲需要記分所以寫了一個BallData的類別如下
public class BallData
{
public int score;
public int tatleScore;
public int count;
}
5 這個類別是要綁在01.GUI這個物件上主要是記分用
在start時候利用GameOjbect.Find(string).GetComponent<type>的方法來取得剛剛場景上創的GUIText
shootUpdateForUI傳入BallData(就是剛剛創的類別)這邊是更新球射出一次就邊更一次次數並顯示
touchBallUpdateForUI傳入BallData,這邊是更新點到球以後就加多少分數
using UnityEngine;
using System.Collections;
public class GameUIManager : MonoBehaviour
{
public GUIText ballCount;
public GUIText gameName;
public GUIText scoreCount;
//初始化取得三個GUItext的資料前提是那物件上必須要有綁上GUIText唷
void Start ()
{
ballCount = GameObject.Find ("BallCount").GetComponent<GUIText>();
gameName = GameObject.Find ("GameName").GetComponent<GUIText>();
scoreCount = GameObject.Find ("ScoreCount").GetComponent<GUIText>();
}
//更新發射次數
public void shootUpdateForUI(BallData data)
{
ballCount.text = data.count.ToString();
}
//更新點擊分數
public void touchBallUpdateForUI(BallData data)
{
scoreCount.text =data.tatleScore.ToString();
}
}
6 這裡算是球更新資料的中繼站綁在camera上面
BallData也是在這邊的INIT才實例化
之所以特地再透過這腳本傳到GameUIManager主要目的是為了告知camera的另一個妙用
using UnityEngine;
using System.Collections;
public class GameDataManager : MonoBehaviour {
// Use this for initialization
public GameUIManager gameUIManager;
public BallData ballData;
// 因為要更新UI資料所以要FIND 01.UI上面的GameUIManager.cs
void Start ()
{
gameUIManager = GameObject.Find ("01.UI").GetComponent<GameUIManager>();
init ();
//StartCoroutine(LoadAssetBunlder());
}
//把BallData實例化並使用也只有在init才會new一次 確保遊戲過程只有一個
void init()
{
ballData = new BallData();
ballData.score = 0;
ballData.tatleScore = 0;
ballData.count = 0;
}
//球每射出一顆就會呼叫這一次
void shootUpdate()
{
ballData.count += 1;
gameUIManager.shootUpdateForUI(ballData);
}
//球每被點擊中一顆就會呼叫這一次
void scoreUpdate(int score)
{
ballData.tatleScore += score;
gameUIManager.touchBallUpdateForUI(ballData);
}
}
7 這裡就是球射出的邏輯請將此腳本綁在02.OBS/shoot(也就是那空物件)
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour
{
// Use this for initialization
public float f_shootTime = 1;//每一秒射一次
float f_tmpShootTime = 0;//只是為了計算過了幾毫秒
//設定球要射出的力道 的最大最小值
public int i_shootPos_x = 350;
public int i_shootPos_y = 450;
public int i_shootPos_z = 100;
void Start ()
{
init ();
}
// Update is called once per frame
void Update ()
{
if(isShoot())//如果這裡回傳true就設球 寫在下面
{
shoot("Ball");發射球傳入字串是因為要去Resource取得這球
}
}
void init()
{
f_tmpShootTime = 0;
}
//判斷是否可以發射了
bool isShoot()
{
bool isshoot;
f_tmpShootTime +=Time.deltaTime;
//這邊只用到一個API Time.deltaTime 這是每一禎就會回傳上一禎到這一禎經過了幾毫秒的API 設下只是計算是否大於以秒要做甚麼
if(f_tmpShootTime>f_shootTime)
{
isshoot = true;
f_tmpShootTime = Time.deltaTime;
}
else
{
isshoot = false;
}
return isshoot;
}
//射球就靠這裡了
void shoot(string name)
{
//Instantiate主要是實例化物件
//Resources.Load主要是取得Resources裡面的prefab來使用所以我有在OBS這資料夾下方了一個Ball的Prefab
GameObject ball = Instantiate(Resources.Load("OBS/"+name),gameObject.transform.position,gameObject.transform.rotation) as GameObject;
//AddComponent是替這物件加上這腳本
ball.AddComponent<TouchBall>();//這個腳本在下面
ball.AddComponent<Rigidbody>();//幫他增加鋼体這樣才能使用物理引擎
ball.GetComponent<TouchBall>().ballScore = 2;//這個腳本上有個參數可以設定
ball.GetComponent<Rigidbody>().useGravity=true;//使用物理引擎
ball.GetComponent<SphereCollider>().isTrigger = true;//是否可以穿透
Camera.main.gameObject.SendMessage("shootUpdate");//這裡就是呼叫剛剛在camera上面的腳本寫的方法用的 很方便吧
ball.transform.rigidbody.AddForce(setShootForce());//這就是位甚麼要加鋼体的原因才能使用AddForce給一個力道讓球飛出去
Destroy(ball,3);//3秒後移除這個ball
}
//這邊是設定要發射的力道是多少
Vector3 setShootForce()
{
int tmpX = Random.Range(-i_shootPos_x,i_shootPos_x);
Vector3 tmpVector3=new Vector3(tmpX,i_shootPos_y,i_shootPos_z);
return tmpVector3;
}
}
8 在做最後一個腳本前 先製作一個球體的gameObject並拉到如圖的位置上做成prefab吧這樣剛剛上面的Resources.Load才有東西能讀阿
9 下面就是這最後一個腳本拉 void OnMouseUpAsButton()
這是繼承了MonoBehaviour後可以複寫的功能 主要是給你點擊螢幕後能處理的事情
這是只有unity4.x以上才支援手機喔 不然只能用滑鼠點
using UnityEngine;
using System.Collections;
public class TouchBall : MonoBehaviour
{
//OnMouseUpAsButton只有当鼠标在同一个GUIElement或Collider按下,在释放时调用。
public int ballScore=1;
void OnMouseUpAsButton()
{
Camera.main.gameObject.SendMessage("scoreUpdate",ballScore);
Destroy(gameObject);
}
}
到這邊就完成一半拉可以執行玩看看喔