什么是Unity?
Unity是一套具有完善体系与编辑器的跨平台游戏开发工具,也可以称之为游戏引擎。
游戏引擎是指一些编写好的可重复利用的代码与开发游戏所用的各功能编辑器。
Unity目前已超过50%的游戏引擎市场占有率。
开发一款游戏
游戏引擎:渲染引擎、物理引擎、碰撞检测、特效系统、......功能
编写游戏逻辑
完成游戏开发
Unity引擎优势
1.基于C#编程,拥有易上手、高安全性的特性
2.独特的面向组件游戏开发思想让游戏开发更加简单易复用
3.拥有十分成熟的所见即所得开发编辑器
4.拥有良好生态圈,商城中包含大量成熟的功能脚本与资源
5.强大的跨平台特性,可以制作PC、主机、手机、AR、VR等多平台游戏
向量:既有大小,也有方向
向量的模:向量的大小
单位化,归一化:把向量转为单位向量的过程
向量的运算 A(1,2);B(2,1)
加法:
A(x1y1) + B(x2y2) = x1+x2,y1+y2 = 3,3
减法:
A(x1y1) - B(x2y2) = x1-x2,y1-y2 = -1,1
点乘:得到两个向量之间的夹角
A·B=x1x2 + y1y2 = n = |a||b|cos& = cos&
n = cos& 通过反余弦公式可得到角度的大小(夹角)
预制件,方便量产组件及修改
修改预制件会修改场景中的文件
点选择,会自动选择预制件
预制件变体
Vector3:向量,坐标,旋转,缩放
Vector3 v = new Vector3(1,1,0.5f);
v = Vecto3.zero;
v = Vector3.right;
Vector3 v2 = Vector3.forward();
计算两个向量夹角:Debug.Log(Vector3.Angle(v,v2));
计算两点之间距离:Debug.Log(Vector3.Distance(v,v2));
点乘:Debug.Log(Vector3.Dot(v,v2));
叉乘:Debug.Log(Vector3.Cross(v,v2));
插值:Debug.Log(Vector3.Cross(v,v2));
插值:Debug.Log(Vector3.Lerp(Vector3.zero,Vector3.one,0.8f));
向量的模Debug.Log(v.magnitude);
规范化向量Debug.Log(v.normalized);
使用欧拉角,四元数描述旋转
// 旋转
Vector3 rotate = new Vector3(0, 30, 0);
Quaternion quaternion = Quaternion.identity;
quaternion = Quaternion.Euler(rotate);
// 看向一个物体
quaternion = Quaternion.LookRotation(new Vector3(0, 0, 0));
输出日志
Debug.Log("test");
Debug.LogWarning("test2");
Debug.LogError("test3")
绘制线(开发可见、玩家不可见)
直线:Debug.DrawLine(Vector3.zero, Vector3.one, Color.red);
射线:Debug.DrawRay(new Vector3(1, 0, 0), Vector3.up, Vector3.yellow);
每个游戏物体都是一个GameObject
获取脚本挂载的游戏物体:GameObject go = this.gameObject;
名称:Debug.Log(gameObject.name);
tag:Debug.Log(gameObject.tag);
layer:Debug.Log(gameObject.layer);
立方体的名称:Debug.Log(Cube.name);
当前真正的激活状态:Debug.Log(Cube.activeInHierarchy);
当前自身激活状态:Debug.Log(Cube.activeSelf);
获取Transform组件:transform
获取位置:Debug.Log(transform.position);
获取位置:Debug.Log(transform.localPosition);
获取旋转:Debug.Log(transform.rotation);
获取旋转:Debug.Log(transform.localRotation);
获取旋转:Debug.Log(transform.eulerAngles);
获取旋转:Debug.Log(transform.localEulerAngles);
获取缩放:Debug.Log(transform.localScale);
向量:Debug.Log(transform.forward);
向量:Debug.Log(transform.right);
向量:Debug.Log(transform.up);
获取其它组件:BoxCollider bc = GetComponent<BoxCollider>();
获取当前物体的子物体身上某个组件:GetComponentInChildren<CapsuleCollider>(bc);
获取当前物体的父物体身上某个组件:GetComponentInParent<BoxCollider>();
添加一个组件:gameObject.AddComponent<>();
通过游戏物体的名称来获取游戏物体:GameObject test = GameObject.Find("test");
t通过游戏标签来获取游戏物体:test = GameObject.FindWithTag("Enemy");
通过预设体来实例化一个游戏物体:GameObject go = Instanitate(Prefab, Vector3.zero, Quaternion.identity);
销毁:Destroy(go);
游戏开始到现在所花时间:Debug.Log(Time.time);
时间缩放值:Debug.Log(Time.timeScale); // 加速、减速、暂停
固定时间间隔:Debug.Log(Time.fixedDeltaTime);
上一帧到这一帧所用的游戏时间:Debug.Log(Time.deltaTime); // 若每秒60帧,则为1/60
计时器:float timer = 0; timer += Time.deltaTime;
游戏数据文件夹路径(只读,加密压缩):Debug.Log(Application.dataPath + "/新建文本文档.txt");
持久化文件夹路径:Debug.Log(Application.persistentDataPath);
StreamingAssets文件夹路径 (只读,配置文件,需要手动创建文件夹):Debug.Log(Application.streamingAssetsPath);
临时文件夹(缓存):Debug.Log(Application.temporaryCachePath);
控制是否在后台运行:Debug.Log(Application.runlnBackground);
打开URL:Application.OpenURL("https://space.bilibili.com/67744423");
退出游戏:Application.Quit();
游戏→场景→物体→脚本
场景跳转:SceneManager.LoadScene("MyScene");
获取当前场景:Scene scene = SceneManager.GetActiveScene();
场景名称:Debug.Log(scene.name);
场景是否已经加载:Debug.Log(scene.isLoaded);
场景路径:Debug.Log(scene.path);
场景索引:Debug.Log(scene.buildIndex);
GameObject[] gos = scene.GetRootGameObjects();
Debug.Log(gos.Length);
场景管理类:using UnityEngine.SceneManager;
场景数量:Debug.Log(SceneManager.sceneCount);
创建新场景:Scene newScene = SceneManager.CreaterScene("newScene"); // 场景是可以存在多个的,多个叠加起来
卸载场景:SceneManager.UnloadSceneAsync(newScene);
加载场景:SceneManager.LoadScene("MyScene", LoadSceneMode.Single);
加载叠加场景:SceneManager.LoadScene("MyScene", LoadSceneMode.Additive);
加载大型场景:SceneManager.LoadSceneAsync(todo);
协程方法用来异步加载场景:
AsyncOperation operation;
void Start() {
StartCoroutine(loadScene());
}
// 协程方法异步加载场景
Ienumerator loadScene(){
operation = SceneManager.LoadSceneAsync(1);
operation.allowSceneActivation = false; // 不允许场景自动跳转
yield return operation;
}
void Update() {
// 输出加载进度(0~0.9)
Debug.Log(operation.progress);
if (operation.progress == 0.9) {
operation.allowSceneActivation = true;
}
}
Transform也用于维持物体之间的父子级关系
void Update() {
时刻看向000点:transform.LookAt(Vector3.zero);
自转:transform.Rotate(Vector3.up, 1);
公转:transform.RotateAround(Vector3.zero, Vector3.Up, 5 )
}