Unity常用基础知识

!屏幕坐标系:

当前屏幕宽度:Screen.width(像素)

当前屏幕高度:Screen.height(像素)

物体在屏幕中的位置:Vector3 pos=Camera.main.WorldToScreenPoint(物体position)(摄像机视角)

Vector3 pos=Camera.main.WorldToScreenPoint(transform.position)世界坐标转屏幕坐标

pos.x>Screen.width(物体超过右边的屏幕宽度),pos.x<Screen.width(物体超过左边的屏幕宽度)

!旋转:

数值为负顺时针旋转,为正逆时针旋转

!脚本执行顺序

Awake>OnEnable>Start>FixUpdate>Update>LateUpdate>OnDrawGizmos>OnDisable>OnDestroy>OnApplicationQuit

组件被禁用时:Awake会执行且只执行一次,其他都不执行。

未禁用时:Awake和Start只执行一次(反复开启和禁用),OnEnable启用一次执行一次

多个组件都含有:Awake、Start、Update....时执行顺序未“无序”,默认顺序未0(Default Time)

执行顺序排序设定:Project Setting>Script Execution Order(或脚本窗口中),数值越小则优先(如-1)

!Invoke()是Unity中内置的一个回调机制。

1)、Invoke() : 不能接受有参数的方法。

2)、Invoke():应该在脚本的生命周期里面(Start,Updata,OnGUI,LateUpdate)中被调用;

3)、Invoke() 会受到Time.ScaleTime的影响。

Invoke(string methodName,float time)等待time后调用methodName

Invoke(string methodName,float time,float delayTime) 等待time之后,再调用方法methodName方法,并且每隔delayTime再去调用methodName方法

InvokeRepeating:会一直执行,这时到达了条件后你就会想要停止掉这个方法。

CancelInvoke()  :  停止当前脚本中所有的Invoke和InvokeRepeating方法。

CancelInvoke("MethodName") : 停止当前脚本某个Invoke和InvokeRepeating方法。

Invoke和协程的比较:

Invoke方法: 执行并没有被挂起,相当于设置完被调用函数的执行时间后即时向下执行。应用到每隔一段时间执行某个函数很方便。

协程:开启一个新的序列(不是线程)并挂起,等待中断指令结束。当需要挂起当前执行时使用。

猜你喜欢

转载自blog.csdn.net/Kevin_meta/article/details/126926317
今日推荐