Transform

Transform组件控制游戏对象在场景中的位置、旋转和大小比例,每个游戏对象都包含一个Transform组件。

成员变量:

成员变量

说明

position

世界坐标系中的位置

localPosition

父对象局部坐标系中的位置

eulerAngles

世界坐标系中以欧拉角表示的旋转

localEulerAngles

父对象局部坐标系中的欧拉角

right

对象在世界坐标系中的右方向

up

对象在世界坐标系中的上方向

forward

对象在世界坐标系中的前方向

rotation

世界坐标系中以四元数表示的旋转

localRotation

父对象局部坐标系中以四元数表示的旋转

localScale

父对象局部坐标系中的缩放比例

parent

父对象的Transform组件

worldToLocalMatrix

世界坐标系到局部坐标系的变换矩阵

localToWorldMatrix

局部坐标系到世界坐标系的变换矩阵

root

对象层级关系中根对象的Transform组件

childCount

子孙对象的数量

lossyScale

全局缩放比例

成员函数:

成员函数

说明

Translate

按指定的方向和距离平移

Rotate

按指定的欧拉角旋转

RotateRound

按给定旋转轴和旋转角度进行旋转

LookAt

旋转使得自身的前方向指定目标的位置

TransformDirection

将一个方向从局部坐标系变换到世界坐标系

InverseTransformDirection

将一个方向从世界坐标系转换为局部坐标系

TransformPoint

将一个位置从局部坐标系转换为世界坐标系

InverseTransformPoint

将一个位置从局部坐标系转换为世界坐标系

DetachChildren

与所有子物体接触父子关系

Find

按名称查找子对象

IsChildOf

判断是否是指定对象的子对象

// 物体向前移动
void Update () {
    var speed=2.0f;
    transform.Translate(Vector3.forward * Time.deltaTime* speed); 
}
// 绕自身Y轴旋转
void Update () {
    var speed=30.0f;
    //Time.deltaTime表示距上一次调用所用的时间。
    transform.Rotate(Vector3.up * Time.deltaTime*speed); 
}
// 物体绕世界坐标轴Y轴旋转
void Update()
{
    var speed = 30.0f;
    transform.RotateAround(Vector3.zero, Vector3.up, speed * Time.deltaTime);
}

// RotateAround源码
/// <summary>
///   <para>Rotates the transform about axis passing through point in world coordinates by angle degrees.</para>
/// </summary>
/// <param name="point"></param>
/// <param name="axis"></param>
/// <param name="angle"></param>
public void RotateAround(Vector3 point, Vector3 axis, float angle)
{
  Vector3 position = this.position;
  Vector3 vector3 = Quaternion.AngleAxis(angle, axis) * (position - point);
  this.position = point + vector3;
  this.RotateAroundInternal(axis, angle * ((float) Math.PI / 180f));
}

Time

Unity中可以通过Time类来获取和时间有关的信息,可以用来计算帧率,调整时间流速等功能。Time类包含了一个重要的变量就是deletaTime,它表示距上一次调用所用的时间。

成员变量:

成员变量

说明

time

游戏从开始到现在经历的时间(秒)

timeSinceLevelLoad

从帧的开始时间(秒),从关卡加载完成开始计算

deltaTime

上一帧耗费的时间(秒)

fixedTime

最近FixedUpdate的时间,该时间从游戏开始计算

fixedDeltaTime

物理引擎和FixedUpdate的更新时间间隔

maximumDeltaTime

一帧的最大耗费时间

smoothDeltaTime

Time.deltaTime的平滑淡出

timeScale

时间流逝速度的比例,可以用来制作慢动作

frameCount

已渲染的帧的总数

realtimeSinceStartup

游戏从开始到现在经历的真实时间(秒),该时间不会受timeScale影响

captureFramerate

固定帧率设置

Random

Random用于生成随机数。

成员变量:

成员变量

说明

seed

随机数生成器种子

value

返回一个0~1之间的随机浮点数,包括0、1

insideUnitSphere

返回位于半径为1的球体内的一个随机点

insideUnitCircle

返回位于半径为1的圆内的一个随机点

onUnitSphere

返回半径为1的球面上一个随机点

rotation

返回一个随机旋转

rotationUnifrom

返回一个均匀分布的随机旋转

成员函数:

成员函数

说明

Range

返回min和max之间的一个随机浮点数,包含min和max

Mathf

成员变量:

成员变量

说明

PI

圆周率,3.1415926

Infinity

正无穷大

NegativeInfinity

负无穷大

Deg2Rad

度到弧度的转换系数,0.01745329f

Rad2Deg

弧度到度的转换系数,57.29578f

Epsilon

一个很小的浮点数

成员函数:

成员函数

说明

Sin

计算角度(单位为弧度)的正弦值

Cos

计算角度(单位为弧度)的余弦值

Tan

计算角度(单位为弧度)的正切值

Sqrt

计算平方根

Abs

计算绝对值

Min

返回若干数中的最小数

Max

返回若干数中的最大数

Pow

Pow(f,p)返回f的p次方

Cell

Cell(f)返回大于或等于f的最小整数

Floor

Floor(f)返回小于或等于f的最大整数

Round

Round(f)返回浮点数f四舍五入后的整数

Clamp

将数值限制在min和max之间

Clamp01

将数值限制在0和1之间

利用Mathf.SmoothDamp函数可以制作相机的缓冲跟踪效果,让物体的移动不是那么僵硬,而是做减速的缓冲效果。

public class SmoothDamp : MonoBehaviour
{
    public Transform target;
    public float smoothTime = 0.3F;
    private float yVelocity = 0.0F;

    // Update is called once per frame
    void Update()
    {
        float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime);
        transform.position = new Vector3(transform.position.x, newPosition, transform.position.z);
    }
}

Coroutine(协同程序)

Coroutine也称为协同程序或者协程,协同程序可以和主程序并行运行,和多线程类似。协同程序可以用来实现让一段程序等待一段时间后继续运行的有效结果。

和协同有关的函数:

函数

说明

StartCoroutine

启动一个协同程序

StopCoroutine

停止一个协同程序

StopAllCoroutine

停止所有协同程序

WaiteForSeconds

等待若干秒

WaiteForFixedUpdate

等待知道下一次FixedUpdate的调用

WaitForSecondsRealtime

等待若秒,不受timeScale影响

WaitUntil

等待某个条件成立再执行