#感觉这门课作业不提前写真搞不定啊_(:зゝ∠)_
#文末有全部代码以及操作方法
先把游戏效果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文件夹:
这样应该是完成了。如果出问题再按照提示改一下呗~
啊好累……_(:зゝ∠)_
新闻热点
疑难解答