3D游戏编程1--井字棋

1.简答题

GameObjects和asserts的区别和联系

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.

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.

分别总结资源、对象组织的结构

资源和对象组织结构呈树形结构,一个对象包含多种资源,比如声音,图片之类的

debug 语句来验证 MonoBehaviour 基本行为或事件触发的条件

// 每隔特定的时间更新
void FixedUpdate(){
    Debug.Log("FixedUpdate");
}
// 程序一开始调用
void Awake(){
    Debug.Log("Awake");
}
// 在Awake之后调用
void Start () {
    Debug.Log("Start");
}

// 每帧调用一次
void Update () {
    Debug.Log("Update");
}
// 所有对象渲染完毕调用
void LateUpdate(){
    Debug.Log("Update");
}
// 对GUI的对象产生交互时调用
void OnGUI()
{
    if (GUI.Button(new Rect(10, 10, 150, 100), "I am a button"))
        print("You clicked the button!");

}
// 对象可用时调用
void OnDisable(){
    Debug.Log("PrintOnDisable: script was disabled");
}
// 对象不可用时调用
void OnEnable(){
    print("script was enabled");
}

了解 GameObject,Transform,Component 对象

翻译官方对三个对象的描述

GameObjects 是Unity中代表人物,道具和风景的基本对象
Transform 确定场景中的每个对象的位置,旋转,和比例
Component 没有单独的解释,是对象的组成部分

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

table

table GameObject
由Component组成

table Tranform 一种Component 描述以下属性

  • Position
  • Rotation
  • Scale

table Component有以下几个Component

  • Transform
  • Cube(Mesh filter)
  • Box Collider
  • Mesh Renderer
用 UML 图描述 三者的关系

uml
https://github.com/yaoxh6/3D/blob/master/Asset/homework1.uxf

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

查找对象

Find (Finds a GameObject by name and returns it.)

FindGameObjectsWithTag (Returns a list of active GameObjects tagged tag. Returns empty array if no GameObject was found.)

FindWithTag (Returns one active GameObject tagged tag. Returns null if no GameObject was found.)

添加子对象

CreatePrimitive (Creates a game object with a primitive mesh renderer and appropriate collider.)

遍历对象树以及清楚所有子对象
List lst = new List;
foreach (Transform child in transform){
    lst.Add(child); 
    Debug.Log(child.gameObject.name);
}
for(int i = 0;i<lst.Count;i++){
    Destroy(lst[i].gameObject);
}

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

预设(Prefabs)有什么好处?

可以将已经做好的对象作为一个模板,可以直接使用,而且改动Prefabs改动,将对所有的由Prefabs产生的对象改动

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

Prefabs和clone都可以产生与本体一样的对象,但是Prefabs与产生的对象相关,clone与产生的对象无关。

制作 table 预制,写一段代码将 table 预制资源实例化成游戏对象
public GameObject obj;
void Start () {
    GameObject instance = (GameObject)Instantiate(obj.gameObject, transform.position, transform.rotation);
}

尝试解释组合模式

组合模式可以将多个物体组合成为一个整体,可以使用户对整体进行操作,使得整体和个体具有一致性。
为子类绑定如下函数

public class test2 : MonoBehaviour {

    void hello()
    {
        Debug.Log("world");
    }
    ……
}

为父类绑定如下函数

public class test : MonoBehaviour {

    void hello()
    {
        Debug.Log("hello world");
    }

    void Start()
    {
        BroadcastMessage("hello");
    }
    ……
}

就可以产生如下图效果,说明当父类调用Start函数时,整体和个体都调用hello函数
BroadcastMessage

2.井字棋

https://github.com/yaoxh6/3D/blob/master/Asset/homework1.mp4

游戏展示

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

public class Chess : MonoBehaviour
{
    private int[,] game = new int[3, 3];
    private int next = 1;
    public Texture2D assassin;
    public Texture2D knight;
    public void Start()
    {
        Reset();
    }

    public void Reset()
    {
        next = 1;
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                game[i, j] = 0;
            }
        }
    }

    int IsWin()
    {

        for(int i = 0; i < 3; i++)
        {
            //检查行
            if(game[i,0] == game[i,1] && game[i,1] == game[i, 2] && game[i,0] != 0)
            {
                return game[i,0];
            }//检查列
            else if(game[0,i] == game[1,i] && game[1,i] == game[2,i] && game[0,i] != 0)
            {
                return game[0,i];
            }

        }
        //检查对角线
        if(game[0,0] == game[1,1] && game[1,1] == game[2,2] && game[0,0] != 0)
        {
            return game[0, 0];
        }
        if(game[0,2] == game[1,1] && game[1,1] == game[2,0] && game[0,2] != 0)
        {
            return game[0, 2];
        }

        //检查游戏是否结束
        int count = 0;
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                if(game[i,j] != 0)
                {
                    count++;
                }
            }
        }
        if(count == 9)
        {
            return 3;
        }
        return 0;
    }


    public void OnGUI()
    {
        if (GUI.Button(new Rect(300, 200, 100, 50), "重新开始"))
        {
            Reset();
        }
        int result = IsWin();
        Debug.Log(result);
        if(result == 1)
        {
            GUI.Label(new Rect(300, 0, 50, 50), assassin);
            GUI.Label(new Rect(350, 20, 50, 50), "O 赢了");
        }
        else if(result == 2)
        {
            GUI.Label(new Rect(300, 0, 50, 50), knight);
            GUI.Label(new Rect(350, 20, 50, 50), "X 赢了");
        }
        else if(result == 3)
        {
            GUI.Label(new Rect(300, 20, 50, 50), "平局");
        }
            for(int i = 0; i < 3; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    if(game[i,j] == 1)
                    {
                        GUI.Button(new Rect(300 + i * 50, 50 + j * 50, 50, 50), assassin);
                    }
                    else if(game[i,j] == 2)
                    {
                        GUI.Button(new Rect(300 + i * 50, 50 + j * 50, 50, 50), knight);
                    }
                    if(GUI.Button(new Rect(300 + i * 50, 50 + j * 50, 50, 50), ""))
                    {
                        if(result == 0)
                        {
                            if (next % 2 == 1)
                            {
                                game[i, j] = 1;
                            }
                            else
                            {
                                game[i, j] = 2;
                            }
                        next++;
                        }
                    }
                }
            }

    }
    public void Update()
    {

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值