3D Game Programming & Design(二):离散仿真引擎基础
2018-03-22 17:26
495 查看
游戏内容: 井字棋 或 贷款计算器 或 简单计算器 等等
技术限制: 仅允许使用 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();
}
}
}
注解:
IMGUI的设计思路是在OnGUI事件函数里不断反复运行有关GUI的代码,所以一般来说,一个类只能负责一层UI功能。
所以我把功能分成了“棋盘”“显示文字消息”“重置”三部分,再增加一个Manager类帮助传递消息
项目包(2017.3.1f1): https://github.com/keven2148/3D-Game-Programming-Design-Lesson-Work
技术限制: 仅允许使用 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() 4000 { 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
相关文章推荐
- Unity3d离散仿真引擎基础
- Dragon-Studio 之 Flash Alternativa 3D引擎 基础教程-1.C'est quoi ? Alternativa
- Mathematics for 3D Game Programming and Computer Graphics基础学习
- Unity3d-Learning Homework 1 离散仿真引擎基础
- 3D游戏编程与设计 离散仿真引擎基础 作业与练习
- Introduction to 3D Game Programming with DirectX 11学习笔记 5 渲染管线(一)
- Introduction to 3D Game Programming with DirectX 11学习笔记 6 Direct3D中的绘制(二)顶点着色器
- 三维渲染引擎渲染层设计(3D Engine Design for Vritual Globes -翻译未完)
- DirectX10 学习1(摘录Introduction to 3D Game Programming with DirectX 10(中文版))
- Advance 3D Game Programming With DirectX 10.0练习源代码
- 学习《Introduction to 3D Game Programming with DirectX 11》
- 第一.Introduction to 3D Game Programming with DirectX 11介绍一
- 3D Game Programming for Kids 学习笔记
- 3D Game Engine Design, Second Edition --- Go to Get it !
- Mathematics for 3D Game Programming and Computer Graphics - Spherical Linear Interpolation
- Introduction to 3D Game Programming with DirectX 11学习笔记 6 Direct3D中的绘制(三)像素着色器
- <@乌龟:>[原创]设计自己的3D图像引擎(3): WuguiEngine 之基础循环的实现
- Introduction to 3D Game Engine Design Using DirectX 9 and C#
- Game Programming with DirectX -- 02[并非3D的3D]
- 终于译完了《Introduction to 3D Game Programming with DirectX 11》