Unity3d离散仿真引擎基础

返回目录


简答题

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

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

以Space Shooter游戏的文件结构为例

3. 编写一个代码,使用 debug 语句来验证 MonoBehaviour 基本行为或事件触发的条件

1. 基本行为包括 Awake() Start() Update() FixedUpdate() LateUpdate()

2. 常用事件包括 OnGUI() OnDisable() OnEnable()

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

    void Awake(){
        Debug.Log ("Awake");
    }

    // Use this for initialization
    void Start () {
        Debug.Log ("Start");
    }
    
    // Update is called once per frame
    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");
    }
}

4. 查找脚本手册,了解 GameObject,Transform,Component 对象

1. 分别翻译官方对三个对象的描述(Description)

2. 描述下图中table对象(实体)的属性、table的Transform的属性、table的部件

workwork

3. 用UML图描述三者的关系

5. 整理相关学习资料,编写简单代码验证以下技术的实现:

  1. 查找对象
  2. 添加子对象
  3. 遍历对象树
  4. 清除所有子对象

代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

    // Use this for initialization
    void Start(){
        //按名字查找
        var cubeFind = GameObject.Find ("Cube");
        if (null != cubeFind) {
            Debug.Log ("Find the \"Cube\"");
        } else {
            Debug.Log ("Did not find the \"Cube\"");
        }

        //按标签查找
        cubeFind = GameObject.FindGameObjectWithTag ("Player");
        if (null != cubeFind) {
            Debug.Log ("Find the \"Cube\" with tag \"Player\"");
        }
        else{
            Debug.Log("Did not find the \"Cube\" with tag \"Player\"");
        }

        //添加子对象
        GameObject cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube2.name = "Cube2";
        cube2.transform.position = new Vector3 (0, Random.Range(0,5), 5);
        cube2.transform.parent = this.transform;
        Debug.Log ("Create \"Cube2\" as a child of \"" + this.name + "\"");

        //遍历对象树
        GameObject[] objs = GameObject.FindObjectsOfType<GameObject>();
        foreach (GameObject obj in objs) {
            Debug.Log ("Visit " + obj.name);
        }

        //清除所有子对象
        foreach (GameObject obj in objs) {
            if(obj.transform.parent == this.transform){
                Debug.Log ("Destory \""+ obj.name + "\", child of \"Cube\"");
                GameObject.Destroy (obj);
            }
        }
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

控制台输出

6. 资源预设(Prefabs)与 对象克隆 (clone)

1. 预设(Prefabs)有什么好处?

2. 预设与对象克隆 (clone or copy or Instantiate of Unity Object) 关系?

3. 制作table预设,写一段代码将table预设资源实例化成游戏对象(教程)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class createTable : MonoBehaviour {

    public GameObject tablePrefab;

    // Use this for initialization
    void Start () {
        Instantiate (tablePrefab);
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

7. 尝试解释组合模式(Composite Pattern/一种设计模式)。使用BroadcastMessage()方法,向子对象发送消息

编程实践,小游戏

1. 游戏内容:井字棋

2. 技术限制:仅允许使用 IMGUI 构建UI

3. 作业目的:

4. 作业代码及报告地址:https://github.com/guojj33/Unity3DLearning/tree/master/HW2


返回目录