加载中...
avatar

Unity基础代码合集

# Unity 学习档案

# 先就列个代码,之后二刷会补上一些细节上的说明

# Chapter_1、基本模板

(生成就有的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}
}

# Chapter_2、脚本的生命周期

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
void Awake()
{
Debug.Log("awake");
}

void OnEnable()//激活组件时就会调用,可能会调用多次(只要重新激活就会调用)
{
Debug.Log("OnEnable");
}

// Start is called before the first frame update
void Start()//唯一调用,调用时间在第一次OnEnable之后
{
Debug.Log("start");
}

// Update is called once per frame
void Update()//每一帧输出一次,不要轻易使用
{

}
void LateUpdate()//每次Update执行完都会执行一遍
{

}
void FixedUpdate()//每隔固定一段时间调用一次,与帧无关,和性能无关,在编辑中的项目设定中的时间进行修改
{

}
void OnDisable()//非激活时调用一次
{
Debug.Log("OnDisable");
}
void OnDestroy()//销毁的时候会调用,一般只会在最后的时候调用一下
{
Debug.Log("Ondestroy");
}
}

# Chapter_3、Vector3 的使用(向量)

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VectorTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//向量,坐标,旋转,缩放
Vector3 v = new Vector3(1,1,0.5f);//也可以用(45,90,0)--旋转
v = Vector3.zero;//通过静态属性快速得到一个结构体,其他有Vector3.one、Vector3.up、Vector3.left等
v = Vector3.right;
//v.x = 0;
//v.y = 1;
//v.z = 2;
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.Lerp(Vector3.zero, Vector3.one, 0.8f));//这两个向量之间做一个比例计算
//向量的模
Debug.Log(v.magnitude);
//规范化的向量
Debug.Log(v.normalized);

}

// Update is called once per frame
void Update()
{

}
}

# Chapter_4、方向描述,欧拉角和四元数

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//旋转:欧拉角,四元数(效率高)
Vector3 rotate=new Vector3(0,30,0);
Quaternion quaternion=Quaternion.identity;//无旋转,亦可以采用Quaternion quaternion=new Quaternion(x,y,z,w);
quaternion=Quaternion.Euler(rotate);//通过欧拉角创建四元数
rotate=quaternion.eulerAngles;//通过四元数转化为欧拉角
//看向一个物体,看向(0,0,0)的位置
quaternion = Quaternion.LookRotation(new Vector3(0, 0, 0));
}

// Update is called once per frame
void Update()
{

}
}

# Chapter_5、Debug 相关的辅助检查手段 & 线的绘制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DebugTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("test");
Debug.LogWarning("test2");
Debug.LogError("test3");
}

// Update is called once per frame
void Update()
{
//绘制一条线,起点,终点;只有调试时能看见,在游戏中是看不见的
Debug.DrawLine(new Vector3(1, 0, 0), new Vector3(1, 1, 0), Color.blue);//颜色设置为蓝色
//绘制一条射线 起点,射线
Debug.DrawRay(new Vector3(1, 0, 0), new Vector3(1, 1, 0), Color.red);//前面的两个参数可以用Vector3.zero、Vector3.up等
}
}

# Chapter_6、动态修改物体属性

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EmptyTest : MonoBehaviour
{
public GameObject Cube;

//获取预设体
public GameObject Prefab;

// Start is called before the first frame update
void Start()
{
//拿到当前脚本所挂载的游戏物体
//GameObject go = this.gameObject;//this是可以省略的
Debug.Log(gameObject.name);//名称
Debug.Log(gameObject.tag);//标签
Debug.Log(gameObject.layer);//图层
//立方体的名称
Debug.Log(Cube.name);
//立方体是否被激活
Debug.Log(Cube.activeInHierarchy);//真正的激活状态
Debug.Log(Cube.activeSelf);//当前自身是否激活挑勾
//获取Transform组件
//Transform trans = this.transform;
Debug.Log(transform.position);
//获取其他组件
BoxCollider bc = GetComponent<BoxCollider>();
//获取当前物体的子物体身上的某个组件
GetComponentInChildren<CapsuleCollider>(bc);
//获取当前物体的父物体身上的某个组件
GetComponentInParent<BoxCollider>();
//添加一个组件
Cube.AddComponent<AudioSource>();
//通过游戏物体的名称来获取游戏物体
test=GameObject.FindWithTag("Enemy");
//GameObject test = GameObject.Find("Test");
test.SetActive(false);
Debug.Log(test.name);

//通过预设体来实例化一个游戏物体
GameObject go = Instantiate(Prefab,transform);//第二维还可以是Vector3.zero,Quaternion.identity
//销毁
Destroy(go);
}

// Update is called once per frame
void Update()
{

}
}

# Chapter_7、游戏时间的使用

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TimeTest : MonoBehaviour
{
float timer = 0;
// 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;//计时器,每帧时间加在一起
//上一帧到这一帧所用的时间(60帧 1/60s 120帧 1/120s)
Debug.Log(Time.deltaTime);
//如果大于10s
if (timer > 3)
{
Debug.Log("大于3秒了");
}
}

private void FixedUpdate()
{

}
}

# Chapter_8、路径权限相关

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ApplicationTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//游戏数据文件夹路径(只读,加密压缩)
Debug.Log(Application.dataPath + "/新建文本文档.txt");
//持久化文件夹路径,可以写
Debug.Log(Application.persistentDataPath);
//StreamingAssets文件夹路径(只读,配置文件,不会加密压缩)
Debug.Log(Application.streamingAssetsPath);
//临时文件夹
Debug.Log(Application.temporaryCachePath);
//控制是否在后台运行
Debug.Log(Application.runInBackground);
//打开url
Application.OpenURL("https://www.bilibili.com/");
//退出游戏
Application.Quit();
}

// Update is called once per frame
void Update()
{

}
}

# Chapter_9、场景类相关

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneTest : MonoBehaviour
{
// Start is called before the first frame update
void 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[] gos = scene.GetRootGameObjects();
Debug.Log(gos.Length);

//场景管理类
//已加载场景个数
Debug.Log(SceneManager.sceneCount);
//创建新场景
Scene newScene = SceneManager.CreateScene("newScene");
//卸载场景
SceneManager.UnloadSceneAsync(newScene);
//加载场景用法拓展
SceneManager.LoadScene("MyScene",LoadSceneMode.Additive);//场景叠加在一起了,除了Additive还有Single

}

// Update is called once per frame
void Update()
{

}
}

# Chapter_10、异步场景加载相关

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AsyncTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
StartCoroutine(loadScene());
}
//协程方法用来异步加载场景
IEnumerator loadScene(){
operation = SceneManager.LoadSceneAsync(1);
//加载完场景不要自动跳转
operation.allowSceneActivation = false;
yield return operation;
}
float timer = 0;
// Update is called once per frame
void Update()
{
//输出加载进度 0-0.9
Debug.Log(operation.progress);
timer += Time.deltaTime;
//如果到达5s,再跳转
if(timer > 5){
operation.allowSceneActivation = true;
}
}
}

# Chapter_11、了解 transform

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransformTest : MonoBehaviour//transform主要的两套用法有:父子关系&旋转相关的操作
{
// Start is called before the first frame update
void Start()
{
/*以下为常用的数据
//获取位置
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);
*/

//父子关系
//获取父物体
//transform.parent.gameObject
//子物体个数
//Debug.Log(transform.childCount);
//接触与子物体的父子关系
//transform.DetachChildren();
//获取子物体
Transform trans = transform.Find("Chuld");//返回值是transform型
trans = transform.GetChild(0);//获取第几个孩子,例中为第0个
//判断一个物体是不是另外一个物体的子物体
bool res = trans.IsChildOf(transform);
Debug.Log(res);
//设置为父物体
trans.SetParent(transform);//此时设置自身为自身的父物体

}

// Update is called once per frame
void Update()
{
//以下为常用的方法
//时时刻刻看向某点(例中为0,0,0)
//tranform.LookAt(Vector3.zero);
//旋转方法
//transform.Rotate(Vector.up, 1);//让绕着up这个轴每秒转动1帧
//绕某个物体旋转
//transform.RotateAround(Vector3.zero, Vector3.up, 5);//绕000点的up轴进行旋转,每帧转动5度
//移动
//transform.Translate(Vector3.forward * 0.1f);//每一帧朝forward方向移动0.1


}
}

# Chapter_12、键鼠操作

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KeyTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
//鼠标操作是时时刻刻都可能有的,所以写在update中,每帧都判断一下
//按下鼠标0左键 1右键 2滚轮
if(Input.GetMouseButtonDown(0))
{
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.A))
{
Debug.Log("持续按下了A");
}
//抬起键盘按键
if(Input.GetKeyUp(KeyCode.A))//不可以用"A",但是可以用"a"
{
Debug.Log("松开了A");
}
}
}

# Chapter_13、虚拟轴的使用

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AxisTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{ /*
//获取水平轴
float horizontal = Input.GetAxis("Horizontal")//()中填写轴的名称
float vertical = Input.GetAxis("Vertical");
Debug.Log(horizontal + " " + vertical);
*/
//虚拟按键
if(Input.GetButtonDown("Jump"))
{
Debug.Log("空格");
}
if(Input.GetButton("Jump"))
{
Debug.Log("空格");
}
if(Input.GetButtonUp("Jump"))
{
Debug.Log("空格");
}
}
}

# Chapter_14、手机上的触摸操作

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//开启多点触摸
Input.multiTouchEnabled = true;
}

// Update is called once per frame
void Update()
{
////判断单点触摸
//if(Input.touchCount == 1)
//{
// //触摸对象
// Touch touch = Input.touches[0];//点击事件都存储在touch这个对象上
// //触摸位置
// Debug.Log(touch.position);
// //触摸阶段
// switch (touch.phase)//单点触摸
// {
// case touchPhase.Began:
// break;
// case touchPhase.Moved:
// break;
// case touchPhase.Stationary://静止时候做什么
// break;
// case touchPhase.Ended:
// break;
// case touchPhase.Canceled:
// break;
// }
//}

//判断多点触摸
if(Input.touchCount == 2)
{
Touch touch = Input.touches[0];
Touch touch1 = Input.touches[1];
}
}
}

# Chapter_15、音频控制相关的脚本

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioTest : MonoBehaviour
{
//AudioClip
public AudioClip music;//;音乐1
public AudioClip se;//音效1

//播放器组件
private AudioSource player;

// Start is called before the first frame update
void Start()
{
player = GetComponent<AudioSource>();//获取到播放器组件
player.clip = music;//设定播放的音频片段
//循环播放
player.loop = true;
//设置音量
player.volume = 0.5f;
//播放
player.Play();
}

// Update is called once per frame
void Update()
{
//按空格切换声音的播放和暂停
if(Input.GetKeyDown(KeyCode.Space))
{
//如果当前正在播放声音
if(player.isPlaying)
{
//暂停
player.Pause();
//player.Stop();停止播放,下次从头开始播放
}
else
{
//继续播放
player.UnPause();
//player.Play();
}
}

//按鼠标左键播放声音
if(Input.GetMouseButtonDown(0))
{
player.PlayOneShot(se);
}
}
}

# Chapter_16、视频控制相关的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;

public class VideoTest : MonoBehaviour
{
private VideoPlayer player;
// Start is called before the first frame update
void Start()
{
player = GetComponent<VideoPlayer>();
}

// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
player.Pause();//操作基本类似音频
}
}
}

# Chapter_17、角色控制基本移动相关的脚本

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour
{
private CharacterController player;
// Start is called before the first frame update
void Start()
{
player = GetComponent<CharacterController>();
}

// Update is called once per frame
void Update()
{
//水平轴
float horizontal = Input.GetAxis("Horizontal");
//垂直轴
float vertical = Input.GetAxis("Vertical");
//创建成一个方向向量
Vector3 dir = new Vector3(horizontal, 0, vertical);
//Debug.DrawRay(transform.position, dir, color.red);

//朝向该方向移动
player.SimpleMove(dir * 2);//不带Simple是不受重力影响的,*2是为了加速移动,当然整体可以改为transform.Translate(dir*2*Time.deltaTime);帧转秒用的方式也体现在此

}
}

# Chapter_18、发生碰撞的监听脚本

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FireTest : MonoBehaviour
{
//创建一个爆炸预设体
public GameObject Prefab;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}
//监听发生碰撞

private void OnCollisionEnter(Collision collision)//会获取到碰撞到的物品的信息
{
//创建一个爆炸物体
Instantiate(Prefab,transform.position,Quaternion.identity);//在当前位置,不旋转
//销毁自身
Destroy(gameObject);//gameObject代表当前物体
//获取碰撞到的物体
Debug.Log(collision.gameObject.name);
}

//持续碰撞中
private void OnCollisionStay(Collision collision)
{

}

//结束碰撞
private void OnCollisionExit(Collision collision)
{

}
}

# Chapter_19、触发器相关的脚本

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TriggerControl : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}
//注意碰撞传的参数是Collision类型,返回的是物体,要得到碰撞物体的碰撞器还要collision.collider
private void OnTriggerEnter(Collider other)//这里是直接给出进入触发的物体的触发器
{
//此处实现让一个叫做door的物体消失,最常用的游戏制作用法之一
GameObject door = GameObject.Find("Door");
if(door!=null)
{
door.SetActive(false);
}
}

private void OnTriggerStay(Collider other)
{

}

private void OnTriggerExit(Collider other)
{

}
}

# Chapter_20、射线检测的脚本

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RayTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//方式1
Ray ray = new Ray(Vector3.zero, Vector3.up);//以前一个点作为出发点,后一个点作为发射方向
}

// Update is called once per frame
void Update()
{
//方式2
if(Input.GetMouseButtonDown(0))//按下鼠标左键时候发出射线
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//声明一个碰撞信息类
RaycastHit hit;
//碰撞检测
bool res = Physics.Raycast(ray, out hit);
//如果碰撞到的情况下,hit就有内容了
if(res == true)
{
Debug.Log(hit.point);
transform.position = hit.point;//当前物体的位置移动到射线碰撞的位置
}
//多检测
//RaycastHit[] hits = Physics.RaycastAll(ray,100,1<<10);//检测射线碰到的所有物体,每碰到一个物体就hits就多上一个,100是检测距离,1<<10代表就检测第10图层,非第十图层不检测
}
}
}

# Chapter_21、画线段组件的脚本

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class lineTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//设定线段位置
//获取线段渲染器
LineRenderer lineRenderer = GetComponent<LineRenderer>();
lineRenderer.positionCount = 3;//设置线段需要多少个点
lineRenderer.SetPosition(0,Vector3.zero);//设置第0个点,位置为000,采用逐个设置的方式
lineRenderer.SetPosition(1,Vector3.one);
lineRenderer.SetPosition(2, Vector3.down);
//设置开始/结束时候的宽度和颜色
//lineRenderer.endwidth、endcolor、startwidth、startcolor

}

// Update is called once per frame
void Update()
{

}
}

# Chapter_22、旧动画组件的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimationTest : MonoBehaviour//旧版动画组件
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
GetComponent<Animation>().Play("Video");//默认没参数时会播放默认视频
}
}
}

# Chapter_23、新动画组件的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimatorTest : MonoBehaviour
{
private Animator animator;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();//获取动画器组件,只需要一次
}

// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
animator.Play("right");//播放相应的动画
}
}
}

# Chapter_24、人物活动 1 的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour//基本动画操作
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.F))//按下F键
{
//触发pickup参数
GetComponent<Animator>().SetTrigger("pickup");//触发叫做pickup的条件参数
}
}
}

# Chapter_25、人物活动 2 的脚本

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl1 : MonoBehaviour
{
//反向动力学(IK)演示对象
public Transform target;

private Animator animator;//设置动画组件成员,人真的跑起来了
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();//获取组件
}

// Update is called once per frame
void Update()
{
//水平轴
float horizontal = Input.GetAxis("Horizontal");
//垂直轴
float vertical = Input.GetAxis("Vertical");
//向量
Vector3 dir = new Vector3(horizontal, 0, vertical);
//当用户按下了方向键
if(dir != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(dir);//面向向量
//播放跑步动画
animator.SetBool("IsRun", true);
//朝向前方移动
transform.Translate(Vector3.forward * 2 * Time.deltaTime);//转成每秒移动
}
else
{
//播放站立动画
animator.SetBool("IsRun", false);
}

//随时获取Test参数并打印出来,打印出动画的曲线
Debug.Log(animator.GetFloat("Test"));

//打印事件
//void leftFoot()
//{
// Debug.Log("左脚");
//}//可以在落地时生成火花,同时可以播放脚步声
//void rightFoot()
//{
// Debug.Log("右脚");
//}


//IK实现写道下面这个方法内
private void OnAnimatorIK(int layerIndex)//控制哪一层IK
{
//设置头部IK
animator.SetLookAtWeight(1);//设置权重为1时,才会有效
animator.SetLookAtPosition(target.position);
//设置右手IK
animator.SetIKPositionWeight(AvatarlGoal.RightHand, 1);
//旋转权重
animator.SetIKRotationWeight(AvatarlGoal.LeftHand, 1);
//设置右手IK
animator.SetIKPosition(AvatarIKGoal.RightHand, target.position);
animator.SetIKRotation(AvatarIKGoal.RightHand, target.rotation);//手部设置一个旋转权重

}
}
}

# Chapter_26、导航活动相关的脚本

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PlayerControl2 : MonoBehaviour
{
private NavMeshAgent.agent;//代理组件
// Start is called before the first frame update
void Start()
{
//获取代理组件
agent = GetComponent<NavMeshAgent>();

}

// Update is called once per frame
void Update()
{
//如果按下鼠标
if(Input.GetMouseButtonDown(0))
{
//获取点击位置
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;//判断射线是否碰到物体
if(Physics.Raycast(ray, out hit))//射线检测,如果检测到射线返回true证明检测成功
{
//点击位置
Vector3 point = hit.point;//就是鼠标的点击位置
//设置该位置为导航目标点
agent.SetDestination(point);
}
}
}
}

# Chapter_27、选项框相关的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DropDownTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//获取下拉组件
Dropdown dropdown = GetComponent<Dropdown>();
//获取组件的选项
List<Dropdown.OptionData> options = dropdown.options;
//修改选项
options.Add(new Dropdown.Option("俄罗斯"));
dropdown.options = options;//加完后的选项赋值上去
}

// Update is called once per frame
void Update()
{

}
}

一刷结束了,撒花,接着看情况修改完善,接着可能会加上一些进阶的操作代码以供参考

文章作者: Ex-monster
文章链接: http://example.com/2023/04/11/Unity%E5%9F%BA%E7%A1%80%E4%BB%A3%E7%A0%81%E5%90%88%E9%9B%86/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 monster's blog
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论