3D游戏编程:离散仿真引擎基础

1.简答题

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

The GameObject is the most important concept in the Unity Editor.
Every object in your game is a GameObject, from characters and collectible items to lights, cameras and special effects. However, a GameObject can’t do anything on its own; you need to give it properties before it can become a character, an environment, or a special effect.

即游戏中的任何对象(从角色到灯光等)都是游戏对象,但游戏对象不能单独存在,需要添加一些组件来发挥作用.

An Asset is a representation of any item you can use in your game or Project. An Asset may come from a file created outside of Unity, such as a 3D Model, an audio file, an image, or any of the other file types that Unity supports. There are also some Asset types that you can create in Unity, such as a ProBuilder Mesh, an Animator Controller, an Audio Mixer, or a Render Texture

资源可以看作是帮助游戏对象构建属性的物品.
2.下载几个游戏案例,分别总结资源、对象组织的结构(指资源的目录组织结构与游戏对象树的层次结构)
关于资源的目录组织结构,我找到一篇文章介绍的比较完善:
Unity项目如何架构(一)目录组织
目录组织依据游戏需要而定.以下为炸弹人小游戏游戏目录,可见目录树:
炸弹人游戏目录

关于游戏对象树的层次,由上gameobject的定义可得,游戏对象无所不包,组织方式同样有多种,因为找到的几个游戏版本都不兼容,重新bulid之后虽然可以运行,但无法找到游戏对象树,因此在网上重新找了一种,如下:

GameObject
| - Player
| - Enemy
| - Surroundings
| - Bulidings
| - Trees

| - Camera
| - Music
| - Audio
| - Environment

3.编写一个代码,使用 debug 语句来验证 MonoBehaviour 基本行为或事件触发的条件
基本行为包括 Awake() Start() Update() FixedUpdate() LateUpdate()
常用事件包括 OnGUI() OnDisable() OnEnable()

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

public class NewBehaviourScript : MonoBehaviour {

    //base
	// Use this for initialization
	void Start () {
        Debug.Log("Start");
	}
	// Update is called once per frame
	void Update () {
        Debug.Log("Update");
	}
    //Awake is called when the script instance is being loaded.only once
    void Awake()
    {
        Debug.Log("Awake");
    }
    // it is called every fixed frame-rate frame.
    //The FixedUpdate frequency is more or less than Update.(fixed,not equal to fps)
    void FixedUpdate()
    {
        Debug.Log("FixedUpdate");
    }
    //LateUpdate is called every frame, if the Behaviour is enabled.
    //LateUpdate is called after all Update functions have been called.
    //it tracks objects that might have moved inside Update
    void LateUpdate()
    {
        Debug.Log("LateUpdate");
    }
    //common
    //OnGUI is called for rendering and handling GUI events.
    void OnGUI()
    {
        Debug.Log("OnGUI");
    }
    //This function is called when the behaviour becomes disabled.
    void OnDisable()
    {
        Debug.Log("OnDisable");
    }
    //This function is called when the object becomes enabled and active.
    void OnEnable()
    {
        Debug.Log("OnEnable");
    }
}

结果如图所示
4.查找脚本手册,了解 GameObject,Transform,Component 对象
①分别翻译官方对三个对象的描述(Description)
②描述下图中 table 对象(实体)的属性、table 的 Transform 的属性、 table 的部件

  • 本题目要求是把可视化图形编程界面与 Unity API 对应起来,当你在 Inspector 面板上每一个内容,应该知道对应 API。
  • 例如:table 的对象是 GameObject,第一个选择框是 activeSelf 属性。)

③用 UML 图描述 三者的关系(请使用 UMLet 14.1.1 stand-alone版本出图)

GameObject: Base class for all entities in Unity Scenes.

GameObject是Unity场景中所有实体的基类

Transform: Position, rotation and scale of an object.
Every object in a Scene has a Transform. It’s used to store and manipulate the position, rotation and scale of the object. Every Transform can have a parent, which allows you to apply position, rotation and scale hierarchically.

Transform说明一个对象的位置、旋转和比例。场景中的每个对象都有一个transform,用来存储和操纵对象的位置、旋转和比例。每个transform都能有一个父类,允许继承使用位置、旋转和比例信息。

Component: Base class for everything attached to GameObjects.
Note that your code will never directly create a Component. Instead, you write script code, and attach the script to a GameObject.

component是附属在游戏对象上的任何事物的基类。注意不能直接创造一个component,必须写出脚本,并将该脚本附属在某个游戏对象上。

在这里插入图片描述
如图所示,cube的对象是 GameObject,第一个选择框是 activeSelf 属性。之后的static选项决定是否是静态对象,Tag设置标签后,可以通过GameObject.FindWithTag()来寻找对象。Layer也用来为游戏对象分类。
下面还列出了Transform、Box Collider、Mesh Renderer和Materials属性。

Transform
Position位置坐标
Rotation旋转角度(绕哪个轴)
Scale放缩比例
Box Collider
Is Trigger如果启用,此碰撞器用于触发事件,并被物理引擎忽略。
Material物理材料,确定与他们互动的方式
Center碰撞器在物体空间种的位置
Size碰撞器尺寸的大小
Materials
SizeSpecify the number of Materials in the Mesh Renderer. If you decrease the size of the list of Materials, Unity deletes the elements at the end of the list.
ElementA list of the Materials in the Mesh Renderer, in numeric order. The first element is always named Element 0.

Mesh Renderer属性较多,在此不一一列出,可以参考官方文档

在这里插入图片描述
5.资源预设(Prefabs)与 对象克隆 (clone)

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

Unity’s Prefab system allows you to create, configure, and store a GameObject complete with all its components, property values, and child GameObjects as a reusable Asset. The Prefab Asset acts as a template from which you can create new Prefab instances in the Scene.
Any edits that you make to a Prefab Asset are automatically reflected in the instances of that Prefab, allowing you to easily make broad changes across your whole Project without having to repeatedly make the same edit to every copy of the Asset.

预制可以看作是将某个游戏对象及其属性制成可复用的一个资源,预制与单纯的复制不同点在于,多个预制副本可以同步更新。

克隆无法在预制对象改动时及时更新。

When you clone a GameObject or Component, all child objects and components will also be cloned with their properties set like those of the original object.

使用如下代码进行克隆

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

public class LoadPre : MonoBehaviour {

    //public Transform prefab;
    public GameObject prefab;
    public GameObject newTable;
    public Vector3 pos=new Vector3(3,-3,0);
	// Use this for initialization
	void Start () {
         newTable = Instantiate(prefab,pos,Quaternion.identity);
	}
	// Update is called once per frame
	void Update () {
		
	}
}

在这里插入图片描述

2.编程实践:井字棋

代码链接如下:
井字棋

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值