文章目录
1. 简答题
(1)解释 游戏对象(GameObjects) 和 资源(Assets)的区别与联系。
区别:游戏对象 (GameObject)是所有其他组件 (Component) 的容器,而资源(Assets)属于组件,两者并不是一个量级的单位,但有时资源也可以成为游戏对象,例如图片等等。
联系:游戏对象通过对资源的组合,合理使用资源,完成相应的功能。
(2)下载几个游戏案例,分别总结资源、对象组织的结构(指资源的目录组织结构与游戏对象树的层次结构)
下载《地下城与勇士》游戏,打开其工作的文件夹,如图:
资源组织的结构:主要包括音频文件(Music文件夹)、图片文件(ImagePacks2)、操作脚本文件等等。
对象组织的结构:主要包括游戏场景、游戏对象分布、游戏内容打包等等。
(3)编写一个代码,使用 debug 语句来验证 MonoBehaviour 基本行为或事件触发的条件
- 基本行为包括 Awake() Start() Update() FixedUpdate() LateUpdate()
- 常用事件包括 OnGUI() OnDisable() OnEnable()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("gameStart");
}
// Update is called once per frame
void Update()
{
Debug.Log("gameUpdate");
}
//脚本实例被载入时Awake被调用。或者脚本构造时调用
void Awake()
{
Debug.Log("gameAwake");
}
//每个游戏循环,由物理引擎调用
void FixedUpdate()
{
Debug.Log("gameFixedUpdate");
}
//所有 Update 调用完后,被游戏循环调用
void LateUpdate()
{
Debug.Log("gameLateUpdate");
}
//渲染和处理GUI事件时调用。
void OnGUI()
{
Debug.Log("After sign being");
}
//当对象变为可用或激活状态时此函数被调用。
void OnEnable()
{
Debug.Log("gameObject can be use");
}
//当对象变为不可用或非激活状态时此函数被调用。
void OnDisable()
{
Debug.Log("gameObject cann't be use");
}
}
调用条件已在注释中写明。
验证结果:
(4)查找脚本手册,了解 GameObject,Transform,Component 对象
- 分别翻译官方对三个对象的描述(Description)
GameObject:Base class for all entities in Unity Scenes.
游戏对象:Unity视图中所有实体的基类。
Transform:Position, rotation and scale of an object.
变换:一个对象的位置、循环以及大小。
Component:Base class for everything attached to GameObjects.
组件:所有附加到游戏对象上的东西的基类。
- 描述下图中 table 对象(实体)的属性、table 的 Transform 的属性、 table 的部件
右边选择框中可以看到,对象实体属性包括命名、状态、Tag、Layer、Prefab;变换属性包括position、Rotation、Scale;组件属性包括Transform、Mesh Filter、Box Collider、Mesh Renderer。
- 用 UML 图描述 三者的关系(请使用 UMLet 14.1.1 stand-alone版本出图)
(5)整理相关学习资料,编写简单代码验证以下技术的实现:
-
查找对象
通过名字查找:GameObject.Find("name");
通过标签查找:GameObject.FindWithTag("tag");
通过标签查找所有符号标签的对象:GameObject.FindGameObjectsWithTag(string tag);
-
添加子对象
GameObect.CreatePrimitive(PrimitiveTypetype)
- 遍历对象树
GameObject[] allThings = GameObject.FindObjectsOfType<GameObject>();
foreach (GameObject thing in allThings) {
//do something
}
- 清除所有子对象
GameObject[] allThings = GameObject.FindObjectsOfType<GameObject>();
foreach (GameObject thing in allThings) {
GameObject.Destroy(thing);
}
(6)资源预设(Prefabs)与 对象克隆 (clone)
- 预设(Prefabs)有什么好处?
通过预设,我们可以为具有相同属性的对象应用同一个模板,从而在一定程度上简化创建的过程。同时,通过改变预设的属性或行为,我们可以同时改变所有应用了该预设的对象,这也方便了对游戏对象的修改,提高了游戏设计的容错性。 - 预设与对象克隆 (clone or copy or Instantiate of Unity Object) 关系?
对象克隆中,原对象与被克隆对象没有实质上的联系,只是属性相同而已,并不会因为原对象的改变而导致被克隆对象随之改变。另外预设的作用主体其实模板,而对象克隆作用的主体却是实实在在的游戏对象实体。抛开这些区别,两者的最终目的都是为了得到属性相同的一堆游戏对象。 - 制作 table 预制,写一段代码将 table 预制资源实例化成游戏对象
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public GameObject table;
// Start is called before the first frame update
void Start()
{
GameObject game=(GameObject)Instantiate(table, transform.position, transform.rotation);
}
// Update is called once per frame
void Update()
{
}
}
2. 编程实践,小游戏
完成简单计算器的制作。
结果如图: