[转载]【Unity 3D】学习笔记二十八:unity工具类

本文转载自:https://blog.csdn.net/u013289188/article/details/36869169

unity为开发者提供了很多方便开发的工具,他们都是由系统封装的一些功能和方法。比如说:实现时间的time类,获取随机数的Random.Range( )方法等等。


时间类

time类,主要用来获取当前的系统时间。


    
    
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Script_04_13 : MonoBehaviour {
  4. void OnGUI()
  5. {
  6. GUILayout.Label( “当前游戏时间:” + Time.time);
  7. GUILayout.Label( “上一帧所消耗的时间:” + Time.deltaTime);
  8. GUILayout.Label( “固定增量时间:” + Time.fixedTime);
  9. GUILayout.Label( “上一帧所消耗固定时间:” + Time.fixedDeltaTime);
  10. }
  11. }
运行:


Time.time:从游戏开始后计时,表示截至目前游戏运行的总时间。
 Time.deltaTime:获取update()方法中完成上一帧所消耗的时间。
 Time.fixedTime:FixedUpdate( )方法中固定消耗的时间总和。
 Time.fixedDeltaTime:固定更新上一帧所耗时间。



程序等待

在程序中使用WaitForSeconds()方法可以以秒为单位让程序等待一段时间,可直接使游戏主线程进入等待状态。该方法返回值为IEnumerator类型。


    
    
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Script_04_14 : MonoBehaviour {
  4. IEnumerator Start()
  5. {
  6. //等待
  7. return Test();
  8. }
  9. //返回等待时间
  10. IEnumerator Test()
  11. {
  12. Debug.Log( "开始等待:" + Time.time);
  13. yield return new WaitForSeconds(2);
  14. Debug.Log( "结束等待:" + Time.time);
  15. }
  16. }
运行:


在需要等待的地方调用yield return new WaitForSeconds(2),方法中的2表示等待2秒。



数学类

unity为开发者封装了一个数学类Mathf。

Mathf.Abs(int i)返回一个整型绝对值。

Mathf.sin(5)返回正弦值。

Mathf.Max(0,100)返回两个数的最大值

Mathf.PI圆周率

感兴趣的可以自行查看API文档或者谷歌



四元数

四元数是非常非常重要的工具类。unity中实现游戏对象旋转,其低层都是由四元数实现的,它可以精确的计算游戏对象旋转的角度。

下面会通过一个简单的例子来总结,在这个例子中,点击旋转固定角度,游戏对象将沿Y轴直接旋转50度,点击插值旋转固定角度,游戏对象将沿Y轴直接旋转60度.


    
    
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Script_04_16 : MonoBehaviour
  4. {
  5. //是否开始插值旋转
  6. bool isRotation = false;
  7. void OnGUI()
  8. {
  9. if(GUILayout.Button( "旋转固定角度",GUILayout.Height( 50)))
  10. {
  11. gameObject.transform.rotation = Quaternion.Euler( 0.0f, 50.0f, 0.0f);
  12. }
  13. if(GUILayout.Button( "插值旋转固定角度",GUILayout.Height( 50)))
  14. {
  15. isRotation = true;
  16. }
  17. }
  18. void Update()
  19. {
  20. //开始差值旋转
  21. if(isRotation)
  22. {
  23. gameObject.transform.rotation = Quaternion.Slerp (gameObject.transform.rotation, Quaternion.Euler( 0.0f, 60.0f, 0.0f), Time.time * 0.1f);
  24. }
  25. }
  26. }
运行:

点击旋转固定角度:

点击插值旋转固定角度:


 Quaternion.Euler( )



Quaternion.Slerp ()



随机数

有时程序需要获取随机数,实现很简单使用Random.Range()方法就行


    
    
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Script_04_15 : MonoBehaviour {
  4. void Start()
  5. {
  6. int a = Random.Range( 0, 100);
  7. float b = Random.Range( 0.0f, 10.0f);
  8. Debug.Log( "获取一个0-100之间的整形随机数" + a);
  9. Debug.Log( "获取一个0.0f-10.0f之间的浮点型随机数" + b);
  10. }
  11. }
运行:


            </div>

unity为开发者提供了很多方便开发的工具,他们都是由系统封装的一些功能和方法。比如说:实现时间的time类,获取随机数的Random.Range( )方法等等。


时间类

time类,主要用来获取当前的系统时间。


  
  
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Script_04_13 : MonoBehaviour {
  4. void OnGUI()
  5. {
  6. GUILayout.Label( “当前游戏时间:” + Time.time);
  7. GUILayout.Label( “上一帧所消耗的时间:” + Time.deltaTime);
  8. GUILayout.Label( “固定增量时间:” + Time.fixedTime);
  9. GUILayout.Label( “上一帧所消耗固定时间:” + Time.fixedDeltaTime);
  10. }
  11. }
运行:


Time.time:从游戏开始后计时,表示截至目前游戏运行的总时间。
 Time.deltaTime:获取update()方法中完成上一帧所消耗的时间。
 Time.fixedTime:FixedUpdate( )方法中固定消耗的时间总和。
 Time.fixedDeltaTime:固定更新上一帧所耗时间。



程序等待

在程序中使用WaitForSeconds()方法可以以秒为单位让程序等待一段时间,可直接使游戏主线程进入等待状态。该方法返回值为IEnumerator类型。


  
  
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Script_04_14 : MonoBehaviour {
  4. IEnumerator Start()
  5. {
  6. //等待
  7. return Test();
  8. }
  9. //返回等待时间
  10. IEnumerator Test()
  11. {
  12. Debug.Log( "开始等待:" + Time.time);
  13. yield return new WaitForSeconds(2);
  14. Debug.Log( "结束等待:" + Time.time);
  15. }
  16. }
运行:


在需要等待的地方调用yield return new WaitForSeconds(2),方法中的2表示等待2秒。



数学类

unity为开发者封装了一个数学类Mathf。

Mathf.Abs(int i)返回一个整型绝对值。

Mathf.sin(5)返回正弦值。

Mathf.Max(0,100)返回两个数的最大值

Mathf.PI圆周率

感兴趣的可以自行查看API文档或者谷歌



四元数

四元数是非常非常重要的工具类。unity中实现游戏对象旋转,其低层都是由四元数实现的,它可以精确的计算游戏对象旋转的角度。

下面会通过一个简单的例子来总结,在这个例子中,点击旋转固定角度,游戏对象将沿Y轴直接旋转50度,点击插值旋转固定角度,游戏对象将沿Y轴直接旋转60度.


  
  
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Script_04_16 : MonoBehaviour
  4. {
  5. //是否开始插值旋转
  6. bool isRotation = false;
  7. void OnGUI()
  8. {
  9. if(GUILayout.Button( "旋转固定角度",GUILayout.Height( 50)))
  10. {
  11. gameObject.transform.rotation = Quaternion.Euler( 0.0f, 50.0f, 0.0f);
  12. }
  13. if(GUILayout.Button( "插值旋转固定角度",GUILayout.Height( 50)))
  14. {
  15. isRotation = true;
  16. }
  17. }
  18. void Update()
  19. {
  20. //开始差值旋转
  21. if(isRotation)
  22. {
  23. gameObject.transform.rotation = Quaternion.Slerp (gameObject.transform.rotation, Quaternion.Euler( 0.0f, 60.0f, 0.0f), Time.time * 0.1f);
  24. }
  25. }
  26. }
运行:

点击旋转固定角度:

点击插值旋转固定角度:


 Quaternion.Euler( )



Quaternion.Slerp ()



随机数

有时程序需要获取随机数,实现很简单使用Random.Range()方法就行


  
  
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Script_04_15 : MonoBehaviour {
  4. void Start()
  5. {
  6. int a = Random.Range( 0, 100);
  7. float b = Random.Range( 0.0f, 10.0f);
  8. Debug.Log( "获取一个0-100之间的整形随机数" + a);
  9. Debug.Log( "获取一个0.0f-10.0f之间的浮点型随机数" + b);
  10. }
  11. }
运行:


            </div>

猜你喜欢

转载自blog.csdn.net/qq_16440237/article/details/81179127
今日推荐