3d

1、简答题

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

游戏对象(GameObjects):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.

资源(Assets):An asset is representation of any item that can be used 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 types of file that Unity supports. There are also some asset types that can be created within Unity, such as an Animator Controller, an Audio Mixer or a Render Texture.

区别与联系:区别在于游戏对象是我们要创建的人物、道具、场景之类的东西,而资源则是创作游戏时加进来的声音、脚本、材质等。联系就在于我们可以使用资源来创建游戏对象。

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

资源的目录组织结构和游戏对象结构都是树形结构。资源的文件里包括脚本,声音,图像,预设等,按照文件类型放在相应的文件夹中,这些文件夹下继续划分。游戏对象分为游戏控制,环境,玩家,目标,对象跟上级对象是继承关系。

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

代码如下:

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

public class Test : MonoBehaviour {
	void Awake() {
		Debug.Log ("It is Awake!");
	}
	
    void Start() {
        Debug.Log ("It is Awake!");
    }
    
	void Update() {
		Debug.Log ("It is Update!");
	}
	
	void FixedUpdate() {
		Debug.Log ("It is FixedUpdate!");
	}
	
	void LateUpdate() {
		Debug.Log ("It is LateUpdate!");
	}
	
	void Update() {
		Debug.Log ("It is Update!");
	}
	
	void OnGUI() {
		Debug.Log ("It is OnGUI!");
	}
	
	void OnDisable() {
		Debug.Log ("It is OnDisable!");
	}
	
	void OnEnable() {
		Debug.Log ("It is OnEnable!");
	}
}

运行后如下:
在这里插入图片描述

Awake():Awake is called when the script instance is being loaded.

Start():Start is called on the frame when a script is enabled just before any of the Update methods are called the first time.

Update():Update is called every frame, if the MonoBehaviour is enabled.

FixedUpdate():与Update()一样,都是当MonoBehaviour启用时,在每一帧被调用,不同的是FixedUpdate()每帧与每帧之间相差的时间是固定的,而Update()每一帧的时间不固定。

LateUpdate():在所有Update函数调用后被调用。

OnGUI() :OnGUI is called for rendering and handling GUI events.

OnDisable():This function is called when the behaviour becomes disabled.

OnEnable():This function is called when the object becomes enabled and active.

(4)查找脚本手册,了解 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.

游戏对象是Unity中表示游戏角色,游戏道具和游戏场景的基本对象。他们自身无法完成许多功能,但是他们构成了那些给予他们实体功能的组件的容器。

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

转换组件决定了游戏场景中每个游戏对象的位置,旋转和缩放比例。每个游戏对象都有转换组件。

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 的部件
    img

table对象的属性:从图中可以看到(从左到右,从上到下),第一个框是activeInHierarchy属性,控制table的active状态。第二个框是isStatic属性,指定游戏对象是否为静态。第三个框是Tag属性,它是游戏对象的标签。第四个是Layer属性,显示游戏对象所在的图层。此外还有activeSelf、scene、transform属性。

table的Transform的属性:有Position、Rotation、Scale属性。

table的部件:Mesh Filter、Box Collider、Mesh Renderer。

  • 用 UML 图描述 三者的关系(请使用 UMLet 14.1.1 stand-alone版本出图)
    在这里插入图片描述
(5)整理相关学习资料,编写简单代码验证以下技术的实现
  • 查找对象
//通过名字查找
public static GameObject Find(string name)

//通过标签查找单个对象
public static GameObject FindWithTag(string tag)

//通过标签查找多个对象
public static GameObject[] FindGameObjectsWithTag(string tag)
  • 添加子对象
public static GameObect CreatePrimitive(PrimitiveTypetype)
  • 遍历对象树
foreach (Transform child in transform) {  
    Debug.Log(child.gameObject.name);  
}  
  • 清除所有子对象
foreach (Transform child in transform) {  
    Destroy(child.gameObject);  
} 
(6)资源预设(Prefabs)与 对象克隆 (clone)
  • 预设(Prefabs)有什么好处?

Prefabs(预设)是最非常用的一种资源类型,是一种可被重复使用的游戏对象。它可以被置入多个场景中,也可以在一个场景中多次置入。所有Prefabs实例都是Prefab的克隆,所以如果实在运行中生成对象会有(Clone)的标记。只要Prefabs原型发生改变,所有的Prefabs实例都会产生变化。如果需要创建一些想要重复使用的东西,预设就非常有用了。

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

克隆游戏对象需要场景中有被克隆对象,而创建预制只需事先创建预制即可,允许场景中一开始并不存在该游戏对象。克隆出来的游戏对象并不会随着被克隆体的变化而发生变化,但是使用预制创建出来的对象会随着预制的改变而发生改变。

  • 制作 table 预制,写一段代码将 table 预制资源实例化成游戏对象
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class table : MonoBehaviour {
	public GameObject original; 
	void Start () {
		GameObject table1 = (GameObject)Instantiate(original);
	}

	void Update () {}
}

2、编程实践

代码块如下:

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

public class chess : MonoBehaviour  {
	private int [,] board = new int[3,3];
	private int turn = 1;
	private int steps = 9;  //可以走多少步
	
	void start() {
		restart();
	}
	
	void restart() {
		steps = 9;
		turn = 1;
		for(int i = 0; i < 3; i++) {
			for(int j = 0; j < 3; j++) {
				board[i,j] = 0;
			}
		}
	}

    int check() { 
        //列举所有可能赢的状态
        int tmp = board[1,1];
        if(tmp != 0) {
            if(tmp == board[0, 0] && tmp == board[2, 2]) {
                return tmp;
            }
            if(tmp == board[0, 2] && tmp == board[2, 0]) {
               return tmp; 
            }
            if(tmp == board[0, 1] && tmp == board[2, 1]) {
               return tmp; 
            }
            if(tmp == board[1, 0] && tmp == board[1, 2]) {
               return tmp; 
            }
        }
        
        tmp = board[0,0];
        if(tmp != 0) {
            if(tmp == board[0,1] && tmp == board[0,2]) {
                return tmp;
            }
            if(tmp == board[1,0] && tmp == board[2,0]) {
                return tmp;
            }
        }
        
        tmp = board[2,2]; 
        if(tmp != 0) {
            if(tmp == board[2,0] && tmp == board[2,1]) {
                return tmp;
            }
            if(tmp == board[0,2] && tmp == board[1,2]) {
                return tmp;
            }
        }
        
        //打成平局
        if(steps == 0) {
            return 3;
        }
        else {
            return 0;
        }
    }
	 
	private void OnGUI() {
		if(GUI.Button(new Rect(350,380,100,60), "Restart")) {
            restart();
        }
        
        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                //1代表×,2代表o
                if(board[i,j] == 1) {
                    GUI.Button(new Rect(i * 100 + 250, j * 100 + 60, 100, 100), "X");
                }
                if(board[i,j] == 2) {
                    GUI.Button(new Rect(i * 100 + 250, j * 100 + 60, 100, 100), "O");
                }
                if(GUI.Button(new Rect(i * 100 + 250, j * 100 + 60, 100, 100), "")) {
                     //开始的时候都是0
                    if(result == 0) {
                		if(turn == 1)  board[i,j] = 1;
						if(turn == 2)  board[i,j] = 2;
                        steps--;
                        if(steps % 2 == 1) {
                            turn = 1;
                        }
                        else turn = 2;
            		}
                }
            }  
        }

        int result = check();
        if(result == 1) {
            GUI.Label(new Rect(350, 20, 100, 50), "X wins");
        }
        else if(result == 2) {
            GUI.Label(new Rect(350, 20, 100, 50), "O wins");
        }
        else if(result == 3) {
            GUI.Label(new Rect(350, 20, 100, 50), "Tied");
        }
	}
 }

运行结果如下:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值