本文转载自:https://blog.csdn.net/u013289188/article/details/36869169
unity为开发者提供了很多方便开发的工具,他们都是由系统封装的一些功能和方法。比如说:实现时间的time类,获取随机数的Random.Range( )方法等等。
时间类
time类,主要用来获取当前的系统时间。
-
using UnityEngine;
-
using System.Collections;
-
-
public
class
Script_04_13 :
MonoBehaviour {
-
-
void OnGUI()
-
{
-
GUILayout.Label(
“当前游戏时间:” + Time.time);
-
GUILayout.Label(
“上一帧所消耗的时间:” + Time.deltaTime);
-
GUILayout.Label(
“固定增量时间:” + Time.fixedTime);
-
GUILayout.Label(
“上一帧所消耗固定时间:” + Time.fixedDeltaTime);
-
}
-
}
运行:
Time.time:从游戏开始后计时,表示截至目前游戏运行的总时间。
Time.deltaTime:获取update()方法中完成上一帧所消耗的时间。
Time.fixedTime:FixedUpdate( )方法中固定消耗的时间总和。
Time.fixedDeltaTime:固定更新上一帧所耗时间。
程序等待
在程序中使用WaitForSeconds()方法可以以秒为单位让程序等待一段时间,可直接使游戏主线程进入等待状态。该方法返回值为IEnumerator类型。
-
using UnityEngine;
-
using System.Collections;
-
-
public
class
Script_04_14 :
MonoBehaviour {
-
-
IEnumerator Start()
-
{
-
//等待
-
return Test();
-
}
-
-
//返回等待时间
-
IEnumerator Test()
-
{
-
Debug.Log(
"开始等待:" + Time.time);
-
yield return new WaitForSeconds(2);
-
Debug.Log(
"结束等待:" + Time.time);
-
}
-
-
}
运行:
在需要等待的地方调用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度.
-
using UnityEngine;
-
using System.Collections;
-
-
public
class
Script_04_16 :
MonoBehaviour
-
{
-
//是否开始插值旋转
-
bool isRotation =
false;
-
void OnGUI()
-
{
-
-
if(GUILayout.Button(
"旋转固定角度",GUILayout.Height(
50)))
-
{
-
gameObject.transform.rotation = Quaternion.Euler(
0.0f,
50.0f,
0.0f);
-
}
-
-
if(GUILayout.Button(
"插值旋转固定角度",GUILayout.Height(
50)))
-
{
-
isRotation =
true;
-
}
-
}
-
-
void Update()
-
{
-
//开始差值旋转
-
if(isRotation)
-
{
-
gameObject.transform.rotation = Quaternion.Slerp (gameObject.transform.rotation, Quaternion.Euler(
0.0f,
60.0f,
0.0f), Time.time *
0.1f);
-
}
-
}
-
-
}
运行:
点击旋转固定角度:
点击插值旋转固定角度:
Quaternion.Euler( )
Quaternion.Slerp ()
随机数
有时程序需要获取随机数,实现很简单使用Random.Range()方法就行
-
using UnityEngine;
-
using System.Collections;
-
-
public
class
Script_04_15 :
MonoBehaviour {
-
-
void Start()
-
{
-
int a = Random.Range(
0,
100);
-
float b = Random.Range(
0.0f,
10.0f);
-
-
Debug.Log(
"获取一个0-100之间的整形随机数" + a);
-
Debug.Log(
"获取一个0.0f-10.0f之间的浮点型随机数" + b);
-
}
-
-
-
}
运行:
</div>
unity为开发者提供了很多方便开发的工具,他们都是由系统封装的一些功能和方法。比如说:实现时间的time类,获取随机数的Random.Range( )方法等等。
时间类
time类,主要用来获取当前的系统时间。
-
using UnityEngine;
-
using System.Collections;
-
-
public
class
Script_04_13 :
MonoBehaviour {
-
-
void OnGUI()
-
{
-
GUILayout.Label(
“当前游戏时间:” + Time.time);
-
GUILayout.Label(
“上一帧所消耗的时间:” + Time.deltaTime);
-
GUILayout.Label(
“固定增量时间:” + Time.fixedTime);
-
GUILayout.Label(
“上一帧所消耗固定时间:” + Time.fixedDeltaTime);
-
}
-
}
运行:
Time.time:从游戏开始后计时,表示截至目前游戏运行的总时间。
Time.deltaTime:获取update()方法中完成上一帧所消耗的时间。
Time.fixedTime:FixedUpdate( )方法中固定消耗的时间总和。
Time.fixedDeltaTime:固定更新上一帧所耗时间。
程序等待
在程序中使用WaitForSeconds()方法可以以秒为单位让程序等待一段时间,可直接使游戏主线程进入等待状态。该方法返回值为IEnumerator类型。
-
using UnityEngine;
-
using System.Collections;
-
-
public
class
Script_04_14 :
MonoBehaviour {
-
-
IEnumerator Start()
-
{
-
//等待
-
return Test();
-
}
-
-
//返回等待时间
-
IEnumerator Test()
-
{
-
Debug.Log(
"开始等待:" + Time.time);
-
yield return new WaitForSeconds(2);
-
Debug.Log(
"结束等待:" + Time.time);
-
}
-
-
}
运行:
在需要等待的地方调用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度.
-
using UnityEngine;
-
using System.Collections;
-
-
public
class
Script_04_16 :
MonoBehaviour
-
{
-
//是否开始插值旋转
-
bool isRotation =
false;
-
void OnGUI()
-
{
-
-
if(GUILayout.Button(
"旋转固定角度",GUILayout.Height(
50)))
-
{
-
gameObject.transform.rotation = Quaternion.Euler(
0.0f,
50.0f,
0.0f);
-
}
-
-
if(GUILayout.Button(
"插值旋转固定角度",GUILayout.Height(
50)))
-
{
-
isRotation =
true;
-
}
-
}
-
-
void Update()
-
{
-
//开始差值旋转
-
if(isRotation)
-
{
-
gameObject.transform.rotation = Quaternion.Slerp (gameObject.transform.rotation, Quaternion.Euler(
0.0f,
60.0f,
0.0f), Time.time *
0.1f);
-
}
-
}
-
-
}
运行:
点击旋转固定角度:
点击插值旋转固定角度:
Quaternion.Euler( )
Quaternion.Slerp ()
随机数
有时程序需要获取随机数,实现很简单使用Random.Range()方法就行
-
using UnityEngine;
-
using System.Collections;
-
-
public
class
Script_04_15 :
MonoBehaviour {
-
-
void Start()
-
{
-
int a = Random.Range(
0,
100);
-
float b = Random.Range(
0.0f,
10.0f);
-
-
Debug.Log(
"获取一个0-100之间的整形随机数" + a);
-
Debug.Log(
"获取一个0.0f-10.0f之间的浮点型随机数" + b);
-
}
-
-
-
}
运行:
</div>