# unity 中基本类的使用
- 向量的基本使用
1 | // Start is called before the first frame update |
欧拉角与四元数
row 1
2
3
4
5
6
7
8
9
10// 旋转,欧拉角,四元数
Vector3 rotate = new Vector3(0,30,0);
Quaternion quaternion = Quaternion.identity;
//欧拉角转为四元数
quaternion = Quaternion.Euler(rotate);
//四元数转为欧拉角
rotate = quaternion.eulerAngles;
//看向一个物体
quaternion = Quaternion.LookRotation(new Vector3(0,0,0));物体类
row 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test2 : MonoBehaviour
{
public GameObject body;
// Start is called before the first frame update
// 获取预设体对象
public GameObject Prefab;
void Start()
{
// 拿到当前脚本所挂载的游戏物体
// GameObject go = this.gameObject;
// 名称
Debug.Log(gameObject.name);
// tag
Debug.Log(gameObject.tag);
// layer
Debug.Log(gameObject.layer);
// 立方体的名称
Debug.Log(body.name);
// 当前真正的激活状态 即可能会因为父级所以没激活
Debug.Log(body.activeInHierarchy);
// 当前自身激活状态
Debug.Log(body.activeSelf);
// 获取Transform组件
// Transform trans = this.transform;
Debug.Log(transform.position);
// 获取其他组件
BoxCollider bc = GetComponent<BoxCollider>();
// 获取当前物体的子物体身上的某个组件
// GetComponentInChildren<CapsuleCollider>();
// 获取当前物体的父物体身上的某个组件
// GetComponentInParent<BoxCollider>();
// 添加一个组件
gameObject.AddComponent<BoxCollider>();
//BoxCollider bc = GetComponent<BoxCollider>();
// 获取当前物体的子物体身上的某个组件
// GetComponentInChildren<CapsuleCollider>(bc);
// 获取当前物体的父物体身上的某个组件
// GetComponentInParent<BoxCollider>();
// 添加一个组件
body.AddComponent<AudioSource>();
// 通过游戏物体的名称来获取游戏物体
GameObject test = GameObject.Find("Test");
// 通过游戏物体标签来获取游戏物体
test = GameObject.FindWithTag("Player");
test.SetActive(false);
Debug.Log(test.name);
// 通过预设来实例化一个游戏物体
Instantiate(Prefab, transform);
}
// Update is called once per frame
void Update()
{
}
}时间类
row 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24public class Time_test : MonoBehaviour
{
float timer = 0f;
// Start is called before the first frame update
void Start()
{
Debug.Log(Time.time);
// 时间缩放值
Debug.Log(Time.timeScale);
//固定时间间隔
Debug.Log(Time.fixedDeltaTime);
}
// Update is called once per frame
void Update()
{
timer += Time.deltaTime; // 每帧更新计时器
if (timer > 10)
{
Debug.Log("sb");
}
}
}application
row 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15void Start()
{
// 输出当前路径
Debug.Log(Application.dataPath);
// 持久化路径
Debug.Log(Application.persistentDataPath);
// StreamingAsserts 文件只读路径 配置文件
Debug.Log(Application.streamingAssetsPath);
//临时文件夹
Debug.Log(Application.temporaryCachePath);
//是否在后台
Debug.Log(Application.runInBackground);
//打开url
Application.OpenURL("https://canvas.sydney.edu.au/courses/59287/modules");
}场景切换
row 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
45
46
47
48
49
50
51void Start()
{
// 场景跳转
SceneManager.LoadScene(1);
// 获取当前场景
Scene scene = SceneManager.GetActiveScene();
// 场景名称
Debug.Log(scene.name);
// 是否已加载
Debug.Log(scene.isLoaded);
//路径
Debug.Log(scene.path);
//索引
Debug.Log(scene.buildIndex);
GameObject[] gmo = scene.GetRootGameObjects();
Debug.Log(gmo.Length);
//场景管理类
SceneManager.CreateScene("newScene");
//已加载场景个数
Debug.Log(SceneManager.sceneCount);
//卸载
SceneManager.UnloadSceneAsync("newScene");
//加载场景 但是如果场景大的话容易卡顿
SceneManager.LoadScene("SampleScene",LoadSceneMode.Additive);
}异步加载
row 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
41public class AsyncTest : MonoBehaviour
{
AsyncOperation operation;
// Start is called before the first frame update
void Start()
{
}
// 协程方法用来加载异步场景
IEnumerator loadScene(){
operation = SceneManager.LoadSceneAsync(1);
yield return operation;
}
// Update is called once per frame
void Update()
{
Debug.Log(operation.progress);
}
}transform
row 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
45
46void Start()
{
//位置 && 角度
Debug.Log(transform.position);
Debug.Log(transform.rotation);
Debug.Log(transform.localPosition);
Debug.Log(transform.localRotation);
//欧拉角
Debug.Log(transform.localEulerAngles);
Debug.Log(transform.eulerAngles);
//缩放
Debug.Log(transform.localScale);
//向量
Debug.Log(transform.forward);
Debug.Log(transform.right);
Debug.Log(transform.up);
//父子关系
//获取父物体
Debug.Log(transform.parent.gameObject);
//子物体个数
Debug.Log(transform.childCount);
//解除父子关系
transform.DetachChildren();
//获取子物体
Transform tra = transform.Find("test");
tra = transform.GetChild(0);
bool res = tra.IsChildOf(transform);
Debug.Log(res);
//设置父物体
tra.SetParent(transform);
}
// Update is called once per frame
void Update()
{
//看向某个点
transform.LookAt(Vector3.zero);
//旋转
transform.Rotate(Vector3.up, 1);
//绕某个物体旋转
transform.RotateAround(Vector3.zero, Vector3.up, 5);
//移动
transform.Translate(Vector3.forward * 0.1f);
}鼠标键盘
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//鼠标
if (Input.GetMouseButtonDown(0))
{
Debug.Log("按下了左键");
}
else if (Input.GetMouseButtonDown(1))
{
Debug.Log("右键");
}
//持续按下
if (Input.GetMouseButton(0))
{
Debug.Log("持续");
}
//抬起鼠标
if (Input.GetMouseButtonUp(0))
{
Debug.Log("抬起");
}
//键盘按下
if(Input.GetKeyDown(KeyCode.A))
{
Debug.Log("A");
}
//持续
if (Input.GetKey(KeyCode.S)) {
Debug.Log("S");
}
//抬起
if (Input.GetKeyUp(KeyCode.D)) {
Debug.Log("d抬起");
}