- 解释 游戏对象(GameObjects) 和 资源(Assets)的区别与联系
游戏对象(GameObject),游戏中出现的物体都是游戏对象,主角,NPC,场景中可互动的物体都是经典的游戏对象
资源(Assets),是游戏中的贴图,音效等重复利用的素材,可用来配置游戏对象
区别与联系
资源可以实体化成游戏中的任何一个游戏对象,或作为游戏对象的某种属性存在
- 编写一个代码,使用 debug 语句来验证 MonoBehaviour 基本行为或事件触发的条件
- 基本行为包括 Awake() Start() Update() FixedUpdate() LateUpdate()
- 常用事件包括 OnGUI() OnDisable() OnEnable()
public class Test : MonoBehaviour {
void Awake() {
Debug.Log ("onAwake");
}
void Start () {
Debug.Log ("onStart");
}
void Update () {
Debug.Log ("onUpdate");
}
void FixedUpdate() {
Debug.Log ("onFixedUpdate");
}
void LateUpdate() {
Debug.Log ("onLateUpdate");
}
void OnGUI() {
Debug.Log ("onGUI");
}
void OnDisable() {
Debug.Log ("onDisable");
}
void OnEnable() {
Debug.Log ("onEnable");
}
}
可以看出
awake:当一个脚本实例被载入时被调用
start:在所有update函数之前被调用一次(仅被调用一次)
update与fixedupdate:当行为启用时,其update在每一帧被调用
OnGUI:渲染和处理GUI事件时调用
OnEnable:当对象变为可用时被调用
OnDisable:当对象变为不可用时被调用
- 查找脚本手册,了解 GameObject,Transform,Component 对象
- 分别翻译官方对三个对象的描述(Description)
游戏对象是Unity场景中的基本对象。场景包含游戏的环境和菜单。把每个独特的场景文件看作一个独特的层次。在每个场景中,你放置你的环境、障碍和装饰,本质上就是设计和构建你的游戏。
变换组件决定场景中每个对象的位置、旋转和比例。
组件是所有附加到游戏物体的基类。
-
- 描述下图中 table 对象(实体)的属性、table 的 Transform 的属性、 table 的部件
- 本题目要求是把可视化图形编程界面与 Unity API 对应起来,当你在 Inspector 面板上每一个内容,应该知道对应 API。
- 例如:table 的对象是 GameObject,第一个选择框是 activeSelf 属性。
- 描述下图中 table 对象(实体)的属性、table 的 Transform 的属性、 table 的部件
第一个选择框是activeSelf属性,第二个是对象名称,第三个是static属性,下一行是标签Tag和Layer,第三行是预设。
table对象的Transform中:
Position是空间位置
Rotation是旋转角度
table的部件:Transform,Mesh Filter(网格过滤器),Box Collider(盒状碰撞器),Mesh Renderer(网格渲染器)。
Scale是比例大小
table的部件有chair1,chair2,chair3,chair4
- 用 UML 图描述 三者的关系(请使用 UMLet 14.1.1 stand-alone版本出图)
- 资源预设(Prefabs)与 对象克隆 (clone)
- 预设(Prefabs)有什么好处?
预设可以提前将设计游戏中所需要的游戏对象进行设计打包,成为一个模板。在我们想要生成一个游戏对象的时候,可以直接从资源中加载,大大减少了重复时间
- 预设与对象克隆 (clone or copy or Instantiate of Unity Object) 关系?
克隆是将已经存在的游戏对象,或者是资源当中的预设进行复制。克隆可以对已经生成好的游戏对象进行克隆,也可以对预设的对象进行克隆。
- 制作 table 预制,写一段代码将 table 预制资源实例化成游戏对象
void Start () {
GameObject table_1 = (GameObject)Instantiate(table);
}
下面是一个简易计算器的实现
using UnityEngine;
using UnityEngine.UI;
public class Calculator : MonoBehaviour
{
public Text resultText; // 引用ResultText UI元素
private string currentInput = ""; // 当前用户输入的字符串
private double currentResult = 0; // 当前计算结果
private string currentOperator = ""; // 当前操作符
// 当数字按钮被点击时调用
public void OnNumberButtonClicked(string number)
{
currentInput += number;
resultText.text = currentInput;
}
// 当操作符按钮被点击时调用
public void OnOperatorButtonClicked(string operatorSymbol)
{
if (!string.IsNullOrEmpty(currentInput))
{
currentResult = double.Parse(currentInput);
currentInput = "";
currentOperator = operatorSymbol;
}
}
// 当等号按钮被点击时调用
public void OnEqualsButtonClicked()
{
if (!string.IsNullOrEmpty(currentInput))
{
double secondOperand = double.Parse(currentInput);
switch (currentOperator)
{
case "+":
currentResult += secondOperand;
break;
case "-":
currentResult -= secondOperand;
break;
case "*":
currentResult *= secondOperand;
break;
case "/":
if (secondOperand != 0)
{
currentResult /= secondOperand;
}
else
{
resultText.text = "Error";
return;
}
break;
}
resultText.text = currentResult.ToString();
currentInput = "";
}
}
// 当清除按钮被点击时调用
public void OnClearButtonClicked()
{
currentInput = "";
currentResult = 0;
currentOperator = "";
resultText.text = "";
}
}