unity3D简答题

解释 游戏对象(GameObjects) 和 资源(Assets)的区别与联系

GameObjects是一个具体的实例,Assets是包括诸多游戏素材的资源。GameObjects是Assets中的一部分,Assets中不仅仅包括GameObject,还有一些C#文件以及音频文件等。

下载几个游戏案例,分别总结资源、对象组织的结构(指资源的目录组织结构与游戏对象树的层次结构)

资源的目录组织包括:

Material: 包括制作游戏的材质
Prefab: 包括GameObjects的各种预制
Scripts: 包括C#文件,用于指定物体的行为、变化
Animation: 用于设置物体起始,移动,终止时的动作效果

编写一个代码,使用 debug 语句来验证 MonoBehaviour 基本行为或事件触发的条件:
  • 基本行为包括 Awake() Start() Update() FixedUpdate() LateUpdate()
  • 常用事件包括 OnGUI() OnDisable() OnEnable()
 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");
 }

 void OnEnable(){
     Debug.Log("OnEnable");
 }
查找脚本手册,了解 GameObject,Transform,Component 对象
  1. 分别翻译官方对三个对象的描述(Description)
  2. 描述下图中 table 对象(实体)的属性、table 的 Transform 的属性、 table 的部件。本题目要求是把可视化图形编程界面与 Unity API 对应起来,当你在 Inspector 面板上每一个内容,应该知道对应 API。例如:table 的对象是 GameObject,第一个选择框是 activeSelf 属性。这里写图片描述
  3. 用 UML 图描述 三者的关系(请使用 UMLet 14.1.1 stand-alone版本出图)
    1.Gameobject是Unity中用来代表特征、道具、场景的基本物体。它们本身并不拥有太多的实现,但它们是用来实现多种功能组件的容器。
    2.table有Transform、Mesh Filter、Box Collider、Mesh Renderer等部件。其中Transform属性包括了描述物体位置的position属性、角度的Rotation属性、大小的Scale属性。
    3. 这里写图片描述
整理相关学习资料,编写简单代码验证以下技术的实现:
  1. 查找对象
  2. 添加子对象
  3. 遍历对象树
  4. 清除所有子对象
查找对象:
    public static GameObject Find(string name)
    public static GameObject FindWithTag(string tag)
    public static GameObject[] FindGameObjectsWithTag(string tag)
    public static Object FindObjectOfType(Type type)//返回类型为type的活动的第一个游戏对象
    public static Object FindObjectsOfType(Type type)//返回类型为type的所有的活动的游戏对象列表

添加子对象:
    public static GameObect CreatePrimitive(PrimitiveTypetype)

遍历对象树:
    foreach (Transform child in transform)

清除所有子对象:
    foreach (Transform child in transform) {
         Destroy(child.gameObject);
}
资源预设(Prefabs)与 对象克隆 (clone)


  1. 预设(Prefabs)有什么好处?
  2. 预设与对象克隆 (clone or copy or Instantiate of Unity Object) 关系?
  3. 制作 table 预制,写一段代码将 table 预制资源实例化成游戏对象

1.预设可以能让开发者使用先前做好的物体,使得开发更加方便,避免重复不必要的工作。
2. 一旦预设中的属性改变了,由预设产生的实例也会相应的改变;而通过对象克隆出来的对象,不会因为本体的改变而改变。
3.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class create : MonoBehaviour {
    public GameObject prefab;

    // Use this for initialization
    void Start () {
        Instantiate(prefab, new Vector3(0f, 0f, 0f), Quaternion.identity);
    }
}
尝试解释组合模式(Composite Pattern / 一种设计模式)。使用 BroadcastMessage() 方法向子对象发送消息

组合模式:将对象组合成树形结构以表示‘部分-整体’的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。
组合模式的实现关键:组合模式实现的最关键的地方是——简单对象和复合对象必须实现相同的接口。这就是组合模式能够将组合对象和简单对象进行一致处理的原因。

父类对象(Cube)的C#代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Broadcast : MonoBehaviour {

    // Use this for initialization
    void Start () {
        this.BroadcastMessage("sayHello");  
    }

    // Update is called once per frame
    void Update () {

    }
}

子类对象(Sphere)的C#代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloSphere : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    void sayHello()
    {
        Debug.Log("Hello Sphere");
    }
}

上述代码结果输出Hello Sphere。

猜你喜欢

转载自blog.csdn.net/qq_36184359/article/details/79685849
今日推荐