Unity3D游戏编程——离散仿真作业
1.简答题asd
1.1解释游戏对象(GameObjects)和资源(Assets)的区别与联系
答:游戏对象(GameObjects)是一系列资源的组成序列。资源(Assets)是一系列可以被Unity采用的数据文件(如纹理,材质,音频等),只有有了相应的资源,在Unity项目中才能呈现出相应的效果(比如需要木质表面,项目文件中就必须有木质材质的资源文件),有了资源之后,我们便可以在Unity中利用已有的资源来构建我们各种各样的游戏对象了,游戏对象是游戏过程中的基本操作单位。
1.2下载几个游戏案例,分别总结资源,对象组织的结构(指资源的目录组织结构与游戏对象树的层次结构)
答:我在Github选取了游戏《天天萌泡泡》作为游戏案例来进行分析。它的游戏对象树和资源目录结构如下:
从游戏对象树我们可以看到,该游戏的游戏对象树的底层先分了几个大的GameObject来承担不同方面的工作,然后各个大GameObject又包含小GameObject来分管某方面的细节性工作。
从资源目录结构我们可以看出,游戏将资源分为动画,音频,字体,图片,插件等方面来分别存储。
1.3编写一个代码,使用debug语句来验证MonoBehaviour基本行为或事件触发的条件,基本行为包括Awake,Start,Update,FixedUpadate,LateUpdate,常用事件包括OnGUI,OnDisable,OnEnable。
答:
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstBeh : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("This Start!");
}
void Awake()
{
Debug.Log("This Awake!");
}
void FixedUpdate()
{
Debug.Log("This FixedUpdate!");
}
void LateUpdate()
{
Debug.Log("This LateUpdate!");
}
void OnGUI()
{
Debug.Log("This OnGUI!");
}
void OnDisable()
{
Debug.Log("This OnDisable!");
}
// Update is called once per frame
void Update()
{
Debug.Log("This Update!");
}
void OnEnable()
{
Debug.Log("This Enabled!");
}
}
代码运行效果如下:
开始:
运行中:
结束
1.4查找脚本手册,了解GameObject,Transform,Component对象。
翻译官方对三个对象的描述:1.GmaeObject是所有Unity场景中的对象的基类。 2.Transform:是一个对象的位置,大小,角度等属性。每个对象都有Transform属性,Transform也有层次组织,以达到相对位置的效果。 3.Component:每个对象都有的基本类,它包括脚本等组件。
描述Table对象(实体)的属性,Table的Transform的属性,Table的组件:
我们可以看到Inspector中第一栏就是activeSelf属性,它的Transform属性在第二栏,包括了位置(0,0,0)
,旋转(0,0,0),大小(1,1,1),除了activeSelf都是Component,包括transform,mesh render等,还包括我自己加入的脚本。
UML图如下:
1.5整理相关学习资料,编写简单代码验证以下技术
查找对象:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SecnodBeh : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
var ob = GameObject.Find("table");
if (ob != null)
{
Debug.Log("Success");
}
}
// Update is called once per frame
void Update()
{
}
}
添加子对象:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SecnodBeh : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GameObject ob = GameObject.CreatePrimitive(PrimitiveType.Cube);
ob.name = "chair5";
ob.transform.position = new Vector3(0, 5, 5);
ob.transform.parent = this.transform;
}
// Update is called once per frame
void Update()
{
}
}
遍历对象树:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SecnodBeh : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("begin");
foreach (Transform child in transform)
{
Debug.Log(child.name);
}
}
// Update is called once per frame
void Update()
{
}
}
清除所有子对象:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SecnodBeh : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
for(int i = 0; i < transform.childCount; i++)
{
Destroy(transform.GetChild(i).gameObject);
}
}
// Update is called once per frame
void Update()
{
}
}
1.6资源预设与对象克隆:
预设的好处:预设是指对GameObject的一种描述,它可以很方便的生成多个相同的对象,但他们实质上是同一份,有点类似于指针的感觉,预设能够方便我们重复性的创建某些功能形状都一致的对象。
预设与对象克隆的关系:他们的区别类似于C/C++中的深拷贝和浅拷贝,预设的修改会改变所有相关的对象,但是对象克隆产生的各个对象是独立的。
写一段代码将table预设资源实例化为游戏对象:
(GameObject)Instantiate(Resources.Load("table"));
使用这段代码的前提是要先将table预设进资源库中,此处参考了其它博客。
2.编程实践,小游戏
游戏内容:
井字棋
技术限制:
仅允许使用 IMGUI 构建 UI
作业目的:
了解 OnGUI() 事件,提升 debug 能力
提升阅读 API 文档能力
源代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoreCode : MonoBehaviour
{
private bool flag = false;
private int[, ] state = new int[3, 3];
private int turn = 1;
private int AI = 0;
GUIStyle white = new GUIStyle();
GUIStyle black = new GUIStyle();
// Start is called before the first frame update
void Start()
{
for(int i = 0; i < 3; i++)
{
for (int j = 0; j < 3;