#感覺這門課作業不提前寫真搞不定啊_(:зゝ∠)_
#文末有全部代碼以及操作方法
先把游戲效果po一下吧(白色膠囊體代表牧師、紅色膠囊體代表魔鬼、藍色代表船、兩條白色圓柱體代表兩岸):
這次作業與TA的實現方法稍有不同,多用了兩個類:PersonStatus(動態加載到6個牧師和惡魔上)用來管理角色上船(即下岸)以及上岸(即下船)的行為;BoatBehaviour(動態加載到船上)用來管理船的移動行為。同時增加了一個IGameJudge接口,用于判斷勝利/失敗行為。其他的應該大致相同,UML圖大概像下面的(第一次畫UML圖, 不知道有沒有錯……):
【作業要求】:
1、BaseCode 腳本:
此腳本主要作用的在同一個命名空間內定義單例類、定義接口。
由于需要使用單例模式,所以先聲明一個命名空間Com.MyGame。然后在里面定義單例類mainSceneController。由于僅定義了一個單例類,所以并不需要掛載到Main Camera上。
2、GenGameObjects 腳本:
此腳本主要作用是創建所有的游戲對象GameObject,同時實現一些游戲操作邏輯。
(1) 這個腳本需要掛載到Main Camera上。
(2) 在此腳本需要先創建牧師、惡魔、船、兩岸這些GameObject(用幾何體代替)。創建后還需要給游戲對象加上名字(有些需要 加上tag用于后面辨識),設置大小、旋轉角度、顏色、位置等屬性;對于牧師、惡魔、船這幾種游戲對象,還要分別綁定上面提到的PersonStatus、BoatBehaviour腳本。(下面僅放其中一種游戲對象的創建代碼):
PRiests = new List<GameObject>();for (int i = 0; i < 3; i++) { GameObject priests = GameObject.CreatePrimitive(PrimitiveType.Capsule); priests.name = "Priest " + (i + 1); priests.tag = "Priest"; priests.GetComponent<MeshRenderer>().material.color = Color.white; priests.AddComponent<PersonStatus>(); Priests.Add(priests);}Priests[0].transform.position = LOCATION_SET.priests_1_LOC;Priests[1].transform.position = LOCATION_SET.priests_2_LOC;Priests[2].transform.position = LOCATION_SET.priests_3_LOC;值得注意的是 LOCATION_SET.priests_1_LOC 是什么??
這是我在BaseCode 腳本里面的Com.MyGame命名空間內先把所有人和物的位置(上岸、下船等一系列位置)先確定,然后在其他的腳本就可以調用靜態變量獲得位置的值了,像下面的:
public class LOCATION_SET { public static Vector3 priests_1_LOC = new Vector3(5, 3, 0); public static Vector3 priests_2_LOC = new Vector3(6.3f, 3, 0); public static Vector3 priests_3_LOC = new Vector3(7.6f, 3, 0); public static Vector3 devils_1_LOC = new Vector3(9.5f, 3, 0); public static Vector3 devils_2_LOC = new Vector3(10.8f, 3, 0); public static Vector3 devils_3_LOC = new Vector3(12.1f, 3, 0); public static Vector3 boat_left_LOC = new Vector3(-3, 0, 0); public static Vector3 boat_right_LOC = new Vector3(3, 0, 0); public static Vector3 bank_left_LOC = new Vector3(-8.5f, 1.5f, 0); public static Vector3 bank_right_LOC = new Vector3(8.5f, 1.5f, 0); public static Vector3 boatLeft_Pos_1 = new Vector3(-3.7f, 1.5f, 0); public static Vector3 boatLeft_Pos_2 = new Vector3(-2.3f, 1.5f, 0); public static Vector3 boatRight_Pos_1 = new Vector3(2.3f, 1.5f, 0); public static Vector3 boatRight_Pos_2 = new Vector3(3.7f, 1.5f, 0);}3、mainSceneController與GenGameObjects 的交互:把GenGameObjects 注入到mainSceneController單例模式作為子對象public class mainSceneController : System.Object, IUserActions, IGameJudge { private static mainSceneController instance; private GenGameObjects myGenGameObjects; public static mainSceneController getInstance() { if (instance == null) instance = new mainSceneController(); return instance; } internal void setGenGameObjects(GenGameObjects _myGenGameObjects) { if (myGenGameObjects == null) { myGenGameObjects = _myGenGameObjects; } }}同時在GenGameObjects 腳本Start()函數里:mainSceneController.getInstance().setGenGameObjects(this);4、接下來是玩家操作,少的可以分5種:船的移動、牧師上船、牧師上岸、惡魔上船、惡魔上岸;多的可以分7種,即兩種人物上岸可以上左岸或右岸。我這里只讓玩家做5種操作(上左/右岸會按照船的??课恢脕泶_定,可以自動識別)。(1) 在BaseCode 腳本添加IUserActions接口:
public interface IUserActions { void boatMove(); void priestsGetOn(); void priestsGetOff(); void devilsGetOn(); void devilsGetOff();}(2) 然后在mainSceneController實現IUserActions接口:由于mainSceneController有GenGameObjects子對象,且所有GameObject都在GenGameObjects腳本里創建,所以我在GenGameObjects里真正實現IUserActions接口的五個方法,而在mainSceneController僅通過GenGameObjects子對象調用自身的方法即可:
public class mainSceneController : System.Object, IUserActions { private static mainSceneController instance; private GenGameObjects myGenGameObjects; public static mainSceneController getInstance() { if (instance == null) instance = new mainSceneController(); return instance; } internal void setGenGameObjects(GenGameObjects _myGenGameObjects) { if (myGenGameObjects == null) { myGenGameObjects = _myGenGameObjects; } } /** * 實現IUserActions接口 **/ public void boatMove() { myGenGameObjects.boatMove(); } public void devilsGetOff() { myGenGameObjects.devilsGetOff(); } public void devilsGetOn() { myGenGameObjects.devilsGetOn(); } public void priestsGetOff() { myGenGameObjects.priestsGetOff(); } public void priestsGetOn() { myGenGameObjects.priestsGetOn(); }}5個函數的具體邏輯實現在這里就不放出來了,因為挺繁瑣的_(:зゝ∠)_。5、UserInterface腳本:
此腳本的主要作用是:監聽玩家的操作,并調用IUserActions接口的方法。
我是用MonoBehaviour提供的OnGUI()方法設置了5個按鈕Button分別監聽。代碼就不在這放了,詳見文末完整代碼。
然后創建一個Empty對象,并掛載上去。
6、使用 C# 集合類型組織對象,我是使用List<GameObject>,感覺這個挺好用的。mainSceneController 對象與GenGameObjects 互動完成游戲邏輯上面也提到了。下面提一下新添的兩個類一個新添的接口吧:
(1) PersonStatus類:
這個類動態綁定到每個牧師和惡魔上。主要作用是定義控制自身行為,即上船/上岸兩個方法,并提供給GenGameObjects 在合適情況下調用。同時,PersonStatus類有 位置 和 在船上/岸上 等狀態的成員變量,實現了每個牧師/惡魔自己管理自己(上船/上岸 自身的位置交換),而不需要在一個類里面統一管理,更加方便而且直觀。
(2) BoatBehaviour類:
這個類動態綁定到船上。主要作用是控制船的自身移動,而不需要在其他類里面管理(由于船的移動是通過Update()方法一點點的修
改位置來變化)。此類會提供方法給GenGameObjects 在合適情況下調用,使船移動,同時檢測是否靠岸。
(3) IGameJudge接口:
此接口通過mainSceneController實現。mainSceneController 內會先聲明一些變量來提示岸上/船上的牧師/惡魔的數量,
IGameJudge接口同樣有改變那幾個變量的函數(牧師/惡魔上船/上岸的時候PersonStatus類會調用接口的方法),同時有一個檢測
游戲是否勝利/失敗的函數(船靠岸的時候BoatBehaviour類會調用接口的方法)。
public interface IGameJudge { void modifyBoatPriestsNum(bool isAdd); void modifyBoatDevilsNum(bool isAdd); void modifyBankPriestsNum(bool isLeftBank, bool isAdd); void modifyBankDevilsNum(bool isLeftBank, bool isAdd); void judgeTheGame(bool isBoatLeft);}7、船未靠岸通過一個bool變量isMoving來控制玩家不能夠操作。
8、游戲勝利、結束的提示文字需要先把GUI文字做出prefab,然后游戲結束的時候動態生成
void showGameText(string textContent) { GameObject Canvas = Camera.Instantiate(Resources.Load("Prefab/Canvas")) as GameObject; GameObject GameText = Camera.Instantiate(Resources.Load("Prefab/GameText"), Canvas.transform) as GameObject; GameText.transform.position = Canvas.transform.position; GameText.GetComponent<Text>().text = textContent;}項目的組織結構:
9、整個游戲沒有出現 Find 游戲對象, SendMessage 這類突破程序結構的通訊耦合語句。
寫這篇東西真夠嗆_(:зゝ∠)_
寫這篇東西真夠嗆_(:зゝ∠)_
寫這篇東西真夠嗆_(:зゝ∠)_
重要事情說三遍……不敢想象后面的作業……
好吧,下面就是完整的代碼:1、BaseCode.cs(不需要掛載到主攝像機):
using UnityEngine;using System.Collections;using System;using UnityEngine.UI;namespace Com.MyGame { public class DIRECTION { public static bool Left = true; public static bool Right = false; } public class MODIFICATION { public static bool Add = true; public static bool Sub = false; } public class LOCATION_SET { public static Vector3 priests_1_LOC = new Vector3(5, 3, 0); public static Vector3 priests_2_LOC = new Vector3(6.3f, 3, 0); public static Vector3 priests_3_LOC = new Vector3(7.6f, 3, 0); public static Vector3 devils_1_LOC = new Vector3(9.5f, 3, 0); public static Vector3 devils_2_LOC = new Vector3(10.8f, 3, 0); public static Vector3 devils_3_LOC = new Vector3(12.1f, 3, 0); public static Vector3 boat_left_LOC = new Vector3(-3, 0, 0); public static Vector3 boat_right_LOC = new Vector3(3, 0, 0); public static Vector3 bank_left_LOC = new Vector3(-8.5f, 1.5f, 0); public static Vector3 bank_right_LOC = new Vector3(8.5f, 1.5f, 0); public static Vector3 boatLeft_Pos_1 = new Vector3(-3.7f, 1.5f, 0); public static Vector3 boatLeft_Pos_2 = new Vector3(-2.3f, 1.5f, 0); public static Vector3 boatRight_Pos_1 = new Vector3(2.3f, 1.5f, 0); public static Vector3 boatRight_Pos_2 = new Vector3(3.7f, 1.5f, 0); } public interface IUserActions { void boatMove(); void priestsGetOn(); void priestsGetOff(); void devilsGetOn(); void devilsGetOff(); } public interface IGameJudge { void modifyBoatPriestsNum(bool isAdd); void modifyBoatDevilsNum(bool isAdd); void modifyBankPriestsNum(bool isLeftBank, bool isAdd); void modifyBankDevilsNum(bool isLeftBank, bool isAdd); void judgeTheGame(bool isBoatLeft); } public class mainSceneController : System.Object, IUserActions, IGameJudge { private static mainSceneController instance; private GenGameObjects myGenGameObjects; private int BoatPriestsNum, BoatDevilsNum, BankLeftPriestsNum, BankRightPriestsNum, BankLeftDevilsNum, BankRightDevilsNum; //人員數量,用于判斷游戲勝負 public static mainSceneController getInstance() { if (instance == null) instance = new mainSceneController(); return instance; } internal void setGenGameObjects(GenGameObjects _myGenGameObjects) { if (myGenGameObjects == null) { myGenGameObjects = _myGenGameObjects; BoatPriestsNum = BoatDevilsNum = BankLeftPriestsNum = BankLeftDevilsNum = 0; BankRightPriestsNum = BankRightDevilsNum = 3; } } /** * 實現IUserActions接口 **/ public void boatMove() { myGenGameObjects.boatMove(); } public void devilsGetOff() { myGenGameObjects.devilsGetOff(); } public void devilsGetOn() { myGenGameObjects.devilsGetOn(); } public void priestsGetOff() { myGenGameObjects.priestsGetOff(); } public void priestsGetOn() { myGenGameObjects.priestsGetOn(); } /** * 實現IGameJudge接口 **/ public void modifyBoatPriestsNum(bool isAdd) { if (isAdd) BoatPriestsNum++; else BoatPriestsNum--; } public void modifyBoatDevilsNum(bool isAdd) { if (isAdd) BoatDevilsNum++; else BoatDevilsNum--; } public void modifyBankDevilsNum(bool isLeftBank, bool isAdd) { if (isLeftBank) { if (isAdd) BankLeftDevilsNum++; else BankLeftDevilsNum--; } else { if (isAdd) BankRightDevilsNum++; else BankRightDevilsNum--; } } public void modifyBankPriestsNum(bool isLeftBank, bool isAdd) { if (isLeftBank) { if (isAdd) BankLeftPriestsNum++; else BankLeftPriestsNum--; } else { if (isAdd) BankRightPriestsNum++; else BankRightPriestsNum--; } } public void judgeTheGame(bool isBoatLeft) { if (isBoatLeft) { if ((BankLeftPriestsNum + BoatPriestsNum > 0 && BankLeftDevilsNum + BoatDevilsNum > BankLeftPriestsNum + BoatPriestsNum) || (BankRightDevilsNum > BankRightPriestsNum && BankRightPriestsNum > 0)) { showGameText("Failed !"); } if (BankLeftDevilsNum + BoatDevilsNum == 3 && BankLeftPriestsNum + BoatPriestsNum == 3) { showGameText("Victory !"); } } else { if ((BankRightPriestsNum + BoatPriestsNum > 0 && BankRightDevilsNum + BoatDevilsNum > BankRightPriestsNum + BoatPriestsNum) || (BankLeftDevilsNum > BankLeftPriestsNum && BankLeftPriestsNum > 0)) { showGameText("Failed !"); } } } void showGameText(string textContent) { GameObject Canvas = Camera.Instantiate(Resources.Load("Prefab/Canvas")) as GameObject; GameObject GameText = Camera.Instantiate(Resources.Load("Prefab/GameText"), Canvas.transform) as GameObject; GameText.transform.position = Canvas.transform.position; GameText.GetComponent<Text>().text = textContent; } }}2、GenGameObjects.cs(掛載到主攝像機)using UnityEngine;using System.Collections;using Com.MyGame;using System.Collections.Generic;public class GenGameObjects : MonoBehaviour { public List<GameObject> Priests, Devils; public GameObject boat, bankLeft, bankRight; private BoatBehaviour myBoatBehaviour; void Start () { Priests = new List<GameObject>(); for (int i = 0; i < 3; i++) { GameObject priests = GameObject.CreatePrimitive(PrimitiveType.Capsule); priests.name = "Priest " + (i + 1); priests.tag = "Priest"; priests.GetComponent<MeshRenderer>().material.color = Color.white; priests.AddComponent<PersonStatus>(); Priests.Add(priests); } Priests[0].transform.position = LOCATION_SET.priests_1_LOC; Priests[1].transform.position = LOCATION_SET.priests_2_LOC; Priests[2].transform.position = LOCATION_SET.priests_3_LOC; Devils = new List<GameObject>(); for (int i = 0; i < 3; i++) { GameObject devils = GameObject.CreatePrimitive(PrimitiveType.Capsule); devils.name = "Devil " + (i + 1); devils.tag = "Devil"; devils.GetComponent<MeshRenderer>().material.color = Color.red; devils.AddComponent<PersonStatus>(); Devils.Add(devils); } Devils[0].transform.position = LOCATION_SET.devils_1_LOC; Devils[1].transform.position = LOCATION_SET.devils_2_LOC; Devils[2].transform.position = LOCATION_SET.devils_3_LOC; boat = GameObject.CreatePrimitive(PrimitiveType.Cube); boat.name = "Boat"; boat.AddComponent<BoatBehaviour>(); myBoatBehaviour = boat.GetComponent<BoatBehaviour>(); boat.GetComponent<MeshRenderer>().material.color = Color.blue; boat.transform.localScale = new Vector3(3, 1, 1); boat.transform.position = LOCATION_SET.boat_right_LOC; bankLeft = GameObject.CreatePrimitive(PrimitiveType.Cylinder); bankLeft.name = "BankLeft"; bankLeft.transform.Rotate(new Vector3(0, 0, 90)); bankLeft.transform.localScale = new Vector3(1, 4, 1); bankLeft.transform.position = LOCATION_SET.bank_left_LOC; bankRight = GameObject.CreatePrimitive(PrimitiveType.Cylinder); bankRight.name = "BankRight"; bankRight.transform.Rotate(new Vector3(0, 0, 90)); bankRight.transform.localScale = new Vector3(1, 4, 1); bankRight.transform.position = LOCATION_SET.bank_right_LOC; mainSceneController.getInstance().setGenGameObjects(this); } void Update () { } public void boatMove() { myBoatBehaviour.setBoatMove(); } //牧師上船 public void priestsGetOn() { if (myBoatBehaviour.isMoving) return; if (!myBoatBehaviour.isBoatAtLeftSide()) { //船在右側 for (int i = 0; i < Priests.Count; i++) { if (Priests[i].GetComponent<PersonStatus>().onBankRight) { //右側岸上有牧師 detectEmptySeat(true, i, DIRECTION.Right); break; } } } else { //船在左側 for (int i = 0; i < Priests.Count; i++) { if (Priests[i].GetComponent<PersonStatus>().onBankLeft) { //左側岸上有牧師 detectEmptySeat(true, i, DIRECTION.Left); break; } } } } //惡魔上船 public void devilsGetOn() { if (myBoatBehaviour.isMoving) return; if (!myBoatBehaviour.isBoatAtLeftSide()) { //船在右側 for (int i = 0; i < Devils.Count; i++) { if (Devils[i].GetComponent<PersonStatus>().onBankRight) { //右側岸上有惡魔 detectEmptySeat(false, i, DIRECTION.Right); break; } } } else { //船在左側 for (int i = 0; i < Devils.Count; i++) { if (Devils[i].GetComponent<PersonStatus>().onBankLeft) { //左側岸上有惡魔 detectEmptySeat(false, i, DIRECTION.Left); break; } } } } //當岸上有牧師/惡魔的時候,檢測船上是否有空位 void detectEmptySeat(bool isPriests, int index, bool boatDir) { if (myBoatBehaviour.isLeftSeatEmpty()) { //船上左位置沒人 seatThePersonAndModifyBoat(isPriests, index, boatDir, DIRECTION.Left); } else if (myBoatBehaviour.isRightSeatEmpty()) { //船上左位置有人,右位置沒人 seatThePersonAndModifyBoat(isPriests, index, boatDir, DIRECTION.Right); } } //牧師/惡魔上船,并調整船的屬性 void seatThePersonAndModifyBoat(bool isPriests, int index, bool boatDir, bool seatDir) { if (isPriests) { Priests[index].GetComponent<PersonStatus>().personSeatOnBoat(boatDir, seatDir); Priests[index].transform.parent = boat.transform; } else { Devils[index].GetComponent<PersonStatus>().personSeatOnBoat(boatDir, seatDir); Devils[index].transform.parent = boat.transform; } myBoatBehaviour.seatOnPos(seatDir); } //牧師下船 public void priestsGetOff() { if (myBoatBehaviour.isMoving) return; if (!myBoatBehaviour.isBoatAtLeftSide()) { //船在右側 for (int i = Priests.Count - 1; i >= 0; i--) { if (detectIfPeopleOnBoat(true, i, DIRECTION.Right)) break; } } else { //船在左側 for (int i = Priests.Count - 1; i >= 0; i--) { if (detectIfPeopleOnBoat(true, i, DIRECTION.Left)) break; } } } //惡魔下船 public void devilsGetOff() { if (myBoatBehaviour.isMoving) return; if (!myBoatBehaviour.isBoatAtLeftSide()) { //船在右側 for (int i = Devils.Count - 1; i >= 0; i--) { if (detectIfPeopleOnBoat(false, i, DIRECTION.Right)) break; } } else { //船在左側 for (int i = Devils.Count - 1; i >= 0; i--) { if (detectIfPeopleOnBoat(false, i, DIRECTION.Left)) break; } } } //檢測是否有牧師/惡魔在船上 bool detectIfPeopleOnBoat(bool isPriests, int i, bool boatDir) { if (isPriests) { if (Priests[i].GetComponent<PersonStatus>().onBoatLeft || Priests[i].GetComponent<PersonStatus>().onBoatRight) { //在船上 myBoatBehaviour.jumpOutOfPos(Priests[i].GetComponent<PersonStatus>().onBoatLeft); Priests[i].GetComponent<PersonStatus>().landTheBank(boatDir); Priests[i].transform.parent = boat.transform.parent; return true; } return false; } else { if (Devils[i].GetComponent<PersonStatus>().onBoatLeft || Devils[i].GetComponent<PersonStatus>().onBoatRight) { //在船上 myBoatBehaviour.jumpOutOfPos(Devils[i].GetComponent<PersonStatus>().onBoatLeft); Devils[i].GetComponent<PersonStatus>().landTheBank(boatDir); Devils[i].transform.parent = boat.transform.parent; return true; } return false; } }}3、PersonStatus.cs(會動態掛載)using UnityEngine;using System.Collections;using Com.MyGame;public class PersonStatus : MonoBehaviour { private Vector3 originalPos; public bool onBoatLeft, onBoatRight; public bool onBankLeft, onBankRight; private IGameJudge gameJudge; void Start () { originalPos = this.transform.position; onBoatLeft = false; onBoatRight = false; onBankLeft = false; onBankRight = true; gameJudge = mainSceneController.getInstance() as IGameJudge; } void Update () { } //人上船(下岸) public void personSeatOnBoat(bool boatAtLeft, bool seatAtLeft) { if (seatAtLeft) { if (boatAtLeft) this.transform.position = LOCATION_SET.boatLeft_Pos_1; else this.transform.position = LOCATION_SET.boatRight_Pos_1; onBoatLeft = true; } else { if (boatAtLeft) this.transform.position = LOCATION_SET.boatLeft_Pos_2; else this.transform.position = LOCATION_SET.boatRight_Pos_2; onBoatRight = true; } onBankLeft = false; onBankRight = false; if (this.tag.Equals("Priest")) { gameJudge.modifyBoatPriestsNum(MODIFICATION.Add); gameJudge.modifyBankPriestsNum(boatAtLeft, MODIFICATION.Sub); } else { gameJudge.modifyBoatDevilsNum(MODIFICATION.Add); gameJudge.modifyBankDevilsNum(boatAtLeft, MODIFICATION.Sub); } } //人上岸(下船) public void landTheBank(bool boatAtLeft) { if (boatAtLeft) { this.transform.position = new Vector3(-originalPos.x, originalPos.y, originalPos.z); onBankLeft = true; } else { this.transform.position = originalPos; onBankRight = true; } onBoatLeft = false; onBoatRight = false; if (this.tag.Equals("Priest")) { gameJudge.modifyBoatPriestsNum(MODIFICATION.Sub); gameJudge.modifyBankPriestsNum(boatAtLeft, MODIFICATION.Add); } else { gameJudge.modifyBoatDevilsNum(MODIFICATION.Sub); gameJudge.modifyBankDevilsNum(boatAtLeft, MODIFICATION.Add); } }}4、BoatBehaviour.cs(會動態掛載)using UnityEngine;using System.Collections;using Com.MyGame;public class BoatBehaviour : MonoBehaviour { private Vector3 moveDir = new Vector3(-0.1f, 0, 0); public bool isMoving; public bool atLeftSide; public bool leftPosEmpty, rightPosEmpty; private IGameJudge gameJudge; void Start () { isMoving = false; atLeftSide = DIRECTION.Right; leftPosEmpty = true; rightPosEmpty = true; gameJudge = mainSceneController.getInstance() as IGameJudge; } void Update () { moveTheBoat(); } private void moveTheBoat() { if (isMoving) { if (!isMovingToEdge()) { this.transform.Translate(moveDir); } } } //檢測靠岸 private bool isMovingToEdge() { if (moveDir.x < 0 && this.transform.position.x <= LOCATION_SET.boat_left_LOC.x) { //向左,已到 gameJudge.judgeTheGame(DIRECTION.Left); isMoving = false; atLeftSide = DIRECTION.Left; moveDir = new Vector3(-moveDir.x, 0, 0); return true; } else if (moveDir.x > 0 && this.transform.position.x >= LOCATION_SET.boat_right_LOC.x) { //向右,已到 gameJudge.judgeTheGame(DIRECTION.Right); isMoving = false; atLeftSide = DIRECTION.Right; moveDir = new Vector3(-moveDir.x, 0, 0); return true; } else { //還沒靠岸 return false; } } /** * 提供給GenGameObjects腳本調用 * 點擊Go按鈕觸發船移動:需要船靜止 + 船上有人(至少一個位置不空) **/ public void setBoatMove() { if (!isMoving && (!leftPosEmpty || !rightPosEmpty)) { isMoving = true; } } public bool isBoatAtLeftSide() { return atLeftSide; } public bool isLeftSeatEmpty() { return leftPosEmpty; } public bool isRightSeatEmpty() { return rightPosEmpty; } public void seatOnPos(bool isLeft) { if (isLeft) leftPosEmpty = false; else rightPosEmpty = false; } public void jumpOutOfPos(bool isLeft) { if (isLeft) leftPosEmpty = true; else rightPosEmpty = true; }}5、UserInterface.cs (掛載到Empty對象上)using UnityEngine;using System.Collections;using Com.MyGame;public class UserInterface : MonoBehaviour { IUserActions myActions; float btnWidth = (float)Screen.width / 6.0f; float btnHeight = (float)Screen.height / 6.0f; void Start () { myActions = mainSceneController.getInstance() as IUserActions; } void Update () { } void OnGUI() { if (GUI.Button(new Rect(5, 250, btnWidth, btnHeight), "Priests GetOn")) { myActions.priestsGetOn(); } if (GUI.Button(new Rect(155, 250, btnWidth, btnHeight), "Priests GetOff")) { myActions.priestsGetOff(); } if (GUI.Button(new Rect(305, 250, btnWidth, btnHeight), "Go!")) { myActions.boatMove(); } if (GUI.Button(new Rect(455, 250, btnWidth, btnHeight), "Devils GetOn")) { myActions.devilsGetOn(); } if (GUI.Button(new Rect(605, 250, btnWidth, btnHeight), "Devils GetOff")) { myActions.devilsGetOff(); } }}操作:1、先弄好上面5份代碼
2、在下面的點擊Create -> Create Empty
3、把GenGameObjects.cs掛載到主攝像機,UserInterface.cs 掛載到Empty對象上。掛載方法:點擊Add Component,輸入腳本名字,點擊腳本,變成右邊的樣子即可
![]()
4、按下圖構造好項目結構(所有腳本均放在Scripts文件夾里)。
5、藍色那些是Prefab,構造方法:學第2點,Create -> UI -> Text。然后把Canvas拖到下面的Prefab文件夾。
GameText要設置一下,再拖到Prefab文件夾:
這樣應該是完成了。如果出問題再按照提示改一下唄~
啊好累……_(:зゝ∠)_
新聞熱點
疑難解答