Unity3D学习笔记(一):Unity简介、游戏物体、组件和生命周期函数

Project(工程、项目):工程是把游戏开发当前所需要的资源归类管理用的。

Console控制台:日志、报错、调试,右上角,消息过滤

Assets:资源,存储游戏中一切用到的资源
Library:临时库文件
ProjectSettings:项目设置、项目控制
Temp:临时文件夹
UnityPackageManager:资源包
工程迁移:
Assets、ProjectSettings、UnityPackageManager,这三个文件夹必须拷贝走
如果报错,先删掉Library和Temp,再重新打开Unity
dependencies关联资源,引用资源
project面板:管理所有项目资源和文件
Inspector:当前选中游戏物体细节
 
程序=数据结构+算法
数据结构:存储数据的结构,栈、堆、链
List的核心是数组,
 
.unity后缀名:场景文件
.unitypackage后缀名:资源包
 
偏好设置
MonoDevelop:微软出品,早年为了实现跨平台(Mac),后来VS2017支持Mac
 
组件的概念:将一些具有功能性的代码封装起来,封装成一个类,当这个类挂到游戏物体身上去的时候,就相当于对此类的实例化,变成了具有这个功能的对象实体,那么这个游戏物体也就具有了这个对象所具有的功能。
组件的挂载相当于创建对象,添加功能性的对象。
游戏物体:
从表现层来看:场景视图中或者层级视图中能被看到的或者能被选中的都叫游戏物体。
从面向对象的角度来看:游戏物体是一个或者多个组件的集合。
Transform是基本组件,即使GameObject空物体也要有基本组件。坐标系,朝向,形体,
Position:单位米
Rotation:单位Unity(欧拉角,角度)和C#(四元素,弧度)
右手坐标系(数学运算)和左手坐标系(几何变换):拇指X轴右,食指Y轴上,中指Z轴前
Scale:单位倍数
Reset:重置还原
 
MonoBehaviour:是每个脚本派生的类的基类。
Start、Updata(生命周期函数、事件函数):在特定的时间点被游戏引擎被动调用的函数叫做生命周期函数。
Unity分为编辑内存和运行内存
第一步实例化所有游戏物体的组件
第二步激活所有游戏物体的组件
Awake():在此组件被实例化的时候执行一次
OnEnable():在此组件被激活的时候执行一次
OnDisable():在此组件被失活的时候执行一次
Start():在此组件对象第一次被激活的时候执行一次(在游戏开始的时候被执行一次,用来初始化)
Updata():每一画面刷新帧被调用一次(在游戏物体和脚本组件被激活的情况下)
FixedUpdate():每一物理帧更新刷新一次(物理帧是固定帧0.02秒)
LateUpdate():每一画面刷新帧被调用一次,只不过比Updata要晚(常用于摄像机跟随,防抖)OnDestroy():在此组件被销毁(remove)的时候执行一次(游戏运行时,在运行内存中销毁,编辑内存仍在)
帧(Frame):屏幕刷新的表示方式
帧率(FPS):帧率 = 帧数 / 时间 (单位时间内画面刷新的帧数)
肉眼:24
手游:30~60
PC/主机游戏:60~120
VR:120以上

collapse折叠调用,显示调用次数

整个游戏物体的激活与失活,游戏物体被激活失活时,会同步激活失活游戏物体的组件

单个物体组件的激活与失活

Mesh网格,正方形4个顶点2个三角面,平面10x10米,面片1x1米

Snap settings 吸附

给物体添加图标

Unity3D 快捷键
当前轴心旋转:Alt+鼠标左键
移动场景:鼠标滚轮键 / Q
缩放场景:使用鼠标滚轮键 / Alt+鼠标右键
居中所选物:F
飞行浏览:鼠标右键+WSAD(按Shift加速),QE上下
Edit菜单
播放:Ctrl+P,暂停:Ctrl+Shift+P,单帧预览:Ctrl+Alt+P
Assets菜单
刷新:Ctrl+R
GameObject菜单
新建空对象:Ctrl+Shift+N,移动物体到视图中心点:Ctrl+Alt+F,移动物体与视图对齐:Ctrl+Shift+F
Component菜单
添加组件:Ctrl+Shift+A
近距离查看游戏对象
在Hierarchy视图中选择游戏对象,然后在Scene视图中按快捷键“F”来近距离查看该游戏对象。
游戏对象不在主摄像头中
Hierarchy中双击选择需要显示的游戏对象,再单击Main Camera选中,最后Ctrl+Shift+F键盘即可。
旋转视图
Alt键 + 鼠标左键 可以任意拖动鼠标来旋转视图。如果x,y,z坐标轴恢复不了,可以先单击X轴(Y、Z)再鼠标右键选择“Free” 最后通过这个来调试。
缩放视图:滚动鼠标中键可以缩放整体视图。
变换工具栏:拖动工具:Q 移动工具:W 旋转工具: E 缩放工具:R
按住Ctrl键移动物体会以一定的增量来移动物体
Edit-Snap Setting 栅格和捕捉设置
Ctrl+Shift 使物体在另一个物体上移动,物体在被相交的物体上移动
顶点吸附 选中物体,按键盘V键 就可以看到物体的顶点:可以快速使两个物体定位
关闭顶点吸附:Shift+V
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]//类和结构都需要被序列化后,才可以在检视面板显示
public class Student
{
    public string name;
    public int age;
    public override string ToString()
    {
        return string.Format("[name:{0},age:{1}]",name ,age);
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Unity1803 : IEnumerable
{
    List<Student> students;//我没有加public,老师加了public
    public Unity1803()
    {
        students = new List<Student>()
        {
            new Student() { name = "张三",age= 20},
            new Student() { name = "李四",age= 18},
            new Student() { name = "王二",age= 22},
            new Student() { name = "赵六",age= 21},
            new Student() { name = "田七",age= 20},
        };
    }
    public IEnumerator GetEnumerator()
    {
        //返回的对象 必须实现了IEnumerator接口
        Unity1803IEnumerator ie = new Unity1803IEnumerator(students);
        return ie;
        //简便写法
        //yield是C#为了简化遍历操作实现的语法糖,我们知道如果要要某个类型支持遍历就必须要实现系统接口IEnumerable,这个接口后续实现比较繁琐要写一大堆代码才能支持真正的遍历功能。
        //for (int i = 0; i < students.Count; i++)
        //{
        //    yield return students[i];
        //}
    }
    public class Unity1803IEnumerator : IEnumerator
    {
        List<Student> students;
        int index = -1;
        public Unity1803IEnumerator(List<Student> students)
        {
            this.students = students;
        }
        public object Current
        {
            get
            {
                return students[index];
            }
        }
        //是否移出了集合的范围 返回true 表示当前的指针还在范围内 false则是在范围外
        public bool MoveNext()
        {
            index++;
            return index < students.Count;
        }
        public void Reset()
        {
            index = -1;
        }
    }
}
Unity复习
UnityEngine.Object是System.Object的子类,Unity默认继承UnityEngine.Object,C#默认继承System.Object
组件挂载的要求
1、继承MonoBehaviour类
2、文件名与类名一致
3、挂载时,所有脚本代码没有编译错误
4、挂载的脚本不能是抽象类
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]//类和结构都需要被序列化后,才可以在检视面板显示
public class Enemy
{
    public string name;
    public int atk;
    public int hp;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum EEnemyType
{
战士,
法师
}
public class NewBehaviourScript : MonoBehaviour
{
    //以检视面板为准
    public int intNum = 0;
    //私有变量不会在检视面板显示
    //特性:[]标签的形式
    //可以把私有的字段在检视面板中显示出来
    [SerializeField]//序列化字段 Attribute 反射(程序集.exe .all)
    private float floatNum = 0.0f;
    [Multiline(4)]//让字符串多行显示
    public string str = "";
    [Header("战士的类型")]//说明文本
    public EEnemyType enemyType = EEnemyType.战士;
    [Header("XX的数量")]
    public int[] nums;
    [Header("XX的数量")]
    public List<int> listArray;
    public Student student;
    public List<Enemy> enemyList;
    public Enemy enemy = new Enemy();
    public Unity1803 unity = new Unity1803();
    private void Awake()
    {
        //Debug.LogFormat("Int :{0}", intNum);
        //Debug.Log("Float :" + floatNum);
        //Debug.Log("Str : " + str);
        //Debug.Log("EnemyType :" + enemyType);
        ////Debug.Log("Awake");
        ////Debug.LogWarning("Awake");
        ////Debug.LogError("Awake");
        //foreach (var item in nums)
        //{
        //    Debug.Log("item :" + item);
        //}
    }
    private void OnEnable()
    {
        Debug.Log("this is OnEnable");
    }
    // Use this for initialization
    void Start()
    {
        Debug.Log("this is Start");//输出
        // 挂载的条件
        // 1. 继承  MonoBehaviour
        // 2. 文件名与类名相同
        // 3. 挂载时,所有的代码无编译错误
        // 4. 挂载的脚本不能是抽象类
        int num = Random.Range(1,55);
        Debug.Log(num);
        int[] nums = new int[] { 1, 2, 3, 5, 7, 8, 9 };
        List<int> list = new List<int>() {11,22,33,66,7,88,9 };
        foreach (var item in nums)
        {
            Debug.Log("item:" + item);
        }
        foreach (var item in list)
        {
            Debug.Log("item:" + item);
        }
        //IEnumerable 枚举器:其实就是一种机制,具有可以逐个访问某个集合每个元素的功能 {a,b,c,d}
        //IEnumerator 迭代器:实现了上面这种机制的具体的 (一个或多个)方法
        Unity1803 unity = new Unity1803();
        foreach (var item in unity)
        {
            Debug.Log("item:" + item);
        }
    }
    // Update is called once per frame
    void Update()
    {
        Debug.Log("this is Update");
    }
    private void FixedUpdate()
    {
        Debug.Log("this is FixedUpdate");
    }
    private void LateUpdate()
    {
        Debug.Log("this is LateUpdate");
    }
    private void OnDisable()
    {
        Debug.Log("this is OnDisable");
    }
    private void OnDestroy()
    {
        Debug.Log("this is OnDestroy");
    }
}

OnPreCull: 在相机剔除场景之前调用此函数。相机可见的对象取决于剔除。OnPreCull 函数调用发生在剔除之前。
OnBecameVisible/OnBecameInvisible: 在对象对于相机可见/不可见时调用此函数。
OnWillRenderObject: 如果对象可见,则为每个相机调用一次此函数。
OnPreRender: 在相机开始渲染场景之前调用此函数。
OnRenderObject: 在完成所有常规场景渲染后调用此函数。此时,可使用 GL 类或 Graphics.DrawMeshNow 绘制自定义几何图形。
OnPostRender: 在相机完成场景渲染后调用此函数。
OnRenderImage(仅限专业版): 在完成场景渲染后调用此函数,以便对屏幕图像进行后处理。
OnGUI: 在每帧上多次调用此函数,以响应 GUI 事件。程序首先将处理 Layout 和 Repaint 事件,然后再处理每个输入事件的 Layout 和 keyboard/鼠标事件。
OnDrawGizmos: 用于在场景视图中绘制小图示 (Gizmos),以实现可视化目的。

猜你喜欢

转载自www.cnblogs.com/ciaowoo/p/10362670.html
今日推荐