Unity 入门

  • Unity 入门

      • MonoBehavior 基类
      • 生命周期函数
      • 获取脚本
      • GameObject
        • 一些属性
        • 静态方法
        • 成员方法
      • 时间相关 API
        • 时间缩放
      • Vector3
      • Position
      • 位移
      • EulerAngles
      • 缩放
        • 看向
      • 父子关系
      • 坐标转换
        • 世界坐标系 转 本地坐标系
        • 本地坐标系 转 世界坐标系
      • Input
        • 鼠标相关
        • 键盘相关
        • 默认轴输入
        • 其他
      • Camera
        • 参数
        • 成员
      • 碰撞检测
        • Rigidbody 刚体
        • Collider
        • 物理材质
        • 常用函数
          • 物理碰撞检测
          • 触发器检测
          • 其他注意
      • 刚体加力
        • 方法
          • 添加水平力
          • 添加扭矩力
          • 改变速度
          • 给世界坐标系的点加力
        • 力的模式
        • 自带的力相关脚本
        • 刚体休眠

MonoBehavior 基类

在这里插入图片描述

生命周期函数

在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class test : MonoBehaviour
{
// 创建时调用,即脚本被挂载上对象时调用,当挂载上后只调用一次与激活失活无关
private void Awake() {
print("Awake!!");
}

// 激活时调用,脚本文件被激活时调用,脚本文件激活需要对象和脚本都激活
private void OnEnable() {
print("OnEnable!!");
}

// 第一次帧更新之前调用,一个函数只调用一次
void Start()
{
print("Start!!");
}

// 物理帧更新调用,可以修改物理帧间隔时间
private void FixedUpdate() {
print("FixedUpdate");
}

// 逻辑帧更新
private void Update()
{
print("Update");
}

// 一般用来处理摄像机更新的内容
private void LateUpdate() {
print("LateUpdate");
}

// 失活时调用
private void OnDisable() {
print("OnDisable");
}

// 销毁时调用
private void OnDestroy() {
print("OnDestroy!!");
}
}

获取脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 得到挂载对象的其他脚本
otherTest myot = this.GetComponent<otherTest>();
print("my Component :" + myot);

otherTest ot = obj.GetComponent<otherTest>();
print("other Component :" + ot);

// 子对象挂载的脚本(自身的挂载也会去查找)
// GetComponentInChildren<otherTest>(false),参数默认是false(失活不查找),填写true(失活也查找)
otherTest childrenOT = this.GetComponentInChildren<otherTest>();
print("children Component :" + childrenOT);

// 父对象挂载的脚本(自身的挂载也会去查找)
otherTest ParentOT = this.GetComponentInParent<otherTest>();
print("parent Component :" + ParentOT);

// 尝试获取,成功返回true,失败返回false
otherTest tryOT;
if (this.TryGetComponent<otherTest>(out tryOT))
{
print("获取成功");
}

GameObject

一些属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 一些属性:
// 脚本的激活
this.enabled = true;

// gameobj对象的激活
bool isActive = this.gameObject.activeSelf;

// 获取是否静态
bool isStatic = this.gameObject.isStatic;

// 获取标签
string tag = this.gameObject.tag;

// 获取层级
int layer = this.gameObject.layer;

静态方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 静态方法:
// 创建几何体
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);

// 查找对象
// 只能找激活对象,存在多个同名随机找出一个
GameObject.Find("otherTest"); // 对象名查找
GameObject.FindGameObjectWithTag("Player"); // 标签名查找
// 找多个
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");

// 实例化对象(空对象)
GameObject instObj = GameObject.Instantiate(testOBJ);

// 删除对象
GameObject.Destroy(instObj); // 删除实例化对象
GameObject.Destroy(instObj,5); // 延迟删除
GameObject.Destroy(this); // 删除脚本,这里是删除自身这个脚本
GameObject.DontDestroyOnLoad(hsgmOjc) /过景移对,里不除载本象
成员方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 成员方法:
// 创建空对象
GameObject gameObject = new GameObject();
GameObject gameObject2 = new GameObject("Test"); // 初始化对象名
GameObject gameObject3 = new GameObject("TestAddComponent",typeof(otherTest)); // 初始化对象名,并且添加脚本(变长

// 为对象添加脚本
otherTest ot = this.AddComponent<otherTest>();

// 标签比较
bool isTag = this.CompareTag("Player");

// gameobj的激活失活
bool isActive = this.gameObject.activeSelf; // 获得激活还是失活状态
this.gameObject.SetActive(true); // 设置激活还是失活

时间相关 API

时间缩放
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void Start()
{
// 时间缩放
Time.timeScale = 0; // 0倍 ( 暂停
Time.timeScale = 2; // 2倍

// 帧间隔时间
float deltaTime = Time.deltaTime; // 受scale影响
float unscaleDelatTime = Time.unscaledDeltaTime; // 不受scale影响

// 游戏开始到现在的时间
float time = Time.time; // 受scale影响
float unscaledTime = Time.unscaledTime; //不受scale影响

// 游戏开始到现在跑了多少帧
float frameCount = Time.frameCount;

}

private void FixedUpdate()
{
// 物理帧间隔时间
float fixedDeltaTime = Time.fixedDeltaTime; // 受scale影响
float fixedUnscaledDelateTime = Time.fixedUnscaledDeltaTime; // 受scale影响
}

Vector3

Unity 中的坐标系UE 不同,垂直屏幕向里面是 z轴

在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Vector3 可以表示点 或者 向量
Vector3 v1 = new Vector3(1, 2); // 缺省为0
Vector3 v2 = new Vector3(1, 5, 3);

// 零向量 单位向量
Vector3 zero = Vector3.zero; // 零向量
Vector3 right = Vector3.right; // 1 0 0
Vector3 left = Vector3.left; // -1 0 0
Vector3 forward = Vector3.forward; // 0 0 1 (面朝向)
Vector3 back = Vector3.back; // 0 0 -1
Vector3 up = Vector3.up; // 0 1 0
Vector3 down = Vector3.down; // 0 -1 0

// 两点距离
float distance = Vector3.Distance(v1, v2);

Position

在这里插入图片描述

1
2
3
4
5
6
7
// 位置 position
Vector3 postiontion = this.transform.position; // 是世界坐标系下的坐标
Vector3 localPosition = this.transform.localPosition; // 本地坐标,相对于父对象的坐标,与面板显示一致
this.transform.position = new Vector3(1, 2, 3); // 不能单独改变position的 x,y,z

// 本地向量
Vector3 myForward = this.transform.forward; // 面朝向

位移

1
2
3
4
5
6
7
8
void Update()
{
// 位移
// 三个等价:朝着自己的面朝向前进
this.transform.position += this.transform.forward * Time.deltaTime;
this.transform.Translate(Vector3.forward * Time.deltaTime); // 参考本地坐标系
this.transform.Translate(this.transform.forward * Time.deltaTime, Space.World); // 参考世界坐标系
}

EulerAngles

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Update()
{
// 旋转
Vector3 eulerAngles = this.transform.eulerAngles; // 世界坐标系
Vector3 localEulerAngles = this.transform.localEulerAngles; // 本地坐标系,面板参数

// 旋转API
// 自转
this.transform.Rotate(Vector3.up * Time.deltaTime * 10);
this.transform.Rotate(Vector3.up * Time.deltaTime * 10, Space.World); // 相对于世界坐标轴

// 相对于点旋转
this.transform.RotateAround(Vector3.zero, Vector3.up, 10 * Time.deltaTime);
}

缩放

一般改localScale,相对于父对象的缩放,即相对缩放

1
2
3
 // 缩放
Vector3 lossyScale = this.transform.lossyScale; // 世界坐标系
Vector3 localScale = this.transform.localScale; // 本地坐标系
看向
1
2
3
4
5
6
7
8
public Transform lookObject;
void Start()
{
//看向一个点
this.transform.LookAt(Vector3.zero);
//看向一个对象
this.transform.LookAt(lookObject);
}

父子关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public Transform son;
void Start()
{
// 父对象
print(this.transform.parent); // 获取
this.transform.parent = null; // 取消父对象
this.transform.parent = GameObject.Find("Cube").transform; // 设置父对象
this.transform.SetParent(null); // API的方法修改父对象
this.transform.SetParent(GameObject.Find("Cube").transform, true); // 保留面板上的参数
this.transform.SetParent(GameObject.Find("Cube").transform, true); // 保持原本的世界坐标系状态,面板参数根据父对象修改

// 子对象
this.transform.DetachChildren(); // 取消所有子对象
this.transform.Find("Cube"); // 根据名字查找子对象,失活也可以查找,区别Gameobject

for(int i = 0; i < this.transform.childCount; i++)
{
print(this.transform.GetChild(i)); // 索引号获得子对象
}

son.IsChildOf(this.transform); // 是子对象返回true 否则false
son.GetSiblingIndex(); // 子对象编号
son.SetAsFirstSibling(); // 设置为第一个子对象
son.SetAsLastSibling(); // 设置为最后一个子对象
son.SetSiblingIndex(2); // 自定义编号

}

坐标转换

世界坐标系 转 本地坐标系

在这里插入图片描述 在这里插入图片描述

1
2
3
4
5
// 世界坐标系的点 转化为 本地坐标系的点
print("(0,0,1)点相对于本地坐标系的位置:" + this.transform.InverseTransformPoint(Vector3.forward)); // 受到自身缩放影响

// 世界坐标系的方向 转化为 本地坐标系的方向
print("(0,0,1)方向相对于本地坐标系的方向:" + this.transform.InverseTransformVector(Vector3.forward)); // 不受自身缩放影响
本地坐标系 转 世界坐标系

在这里插入图片描述
在这里插入图片描述

1
2
3
4
5
// 本地坐标系的点 转化为 世界坐标系的点
print(this.transform.TransformPoint(Vector3.forward)); // 受自身缩放影响
// 本地坐标系的方向 转化为 世界坐标系的方向
print(this.transform.TransformDirection(Vector3.forward)); // 不受自身缩放影响
print(this.transform.TransformVector(Vector3.forward)); // 受自身缩放影响

Input

鼠标相关
1
2
3
4
5
6
7
8
9
10
11
12
13
// 鼠标位置
Vector3 mousePosition = Input.mousePosition; // 0,0,0 原点在左下角

// 鼠标输入 : 0左键 1中键 2右键
// 按下瞬间检测
bool isButtonDown = Input.GetMouseButtonDown(0);
// 抬起瞬间检测
bool isButtonUp = Input.GetMouseButtonUp(0);
// 长按,按下,抬起都会检测
bool isButton = Input.GetMouseButton(0);

// 中键滚动 : Y = -1 下滚, Y = 0 未滚, Y = 1 上滚, X始终0
Vector2 mouseScrollDelta = Input.mouseScrollDelta;
键盘相关
1
2
3
4
// 键盘输入
bool isKeyDown = Input.GetKeyDown(KeyCode.W); // 按下
bool isKey = Input.GetKey(KeyCode.W); // 长按
bool isKeyUp = Input.GetKeyUp(KeyCode.W); // 抬起
默认轴输入
1
2
3
4
5
// 默认轴输入 : 取值范围[-1,1]
float axisHorizontal = Input.GetAxis("Horizontal");
float axisVertical = Input.GetAxis("Vertical");
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");

另外一种:GetAxisRaw 方法取值有 -1 0 1 没有浮点过度

在这里插入图片描述

其他
1
2
3
4
5
6
7
8
9
// 是否存在任意鼠标或键盘 按下
bool anKeyDown = Input.anyKeyDown;
// 是否存在任意鼠标或键盘 长按
bool anyKey = Input.anyKey;
// 这一帧的键盘输入
if (Input.anyKeyDown)
{
string inputString = Input.inputString;
}

Camera

参数

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

成员
1
2
3
4
5
6
7
8
9
10
11
// 静态成员
// 获取主摄像机
Camera camera = Camera.main; // 标签Tag必须是MainCamera,一般只设置一个主摄像机
// 获取所有摄像机
Camera[] allCameras = Camera.allCameras;

// 成员方法
// 世界坐标 转 屏幕坐标
Vector3 screenPoint = Camera.main.WorldToScreenPoint(this.transform.position);
// 屏幕坐标 转 世界坐标
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);

世界坐标 转 屏幕坐标 Vector3:x , y 表示屏幕坐标系,z 表示相对于摄像机的纵深距离

在这里插入图片描述

屏幕坐标 转 世界坐标 Vector3: z 表示纵深距离,类似横切面

在这里插入图片描述

碰撞检测

Rigidbody 刚体

两个物体相互碰撞必要条件:1.都有碰撞盒子 2.至少一个拥有刚体

刚体类似于模拟物理

在这里插入图片描述

详细:

物理帧更新直接影响刚体插值运算

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
在这里插入图片描述

碰撞检测模式,具体含义不重要,了解性能消耗大小即可

在这里插入图片描述

约束轴移动或者轴旋转

在这里插入图片描述

Collider

在这里插入图片描述

物理材质

在这里插入图片描述

常用函数

特殊的生命周期

在这里插入图片描述

物理碰撞检测
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 碰撞开始时调用
private void OnCollisionEnter(Collision other) {
// 碰撞对象
GameObject gameObject = other.gameObject;
// 碰撞对象的碰撞器
Collider collider = other.collider;
// 碰撞对象的位置
Transform transform = other.transform;
}

// 重叠过程中调用,需要产生摩擦
private void OnCollisionStay(Collision other) {

}

// 碰撞结束时调用
private void OnCollisionExit(Collision other) {

}
触发器检测

在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 触发开始时调用
private void OnTriggerEnter(Collider other) {

}

// 触发重叠时调用
private void OnTriggerStay(Collider other) {

}

// 触发结束时调用
private void OnTriggerExit(Collider other) {

}
其他注意

在这里插入图片描述

刚体加力

方法

获取刚体组件

1
Rigidbody rigidBody = this.gameObject.GetComponent<Rigidbody>();
添加水平力
1
2
3
4
// 1.相对世界坐标
rigidBody.AddForce(Vector3.forward); // 朝世界坐标 0 0 1 方向施加力
// 2.相对本地坐标
rigidBody.AddRelativeForce(Vector3.forward); // 朝面朝向施加力
添加扭矩力
1
2
3
4
// 1.相对世界坐标
rigidBody.AddTorque(Vector3.up);
// 2.相对本地坐标
rigidBody.AddRelativeTorque(Vector3.up);
改变速度
1
rigidBody.velocity = Vector3.forward; // 相对世界坐标
给世界坐标系的点加力
1
2
3
4
float forceValue = 100;
float radius = 10;
Vector3 point = Vector3.zero;
rigidBody.AddExplosionForce(forceValue,point,radius); // 只对rigibody调用的刚体起作用
力的模式

在这里插入图片描述

自带的力相关脚本

在这里插入图片描述

刚体休眠

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

1
2
3
4
5
6
// 获取刚体是否休眠
if (rigidBody.IsSleeping())
{
rigidBody.WakeUp(); // 唤醒
}
```

Unity 入门
https://clearacg.com/2024/02/21/forUnity/UnityBeginner/
作者
Aki_CCLing
发布于
2024年2月21日
许可协议