Unity 小功能小方法系列

本文介绍了Unity3D中的一些高级技巧,包括帧率设置、系统休眠、延迟执行方法、协同程序的使用、子/父级关系管理、旋转角度限制等,帮助开发者更好地掌握Unity3D开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文转载自:点击打开链接

1.QualitySettings界面的VSync count属性选择Don't Sync。
2.代码:
  1. Application.targetFrameRate = 30;//手游30帧  

系统休眠

  1. Screen.sleepTimeout = (int)SleepTimeout.NeverSleep;//从不休眠  
  2. //(int)SleepTimeout.SystemSetting //按照系统设置。  

延迟执行某方法

  1. void Start () {    
  2.     //重复调用  
  3.     InvokeRepeating("LaunchProjectile", 1,5);//1秒后调用LaunchProjectile () 函数,之后每5秒调用一次    
  4.   
  5.     //调用一次  
  6.     Invoke("LaunchProjectile", 5);//5秒后调用LaunchProjectile () 函数  
  7.   
  8.     //取消调用  
  9.     CancelInvoke("LaunchProjectile");   
  10. }    
  11.   
  12. void LaunchProjectile () {    
  13.     print("hello");    
  14. }  
  15.       

协同程序

同步执行

  1. //运行结果:"Starting 0.0"和"Before WaitAndPrint Finishes 0.0"两句,2秒后打印"WaitAndPrint 2.0"  
  2. //WaitAndPrint在Start函数内执行,可以视同于它与Start函数同步执行.  
  3. void Start() {  
  4.     print("Starting " + Time.time);  
  5.     StartCoroutine(WaitAndPrint(2.0F));  
  6.     print("Before WaitAndPrint Finishes " + Time.time);  
  7. }  
  8. IEnumerator WaitAndPrint(float waitTime) {  
  9.     yield return new WaitForSeconds(waitTime);  
  10.     print("WaitAndPrint " + Time.time);  
  11. }  

执行完成后,执行下面的

  1. //运行结果:0秒时打印"Starting 0.0",2秒后打印"WaitAndPrint 2.0"和"Done 2.0"  
  2. // 运行WaitAndPrint直到完成。  
  3. IEnumerator Start() {  
  4.     print("Starting " + Time.time);  
  5.     yield return StartCoroutine(WaitAndPrint(2.0F));  
  6.     print("Done " + ime.time);  
  7. }  
  8. IEnumerator WaitAndPrint(float waitTime) {  
  9.     yield return new WaitForSeconds(waitTime);  
  10.     print("WaitAndPrint " + Time.time);  
  11. }  
  12.       

同步执行

  1. //StopCoroutine停止协同程序  
  2. IEnumerator Start() {  
  3.     StartCoroutine("DoSomething", 2.0F);  
  4.     yield return new WaitForSeconds(1);  
  5.     StopCoroutine("DoSomething");  
  6. }  
  7. IEnumerator DoSomething(float someParameter) {  
  8.     while (true) {  
  9.         print("DoSomething Loop");  
  10.         yield return null;  
  11.     }  
  12. }  

子/父级关系

设置子/父级关系

  1. //B为A的父级  
  2. A.transform.parent = B.transform;  

获取所有子物体

  1. foreach (Transform item in this.transform)  
  2. {  
  3.     print(item.name);  
  4. }  

旋转/角度

旋转(手机)屏幕

  1. //Portrait          正常(头朝上)  
  2. //LandscapeLeft     头朝左  
  3. //LandscapeRight    头朝右  
  4. Screen.orientation = ScreenOrientation.LandscapeLeft;  

限制旋转角度

  1. /// <summary>  
  2. /// 限制旋转角度   
  3. /// </summary>  
  4. /// <param name="angle" />要旋转的Deg  
  5. /// <param name="min" />最小角度  
  6. /// <param name="max" />最大角度  
  7. /// <returns>符合标准的deg</returns>  
  8. public static float ClampAngle(float angle, float min, float max)  
  9. {  
  10.     if (angle < -360)  
  11.         angle += 360;  
  12.     if (angle > 360)  
  13.         angle -= 360;  
  14.     return Mathf.Clamp(angle, min, max);  
  15. }  

限制旋转最大扭距

  1. /// <summary>  
  2. /// 限制旋转最大扭距   
  3. /// </summary>  
  4. /// <param name="angleTarget">要达到的deg  
  5. /// <param name="angleNow">当前角度  
  6. /// <returns>符合标准的deg</returns>  
  7. public static float reviseMaxAngle(float angleTarget, float angleNow)  
  8. {  
  9.     float max = 160;  
  10.   
  11.     float DValue = angleTarget - angleNow;  
  12.   
  13.     if (DValue < -360)  
  14.         DValue += 360;  
  15.     if (DValue > 360)  
  16.         DValue -= 360;  
  17.   
  18.     if (DValue > max)  
  19.     {  
  20.         DValue = max;  
  21.     }  
  22.     else if (DValue < -max)  
  23.     {  
  24.         DValue = -max;  
  25.     }  
  26.     angleTarget = angleNow + DValue;  
  27.   
  28.     return angleTarget;  
  29. }  

Edit

DrawGizmos

  1. void OnDrawGizmos()  
  2. {  
  3.     Gizmos.color = Color.red;  
  4.     Gizmos.DrawCube(transform.position,new Vector3(1,1,2));  
  5. }  

Other

确定目标可见性(α透明)

  1. bool isVisible = (gameCamera.isOrthoGraphic || pos.z > 0f) && (!disableIfInvisible || (pos.x > 0f && pos.x < 1f && pos.y > 0f && pos.y < 1f));  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值