简答题
-
解释 游戏对象(GameObjects) 和 资源(Assets)的区别与联系
- 区别
* 游戏对象
游戏中的每一个对象都是一个游戏对象。他们本身不会做任何事情,我们赋予他们各自的属性之后,就成为了我们在游戏中看到的角色或环境等。可以将游戏对象比喻成一个空容器,再向其加入其中的组件和赋予其的属性后,它变得与其他游戏对象不同
* 资源
资源有很多,比如对象、材质、场景、声音、预设、贴图、脚本和动作资源。这些资源可以在项目打开时,被导入到游戏里。可以被游戏对象使用,也可以被实例化为游戏对象。- 联系
资源被导入到游戏中,与游戏对象相互配合以此完成相应功能。
- 联系
- 区别
-
编写一个代码,使用 debug 语句来验证 MonoBehaviour 基本行为或事件触发的条件
- 基本行为包括Awake() Start() Update() FixedUpdate() LateUpdate()
- 常用事件包括 OnGUI() OnDisable() OnEnable()
public class behaviorTest : MonoBehaviour{ void Awake(){ Debug.Log(" Awake!"); } void Start(){ Debug.Log("Start!"); } void Update(){ Debug.Log("Update!"); } void FixedUpdate(){ Debug.Log("FixedUpdate!"); } void LateUpdate(){ Debug.Log("LateUpdate!"); } void OnGUI(){ Debug.Log("OnGUI!"; } void OnDisable(){ Debug.Log("OnDisable!"); } }
-
查找脚本手册,了解 GameObject,Transform,Component 对象
-
分别翻译官方对三个对象的描述(Description)
-
GameObject
游戏对象是代表人物、道具和布景的基本对象。它们本身没有实现太多功能,但它们充当组件的容器,而容器实现真正的功能。 -
Transform
变换是用来存储一个对象的位置、旋转、尺度和父对象的状态的,是非常重要的。 -
Component
组件是所有附属于游戏对象的基类。
-
-
描述下图中 table 对象(实体)的属性、table 的 Transform 的属性、 table 的部件
-
table 对象的属性
Layer: Default
Tag: Untagged -
table的Transform属性
Position:(0,0,0)
Rotation:(0,0,0)
Scale:(1,1,1) -
table的部件
Mesh Renderer
Transform
Box Collider
-
-
用 UML 图描述 三者的关系
-
整理相关学习资料,编写简单代码验证以下技术的实现
- 查找对象
public static GameObject Find(string name); public static GameObject[] FindGameObjectsWithTag(string tagName); public static GameObject FindWithTag(string tagName); public static GameObject[] FindObjectOfType(string typeName); public static GameObject FindObjectOfType(striing typeName);
- 添加子对象
public static GameObject CreatePrimitive(PrimitiveType type)
- 遍历对象树
foreach(Transform child in transform){}
- 清除所有子对象
foreach(Transform child in transform){ Destroy(child.gameObject); }
-
预设与克隆
-
预设(Prefabs)有什么好处?与对象克隆 (clone or copy or Instantiate of Unity Object) 关系?
向场景添加一个预设会创建一个实例,通过对预设的更改可以改变实例的属性。
克隆是复制一个对象的实例,对一个对象作出更改不会影响到另一个对象。 -
制作 table 预制,写一段代码将 table 预制资源实例化成游戏对象
using System.Collections;
using System.Collections.Generic;
uisng UnityEngine;
public class createNewScript: MonoBeheviour{
public transform res;
//use this for initialization
void start(){
//load resource
GameObject newobj=Instantiate<Transform>(res,this.transform).gameObject;
newobj.transform.position=new Vector3(0,Random.Range(-2,2),0);
}
}
- 尝试解释组合模式(Composite Pattern / 一种设计模式)并使用 BroadcastMessage() 方法向子对象发送消息
将对象组合成树形结构以表示“部分-整体”的层次结构。Composite模式使得用户对单个对象和组合对象的使用具有一致性。- 父类
public class ParentScript: MonoBehaviour{ void Start(){ Debug.Log("start!"); this.BroadcastMessage("Test"); } }
- 子类
public calss ChildScript: MonoBehaviour{ void Test(){ Debug.Log("Child"); } }