3D作业2--离散仿真引擎基础

简答题【建议做】

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

在unity官网中,查询GameObjects和Assets的意思,得到如下解释:

GameObjects: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.

翻译成中文意思是:游戏中的每一个对象都是一个GameObject(游戏对象),从角色和可收集的物品到灯光、相机和特效。然而,GameObject不能自己做任何事情;在它成为角色、环境或特殊效果之前,您需要给它赋予属性。

Assets: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 TextureA special type of Texture that is created and updated at runtime. To use them, first create a new Render Texture and designate one of your Cameras to render into it. Then you can use the Render Texture in a Material just like a regular Texture.

翻译成中文意思是:资产是您可以在游戏或项目中使用的任何项目的表示。资产可能来自Unity之外创建的文件,例如3D模型、音频文件、图像或Unity支持的任何其他文件类型。你也可以在Unity中创建一些资产类型,比如ProBuilder Mesh, Animator Controller, Audio Mixer,或者运行时创建并更新的Render TextureA特殊类型的纹理。要使用它们,首先创建一个新的渲染纹理,并指定一个相机渲染到它。然后你可以在材质中使用渲染纹理,就像普通纹理一样。

由以上内容我们可以知道GameObjects和Assets的区别和联系:
区别: GameObjects是游戏中的任何一个对象,Assets 则是指项目文件里面的资源。

联系: GameObjects可以看做是Asset的具体的实例化的结果,一个Asset可以包含多个GameObjects。

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

下载游戏(unity 3D提供的赛车游戏):
在这里插入图片描述
查看资源的目录组织:
在这里插入图片描述
查看游戏对象树的层次:
在这里插入图片描述
由上述游戏我们可以看出:
资源的目录组织: 包括动画(Animations)、音频(Audio)、文档(documentation)、材质(Materials)、模型(Model)、预设(Prefabs)、场景(Scenes)、代码(Script)、着色器(Shader)等。
游戏对象树的层次: 主要包括摄像机(Main Camera),检查点(Checkpoint)、开始和结束的位置,场景布局(如时间显示帆布(Timedisplaycanvas))等。

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

基本行为包括 Awake() Start() Update() FixedUpdate() LateUpdate()
常用事件包括 OnGUI() OnDisable() OnEnable()

代码:

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

public class NewBehaviourScript : 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");
    }
    void OnEnable()
    {
        Debug.Log("OnEnable");
    }
}

保存运行得到以下结果:
在这里插入图片描述
再查找相关资料,从而可以得到MonoBehaviour基本行为或事件触发的条件:

Awake() : 在脚本被载入时调用,且整个脚本的生命周期之中只被调用一次。用于初始化。
Start() : 在Update()之前、Awake()之后被调用。
Update() : 每帧都被调用一次。
FixedUpdate() : 每固定帧被调用一次。
LateUpdate() : 在每帧执行完毕时调用。
OnEnable() : 当对象变为可用或激活状态时此函数被调用。
OnDisable() : 当对象变为不可用或非激活状态时被调用。
OnGUI() : 绘制GUI时候触发。用于渲染和处理GUI。

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

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

GameObject:

GameObjects are the fundamental objects in Unity that represent characters, props and scenery. They do not accomplish much in themselves but they act as containers for Components, which implement the real functionality.

翻译:游戏对象是统一体中代表人物、道具和场景的基本对象。它们本身并没有完成多少工作,但是它们充当组件的容器,组件实现真正的功能。

Transform:

The Transform component determines the Position, Rotation, and Scale of each object in the scene. Every GameObject has a Transform.

翻译:Transform(转换)组件决定场景中每个对象的位置、旋转和比例。每个游戏对象(GameObject)都有一个转换。

Component:

Components are the nuts & bolts of objects and behaviors in a game.They are the functional pieces of every GameObject.

翻译:组件是游戏中对象和行为的细节。它是每一个游戏对象的功能。

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

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

图: 上课所用的Table and four chairs。
由此可看出:
table对象(实体)的属性: Tag(标签)、Layer(图层);
table的Transform属性: Position(位置)、Rotation(旋转)、Scale(比例);
table的部件: Mesh Filter、Box Collider、Mesh Renderer

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

在这里插入图片描述

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

预设(Prefabs)有什么好处?

相当于模板,便于创建属性和行为相同的多个对象。且方便统一管理这些对象。

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

相同点: 都能创建出相同的对象。
不同点: 预设会使创建的对象随着预设的改变而一起改变,而克隆出的对象与原对象并没有联系。

制作 table 预制,写一段代码将 table 预制资源实例化成游戏对象

void Start () {  
        GameObject anotherTable = (GameObject)Instantiate(table.gameObject);  
 }

编程实践,小游戏

游戏内容: 井字棋 或 贷款计算器 或 简单计算器 等等
技术限制: 仅允许使用 IMGUI 构建 UI
作业目的:了解 OnGUI() 事件,提升 debug 能力,提升阅读 API 文档能力

选择游戏: 井字棋
参考代码: https://blog.csdn.net/marvelgl/article/details/60639308

代码及分析:

(分析见代码注释部分)
设置变量:

	//设置变量
    int turn = 1;  //用1和-1来表示回合的轮流
    int[,] state = new int[3, 3]; //用于表示棋盘在该处的状态,0为空,1为O,2为X

设置GUI:

//设置GUI
void OnGUI()
{
    //设置颜色
    GUIStyle style = new GUIStyle();
    style.normal.textColor = new Color(30 / 256f, 93f / 256f, 124 / 256f);

    int result = Check();  //用于检查游戏结果

    //开始游戏
    if (GUI.Button(new Rect(Screen.width / 2 - 45, Screen.height / 2 + 130, 100, 50), "      开始游戏", style))
    {
        Reset();
    }
    //O方胜利
    if (result == 1)
    {
        GUI.Label(new Rect(Screen.width / 2 - 20, Screen.height / 2 + 75, 100, 50), "O方胜利!", style);
    }
    //X方胜利
    else if (result == 2)
    {
        GUI.Label(new Rect(Screen.width / 2 - 20, Screen.height / 2 + 75, 100, 50), "X方胜利!", style);
    }
    //平局
    else if (result == 3)
    {
        GUI.Label(new Rect(Screen.width / 2 - 20, Screen.height / 2 + 75, 100, 50), "  平局!", style);
    }

    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            //O
            if (state[i, j] == 1)
            {
                GUI.Button(new Rect(Screen.width / 2 - 75 + 50 * i, Screen.height / 2 - 130 + 50 * j, 50, 50), "O");
            }
            //X  
            if (state[i, j] == 2)
            {
                GUI.Button(new Rect(Screen.width / 2 - 75 + 50 * i, Screen.height / 2 - 130 + 50 * j, 50, 50), "X");
            }
            //空
            if (GUI.Button(new Rect(Screen.width / 2 - 75 + 50 * i, Screen.height / 2 - 130 + 50 * j, 50, 50), ""))
            {
                if (result == 0)
                {
                    if (turn == 1)
                        state[i, j] = 1;
                    else
                        state[i, j] = 2;
                    turn *= -1;
                }
            }
        }
    }
}

重置函数Reset:

//重置函数Reset:
    void Reset()
    {
        turn = 1;
        for (int i = 0; i < 3; i ++)
        {
            for (int j = 0; j < 3; j ++)
            {
                state[i, j] = 0;
            }
        }
    }

检查结果函数:

//检查结果函数
    int Check()
    {
        //横向连成三个
        for (int i = 0; i < 3; ++i)
        {
            if (state[i, 0] != 0 && state[i, 0] == state[i, 1] && state[i, 0] == state[i, 2])
            {
                return state[i, 0];
            }
        }
        //竖向连成三个
        for (int i = 0; i < 3; ++i)
        {
            if (state[0, i] != 0 && state[0, i] == state[1, i] && state[1, i] == state[2, i])
            {
                return state[0, i];
            }
        }
        //斜着连成三个-->必定包含正中间的那一个
        if (state[1, 1] != 0 && ((state[0, 0] == state[1, 1] && state[1, 1] == state[2, 2]) ||
            (state[0, 2] == state[1, 1] && state[1, 1] == state[2, 0])))
        {
            return state[1, 1];
        }

        // 平局-->全都有下(即不等于0),否则游戏继续
        int tied = 1;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (state[i, j] == 0)
                {
                    tied = 0;
                }
            }
        }
        if (tied == 1)
        {
            return 3;
        }
        else
        {
            return 0;
        }
    }

GitHub地址: https://github.com/LLLSic/Unity3D

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值