Unity 笔记 ---记录用
System.Reflection.MethodBase.GetCurrentMethod().Name ;
使用反射系统获得当前的函数名字
Debug.Log("");
GetComponment<Type>();
this.gameObject.AddComponment<type>();
生命周期
Awake()->OnEnable()->Start()->FixedUpdate()
->Update()->LateUpdate()->OnGUI()->OnDisable()->OnDestroy()
OnMouse___(Down Up Drag Enter Over Exit) 鼠标事件
OnCollision___(Enter , Stay ,Exit) 物理碰撞事件
OnTrigger____(Enter,stay,Exit) 触发事件
Quaterion quaternion = Quaterion.identity ; // (0,0,0)
//欧拉角转四元数
quaternion = Quaterion.Euler(rotation);
// 看向一个物体
quaternion = Quaterion.lookRotation(new vector(0,0,0));
//四元数转欧拉角
rotation = quaternion.eulerAngles;
//调试
Debug.Log("");
Debug.Warning("");
Debug.LogError("");
Debug.DrawLine(startpoint,endpoint,LineColor);
Debug.DrawRay(startpoint,ray,LineColor);
启用和禁用组件:
.enabled = true / false ;
启用和禁用游戏对象
gameObject.SetActive(false/true)
gameObject
name
tag
layer
//当前真正的激活状态
activeInHierarchy
//当前自身的激活状态
activeSelf
transform
//获取其他组件
GetComponment<组件类型>();
//获取当前子物体身上的某个组件
GetComponmentInChildren<>();
//获取父物体身上的某个组件
GetComponmentInParent<>();
// 添加组件
gameObject.addComponment<>();
//获取游戏物体
名字
--- GameObject.Find("") // 全局搜索
transform.Find("")//
标签
--- GameObject.FindWithTag("");
SetActive
Instantiate()// 实例化游戏物体
Destroy(); // 销毁游戏物体
Time
Time.time ; // 从开始到现在花的时间
Time.Scale ; // 时间缩放
Time.fixedDeltaTime 0.02
Time.deltaTime ;
Application
只读的,加密压缩
Application.dataPath // assets
持久化路径 可写的
Application.persistentDataPath
只读的 , 不会加密压缩 -- 配置文件
Application.streamingAssetsPath
//临时文件夹
Application.temporaryCachePath
//控制是否在后台运行
Application.runInBackground
// 打开url
Application.OpenURL("");
Application.Quit();
游戏由场景组成,场景由游戏物体组成。
游戏物体由 不同的脚本组件组成
加载场景
//可以通过索引和场景名称
sceneManager.LoadScene()
//获取当前场景
SceneManager.GetActiveScene();
//场景是否已经加载
scene.isLoaded
//场景路径
scene.papth
//场景索引号
scene.buildIndex
//获得场景内所有的游戏物体
GameObject[] _goArr = scene.GetRootGameObjects();
活动场景的个数
SceneManager.sceneCount();
//创建新场景
sceneManager.CreateScene();
//卸载场景
sceneManager.UnloadSceneAsync();
//替换场景
sceneManager.LoadScene("MyScene",LoadSceneMode.Single)
//添加场景
sceneManager.LoadScene("MyScene",LoadSceneMode.Additive)
异步加载
多线程和协程
//协程方法来异步加载场景
-- 异步加载函数
IEnumerator loadScene(){
AsyncOperation operation =
SceneManager.LoadSceneAsync(1);
// 加载完场景不要自动跳转
operation.allowSceneActivation = false ;
yield return operation ;
}
void start(){
StartCoroutine(loadScene()); //调用异步加载函数
}
operation.progress 0 - 0.9 场景加载进度
transform
维持父子关系
data :
transform.postion -- 世界位置
transform.localpostion -- 相对位置
[] = transform
[].ratation
[].localRotation
[].eulerAngles
[].localEulerAngles
[].localScale
forward
right
up
func():
LookAt() // 看向
Rotate() // 自己绕某轴旋转
RotateAround(中心点,旋转轴,速度)
Translate(); // 移动
获取父物体
transform.parent.gameObject
获取子物体
transform.childCount
//接触与子物体的父子关系
transform.DetachChildren();
// 获取子物体
transform.Find("child")
获取第几个子物体
transform.getChild(0)
trans.isChildOf(transform); // trans 是否是 子物体
设置为父物体
trans.SetParent(transform);
延时调用
Invoke("function_name",time)
只有 没有参数 并且返回值是void的函数可以被Invoke函数调用 、
InvokeRepeating()
CancelInvoke(); 停止Invoke 函数
键鼠事件
鼠标点击 0 左键 1 右键 2 滚轮
Input.GetMouseButtonDown(0) 按下的瞬间 调用一次
持续按下鼠标
GetMouseButton(0)
抬起鼠标
GetMouseButtonUp(0)
按下键盘按键
GetKeyDown(KeyCode.A)
持续按下键盘按键
GetKey()
抬起键盘按键
GetKeyUp() // 使用字符要使用小写 ,最好用 枚举KeyCode.
虚拟轴
Input.GetAxis("") // 轴的名称 --- 获取轴
//虚拟按键
Input.GetButtonDown("Jump")
GetButton()
GetButtonUp()
触摸操作 --- 针对手游
开启多点触摸
Input.multiTouchEnable = true ;
Input.touchCount 触摸了几点
Touch touch = Input.touches[0] ; 触摸对象
touch.postion 触摸位置
判断单点触摸
switch(touch.phase) 触摸阶段
case TouchPhase.Began 开始
case moved 移动
case stationary 静止
case Ended 结束
caseq Canceled 触摸被打断 /取消
判断多点触摸
Touch touch = Input.touches[0] ; 触摸对象
Touch touch = Input.touches[1] ; 触摸对象
几点触摸就是几个Touch对象
音乐相关 Auido
背景音乐
AudioClip 音乐片段
Clip
Volume 音量
loop 循环
Play() 播放音乐 从头开始播放
IsPlaying 是否正在播放
Pause() 停止播放
UnPause() 继续播放
音效
PlayOneShot ()
视频 video Player
isPlaying()
角色控制器 完整的解决方案 、unity 自带的 、
通过物理系统自己做
GetComponment<CharacterController>()
Move() 不受重力影响
SimpleMove() 受重力影响
使用物理系统做角色控制
物理系统做角色控制
Rigidbody 物理 刚体
Colider 碰撞
要碰撞 必须俩个物体都有 Colider , 其中的一个物体有Rigidbody
进一步解释 :发生碰撞的物体,有rigidbody的物体才有物理效果 ,没有rigidbody的物体坚不可摧
不会被推动,也没有物理效果
而且 rigidbody 和 charactercontrol不能一起添加
charactercontrol 不具备物理效果(重力) ,但是会被 具有碰撞器的物体阻挡
//监听发生碰撞
private void OnCollisionEnter(Collision collision)
//持续碰撞
private void OnCollisionStay(Collision collision)
//结束碰撞
private void OnCollisionExit(Collision collision)
collision.collider
脚本 ---- 谁的事情谁去做 自己控制自己的事情 交互 的双方 固定一方去做
触发器 不具备物理效果
private void OnTriggerEnter(Colider other)
private void OnTriggerStay(Colider other)
private void OnCollisionExit(Colider other)
物理关节
Hinge Joint 铰链关节
spring Joint 弹簧关节
Fixed Joint 固定关节
物理材质
实际上和材质屁关系没有,翻译一坨屎
就是高级物理模拟参数 静态动态摩擦力 , 弹力 等等
基础的rigidbody 中的阻力是空气阻力
射线检测
Ray ray = new (start,dirction)
ray = Camera.main.ScreenPointToRay() ;
RaycastHit hit ; 碰撞结果
Physics.Raycast(ray,out hit) hit 类似于一个引用
RaycastHit[] hits = Physics.RaycastAll(ray,distance,layer) layer 1<<10 第10 图层的
特效部分
粒子系统:
没啥记得,跳过
线段渲染器
LineRender LineRender = GetComponment<LineRender>()
LineRender.postionCount = 3 ; // 有几个点
LineRender.setPosition()
LineRender.SetPositions()
LineRender.StartColor Startwidth endColor endWidth
拖尾
动画系统
Animation ctrl+6 动画界面
GetComponment<Animation>().Play() 默认 play("")
Animater
动画过度 就是ue的切换规则
通过脚本的方式触发 动画状态机中的Trigger
GetComponment<Animater>().SetTrigger("name")
unity InputSystem unity 输入系统
Cinemachine 高级相机移动