文章目录
1. 简答题
-
解释
游戏对象(GameObjects)
和资源(Assets)
的区别与联系。区别: 游戏对象是游戏程序空间中的事物,可能是Empty(空,最有用的事物)、2D、3D、光线、摄像机等; 游戏资源是构造游戏对象、装饰游戏对象、配置游戏的物体和数据。 联系: 游戏资源可以实例为具体的游戏对象,也可以作为某种属性被游戏对象使用。
-
下载几个游戏案例,分别总结资源、对象组织的结构(指资源的目录组织结构与游戏对象树的层次结构)
游戏案例:
资源的目录组织结构:
游戏对象树的层次结构:
资源是按照文件夹的形式来组织的,根目录一般是各种类型的资源目录以及说明文件,各资源目录下则是该类型的资源文件。游戏对象树由对象之间的父子关系构成的,父亲对象作为根节点,孩子对象作为子节点。 -
编写一个代码,使用
debug
语句来验证MonoBehaviour
基本行为或事件触发的条件- 基本行为包括
Awake()
Start()
Update()
FixedUpdate()
LateUpdate()
- 常用事件包括
OnGUI()
OnDisable()
OnEnable()
using System.Collections; using System.Collections.Generic; using UnityEngine; public class test : MonoBehaviour { void Awake() { Debug.Log("This Awake!"); } // Start is called before the first frame update void Start() { Debug.Log("This Start!"); } // Update is called once per frame void Update() { Debug.Log("This Update!"); } void FixedUpdate() { Debug.Log("This FixedUpdate!"); } void LateUpdate() { Debug.Log("This LateUpdate!"); } void OnGUI() { Debug.Log("This OnGUI!"); } void OnDisable() { Debug.Log("This OnDisable!"); } void OnEnable() { Debug.Log("This OnEnable!"); } }
代码运行结果:
Awake()
:控制脚本被装载时调用;
Start()
:在所有Update方法之前被调用一次,表示开始游戏。
Update()
:每帧调用一次,用于场景更新。
FixedUpdate()
:每固定帧绘制时调用一次,渲染帧执行,渲染效率低下时调用次数减少。
LateUpdate()
:每帧执行完毕时调用,在每个Update方法之后。
OnEnable()
:当对象变为可用或激活状态时触发。
OnDisable()
:当对象变为不可用或非激活状态时触发。
OnGUI()
:绘制GUI时触发。一般在这个函数里绘制GUI菜单。 - 基本行为包括
-
查找脚本手册,了解 GameObject,Transform,Component 对象
- 分别翻译官方对三个对象的描述(Description)
GameObject:GameObjects are the fundamental objects in Unity that represent characters, props and scenery. They do not accomplish much in themselves but they act as containers for Components, which implement the real functionality. 游戏对象是统一的基本对象,代表人物、道具和风景。它们本身并不能完成很多工作,但它们充当组件的容器,这些组件实现了真正的功能。
Transform:The Transform is used to store a GameObject’s position, rotation, scale and parenting state and is thus very important. A GameObject will always have a Transform component attached - it is not possible to remove a Transform or to create a GameObject without one. 转换用于存储游戏对象的位置、旋转、缩放和父元素状态,因此非常重要。一个游戏对象总是有一个附加的转换组件-不可能移除一个转换或者创建一个没有转换的游戏对象。
Component:Components are the nuts & bolts of objects and behaviors in a game. They are the functional pieces of every GameObject. 组件是游戏中对象和行为的螺母和螺栓。它们是每个游戏对象的功能部件。
- 描述下图中 table 对象(实体)的属性、table 的 Transform 的属性、 table 的部件
- 本题目要求是把可视化图形编程界面与 Unity API 对应起来,当你在 Inspector 面板上每一个内容,应该知道对应 API
- 例如:table 的对象是 GameObject,第一个选择框是 activeSelf 属性。
table对象的属性:activeInHierarchy(表示GameObject是否在场景中处于active状态)、 activeSelf(GameObject的本地活动状态)、 isStatic(仅编辑器API,指定游戏对象是否为静态)、 layer(游戏对象所在的图层)、scene(游戏对象所属的场景)、tag(游戏对象的标签)、 transform(游戏对象的转换,最常见的部件) table的Transform的属性:Position、Rotation、Scale table的部件有:Mesh Filter、Box Collider、Mesh Renderer
- 用 UML 图描述 三者的关系(请使用 UMLet 14.1.1 stand-alone版本出图)
- 分别翻译官方对三个对象的描述(Description)
-
资源预设(Prefabs)与 对象克隆 (clone)
- 预设(Prefabs)有什么好处?
预设可以提前将设计游戏中所需要的游戏对象进行设计打包,成为一个模板。在设计的过程中,随时可以直接从资源当中加载,成为一个游戏对象。
- 预设与对象克隆 (clone or copy or Instantiate of Unity Object) 关系?
预设和克隆都是通过复制产生对象。不同的是,预设修改时可以保持所有副本同步,提高使用资源的效率; 克隆对象修改时其副本独立于被克隆的对象,不会发生变化。
- 制作 table 预制,写一段代码将 table 预制资源实例化成游戏对象
using System.Collections; using System.Collections.Generic; using UnityEngine; public class test : MonoBehaviour { public GameObject table; // Start is called before the first frame update void Start() { GameObject instance = (GameObject) Instantiate (table, new Vector3(0, Random.Range(5, 10), 0), transform.rotation); } // Update is called once per frame void Update() { } }
- 预设(Prefabs)有什么好处?
2. 编程实践,小游戏
- 游戏内容: 井字棋 或 贷款计算器 或 简单计算器 等等
- 技术限制: 仅允许使用 IMGUI 构建 UI
- 作业目的:
- 了解 OnGUI() 事件,提升 debug 能力
- 提升阅读 API 文档能力
这里实现的是井字棋游戏。
了解 IMGUI 请参考unity官方手册关于 IMGUI 的内容
井字棋游戏:两个玩家,一个打圈(O),一个打叉(X),轮流在3乘3的格上打自己的符号,最先以横、直、斜连成一线则为胜。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class jingziqi : MonoBehaviour
{
private int played = 0;
private int turn = 1;
private int[,] chess = new int [3,3];
// Use this for initialization
void Start()
{
reset();
}
void reset()
{
played = 0;
turn = 1;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
chess[i,j] = 0;
}
}
}
private void OnGUI()
{
//设置字体大小
GUI.skin.button.fontSize = 20;
GUI.skin.label.fontSize = 30;
if(GUI.Button(new Rect(450, 400, 200, 80), "Reset"))
{
reset();
}
int res = is_win();
if(res == 1)
{
GUI.Label(new Rect(500, 20, 100, 50), "O wins!");
}
else if(res == 2)
{
GUI.Label(new Rect(500, 20, 100, 50), "X wins!");
}
else if(res == 3)
{
GUI.Label(new Rect(470, 20, 200, 50), "No one wins!");
}
for(int i = 0 ; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(chess[i,j] == 1)
{
GUI.Button(new Rect(i * 100 + 400, j * 100 + 80, 100, 100), "O");
}
if(chess[i,j] == 2)
{
GUI.Button(new Rect(i * 100 + 400, j * 100 + 80, 100, 100), "X");
}
if(GUI.Button(new Rect(i * 100 + 400, j * 100 + 80, 100, 100), ""))
{
if(res == 0)
{
if(turn == 1) chess[i,j] = 1;
if(turn == 2) chess[i,j] = 2;
played++;
if(played % 2 == 1)
{
turn = 2;
}
else
{
turn = 1;
}
}
}
}
}
}
int is_win()
{
//游戏结束的情况
//最中间格子所在的对角线或行或列为同一种棋子时(O或X)
int temp = chess[1,1];
if(temp != 0)
{
if(temp == chess[0,0] && temp == chess[2,2])
{
return temp;
}
if(temp == chess[0,2] && temp == chess[2,0])
{
return temp;
}
if(temp == chess[0,1] && temp == chess[2,1])
{
return temp;
}
if(temp == chess[1,0] && temp == chess[1,2])
{
return temp;
}
}
//左上角格子所在的行或列为同一种棋子时(O或X)
temp = chess[0,0];
if(temp != 0)
{
if(temp == chess[0,2] && temp == chess[0,1])
{
return temp ;
}
if(temp == chess[1,0] && temp == chess[2,0])
{
return temp;
}
}
//右下角格子所在的行或列为同一种棋子时(O或X)
temp = chess[2,2];
if(temp != 0)
{
if(temp == chess[2,0] && temp == chess[2,1])
{
return temp;
}
if(temp == chess[0,2] && temp == chess[1,2])
{
return temp;
}
}
//填完所有格子且不属于任何以上任何一种情况为平局
if(played == 9)
{
return 3;
}
else
{
return 0;
}
}
}