3D学习笔记(1)

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

游戏中的每个对象都是一个游戏对象(角色、道具、景物),游戏对象本身不做任何事情。游戏对象是一种容器,能容纳不同的组件,而组件可以实现功能,组件可以为游戏对象提供特殊属性,然后游戏对象才能成为角色、环境或特效。

资源表示 Unity 项目中用来创建游戏或应用的任何项。资源可以代表项目中的视觉或音频元素,例如 3D 模型、纹理、精灵、音效或音乐。资源还可以表示更抽象的项目,例如任何用途的颜色渐变、动画遮罩或任意文本或数字数据。

资源可以实例化为具体的游戏对象,用于构造游戏对象,装饰游戏对象,配置游戏的物体和数据。

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

在这里插入图片描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/236a9b3db69e4ecd9732746eb2dfcd33.png

从以上可以看到,资源的组织结构包括音频、场景、脚本、预制等资源文件。
游戏对象可以分为光源、摄像机、各种实体等。

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

基本行为包括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!");
    }
}

控制台输出如下:
在这里插入图片描述

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

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

GameObject: 用于表示任何可以存在于场景中的事物。
Transform: 提供多种方式来通过脚本处理游戏对象的位置、旋转和缩放,以及与父和子游戏对象的层级关系。
Component: 一切附加到游戏物体的基类。

描述下图中cube对象的属性

在这里插入图片描述
Transform: 定义对象的位置、绕坐标轴的转动角度和大小
Box Collider:调整坐标系的位置和大小
Component:给对象增加行为

用UML图描述三者的关系

在这里插入图片描述

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

预设有什么好处

预设是预先设计好的游戏对象,有完整的组件以及属性。在设计的过程中,随时可以直接从资源当中加载,成为一个游戏对象。预设的存在,方便了面向对象思想的应用,使我们在设计游戏中更加便捷。

预设与对象克隆的关系

根据预设生成实例时,如果修改预设,由它实例的对象都会改变;对象克隆是从已有的对象中复制出新的对象,修改克隆的对象时不会影响另一个,两个对象之间是相互独立的。

制作table 预设,写一段代码将table预设资源实例化为游戏对象
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class prefabBeh: MonoBehaviour {
    public Transform table;
    void Start () {
        table = gameObject.GetComponent<Transform>();
        GameObject table2 = (GameObject)Instantiate(Resources.Load("table"));
        table2.transform.position = new Vector3(0, 0, 0);
        table2.transform.parent = this.transform;
    }
    void Update() {}
}

编程实践:井字棋

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

public class Chess_1 : MonoBehaviour
{
    GUIStyle myStyle;
    private int turn = 1;
    private int chess_num = 0;

    private int[,] matrix = new int[3,3];
    int middle = Screen.width / 2;//用于UI,得到屏幕中央位置

    void reset(){
        turn = 1;
        chess_num = 0;
        for(int i = 0 ;  i < 3  ; i ++){
            for(int j = 0 ; j < 3 ; j ++)
                matrix[i,j] = 0;
        }
        print("game reseted.");
    }

    int check(){
        for(int i =  0 ; i < 3 ; i ++){
            // 列的检测
            if(matrix[i,0] != 0 && (matrix[i,0] == matrix[i,1] && matrix[i,0] == matrix[i,2]) ){
                return matrix[i,0];
            }    
            // 行的检测
            if(matrix[0,i] != 0 && (matrix[0,i] == matrix[1,i] && matrix[0,i] == matrix[2,i]) ){
                return matrix[0,i];
            } 
        }
        // 对角线检测
        if(matrix[0,0] != 0 && (matrix[0,0] == matrix[1,1] && matrix[0,0] == matrix[2,2]) ){
            return matrix[0,0];
        }
        if(matrix[0,2] != 0 && (matrix[0,2] == matrix[1,1] && matrix[0,2] == matrix[2,0]) ){
            return matrix[0,2];
        }
        // 对局还没结束
        if (chess_num < 9) return 0;
        // 平局
        return 3;
    }

    void result(int res){
        switch(res){
            case 0: 
                // 对局还没结束,显示目前轮到谁的场次
                if(turn == 1) 
                    GUI.Box(new Rect(middle - 45, 55, 100, 35), "O is going");
                else if(turn == 2)
                    GUI.Box(new Rect(middle - 45, 55, 100, 35), "X is going");
                break;
            case 1:
                GUI.Box(new Rect(middle - 45, 55, 100, 35), "O Win!");
                break;
            case 2:
                GUI.Box(new Rect(middle - 45, 55, 100, 35), "X Win!");
                break;
            case 3:
                GUI.Box(new Rect(middle - 45, 55, 100, 35), "Draw!");
                break;
        }

    }

    void click(int i, int j){
        matrix[i,j] = turn;
        chess_num ++;
        if(turn == 1) 
            turn = 2;
        else 
            turn = 1;
    }

    void OnGUI(){
        // 游戏背景的box
        GUI.Box(new Rect(middle - 120, 30, 252, 330), "tictactoe");
        // restart按钮
        if (GUI.Button(new Rect(middle - 35, 95, 75, 30), "Restart")){
            reset();
        }
       
        int res = check();
        for(int i = 0 ; i < 3 ; i ++){
            for(int j = 0 ; j < 3 ; j ++){
                if(matrix[i,j] == 1){
                    GUI.Button(new Rect(middle - 120 + i*84, 135 + j*70 , 84, 70), "O");
                }
                if(matrix[i,j] ==2){
                    GUI.Button(new Rect(middle - 120 + i*84, 135 + j*70 , 84, 70), "X");
                }
                if(GUI.Button(new Rect(middle - 120 + i*84, 135 + j*70 , 84, 70), "")){
                    click(i,j);
                }
            }
        }
        result(res);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值