- 课程链接:https://pmlpml.github.io/unity3d-learning/
- 游戏内容: 井字棋 或 贷款计算器 或 简单计算器 等等
- 技术限制: 仅允许使用 IMGUI 构建 UI
- 8012年了,还有人在扩写编辑器以外的地方用IMGUI
演示:
类图:
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Restart : MonoBehaviour {
Manager gameManager;
void Start()
{
gameManager = GameObject.Find("Manager").GetComponent<Manager>();
}
void OnGUI()
{
if (GUI.Button(new Rect(280,450,80,20), "Restart"))
{
gameManager.OnReStart();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Manager : MonoBehaviour {
public GameObject borad;
public GameObject message;
private GameObject _board;
private GameObject _message;
void Start() {
_board = Instantiate(borad);
_message = Instantiate(message);
}
public void OnSelectOnZone(int x, int y, int symbol)
{
if (symbol == 1) _message.GetComponent<Message>().OnChangeBox(x, y, "√");
else _message.GetComponent<Message>().OnChangeBox(x, y, "○");
}
public void OnWin(string name)
{
_message.GetComponent<Message>().OnChangeWinnerMessage(name + " Win this game!!!!");
}
public void OnReStart() {
Destroy(_board);
Destroy(_message);
_board = Instantiate(borad);
_message = Instantiate(message);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Board : MonoBehaviour {
public int[,] borad = new int[4,4];
private int currentPlayer = 1;
Manager gameManager;
Rect middleRect = new Rect(Screen.width/2 - 150, Screen.height/2 - 150, Screen.width/2 + 150, Screen.height/2 + 300);
void Start() {
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
borad[i, j] = 0;
}
}
gameManager = GameObject.Find("Manager").GetComponent<Manager>();
}
void OnGUI()
{
GUI.BeginGroup(middleRect);
#region nine button
if (GUI.Button(new Rect(0,0,100,100),"1"))
{
if (borad[0, 0] == 0)
{
borad[0, 0] = currentPlayer;
gameManager.OnSelectOnZone(0, 0, currentPlayer);
CheckBoard();
}
}
if (GUI.Button(new Rect(100, 0, 100, 100), "2"))
{
if (borad[0, 1] == 0)
{
borad[0, 1] = currentPlayer;
gameManager.OnSelectOnZone(0, 1, currentPlayer);
CheckBoard();
}
}
if (GUI.Button(new Rect(200, 0, 100, 100), "3"))
{
if (borad[0, 2] == 0)
{
borad[0, 2] = currentPlayer;
gameManager.OnSelectOnZone(0, 2, currentPlayer);
CheckBoard();
}
}
if (GUI.Button(new Rect(0, 100, 100, 100), "4"))
{
if (borad[1, 0] == 0)
{
borad[1, 0] = currentPlayer;
gameManager.OnSelectOnZone(1, 0, currentPlayer);
CheckBoard();
}
}
if (GUI.Button(new Rect(100, 100, 100, 100), "5"))
{
if (borad[1, 1] == 0)
{
borad[1, 1] = currentPlayer;
gameManager.OnSelectOnZone(1, 1, currentPlayer);
CheckBoard();
}
}
if (GUI.Button(new Rect(200, 100, 100, 100), "6"))
{
if (borad[1, 2] == 0)
{
borad[1, 2] = currentPlayer;
gameManager.OnSelectOnZone(1, 2, currentPlayer);
CheckBoard();
}
}
if (GUI.Button(new Rect(0, 200, 100, 100), "7"))
{
if (borad[2, 0] == 0)
{
borad[2, 0] = currentPlayer;
gameManager.OnSelectOnZone(2, 0, currentPlayer);
CheckBoard();
}
}
if (GUI.Button(new Rect(100, 200, 100, 100), "8"))
{
if (borad[2, 1] == 0)
{
borad[2, 1] = currentPlayer;
gameManager.OnSelectOnZone(2, 1, currentPlayer);
CheckBoard();
}
}
if (GUI.Button(new Rect(200, 200, 100, 100), "9"))
{
if (borad[2, 2] == 0)
{
borad[2, 2] = currentPlayer;
gameManager.OnSelectOnZone(2, 2, currentPlayer);
CheckBoard();
}
}
#endregion
GUI.EndGroup();
}
void CheckBoard()
{
for (int i = 0; i < 3; i++)
{
borad[i, 3] = borad[i, 0] + borad[i, 1] + borad[i, 2];
if (borad[i, 3] == 3)
{
gameManager.OnWin("√");
}
else if (borad[i, 3] == -3)
{
gameManager.OnWin("○");
}
}
for (int i = 0; i < 3; i++)
{
borad[3, i] = borad[0,i] + borad[1,i] + borad[2,i];
if (borad[3, i] == 3)
{
gameManager.OnWin("√");
//GUI.Label(tittleRect, "√ WIN!!!!");
}
else if (borad[3, i] == -3)
{
gameManager.OnWin("○");
//GUI.Label(tittleRect, "○ Win!!!!!");
}
}
borad[0, 3] = borad[0, 2] + borad[1, 1] + borad[2, 0];
if (borad[0, 3] == 3)
{
gameManager.OnWin("√");
//GUI.Label(tittleRect, "√ WIN!!!!");
}
else if (borad[0, 3] == -3)
{
gameManager.OnWin("○");
//GUI.Label(tittleRect, "○ Win!!!!!");
}
borad[3, 3] = borad[0, 0] + borad[1, 1] + borad[2, 2];
if (borad[3, 3] == 3)
{
gameManager.OnWin("√");
// GUI.Label(tittleRect, "√ WIN!!!!");
}
else if (borad[3, 3] == -3)
{
gameManager.OnWin("○");
//GUI.Label(tittleRect, "○ Win!!!!!");
}
currentPlayer = -currentPlayer;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Message : MonoBehaviour {
string winnerMessage;
string currentPlayer ="√";
private string[,] box = new string[3, 3];
void Start() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
box[i,j] = "empty";
}
}
}
void OnGUI()
{
GUI.Label(new Rect(260, 400, 120, 20), winnerMessage);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
GUI.Label(new Rect(50*j, 50*i, 50, 50), box[i,j]);
}
}
GUI.Label(new Rect(260, 50, 120, 20), "Current Player is :" + currentPlayer);
}
public void OnChangeWinnerMessage(string newMessage)
{
winnerMessage = newMessage;
}
public void OnChangeBox(int i, int j, string symbol)
{
box[i, j] = symbol;
if (symbol == "√") currentPlayer = "○";
else currentPlayer = "√";
}
}
注解:
IMGUI的设计思路是在OnGUI事件函数里不断反复运行有关GUI的代码,所以一般来说,一个类只能负责一层UI功能。
所以我把功能分成了“棋盘”“显示文字消息”“重置”三部分,再增加一个Manager类帮助传递消息
项目包(2017.3.1f1):
https://github.com/keven2148/3D-Game-Programming-Design-Lesson-Work