WXDUnity API讲解笔记17-39

一、MonoBehaviour类

继承层次:Object->Component->Behaviour->MonoBehaviour

调用方法Invoke(可用于延时):
CancelInvoke() 取消在这个脚本上的所有调用
void Invoke(string methodName, float time) 在time时间内调用名字为methodName的函数
void InvokeRepeating(string methodName, float time, float repeatRate) 在time时间内调用名字为methodName的函数,此后每
repeatRate秒就调用一次该函数
bool IsInvoking() 该MonoBehaviour上是否有等候调用的函数

协程:
StartCooutine 开启协程
StopAllcooutines停止所有的协程
StopCoroutine停止指定的协程

通过协程实现颜色渐变

public class API8CoroutineFadeColor : MonoBehaviour {

    public GameObject cube;
    // private bool started = false;
    
    // Update is called once per frame
    void Update () {
        // if (Input.GetKeyDown(KeyCode.Space) && !started) {
        if(Input.GetKeyDown(KeyCode.Space)) {
            StartCoroutine(Fade());
        }
    }

    IEnumerator Fade() {
        // started = true;
        for(float i = 1; i>=0; i -= 0.1f) {
            cube.GetComponent<MeshRenderer>().material.color=new Color(i,i,i);
            yield return new WaitForSeconds(0.1f);
        }
        yield return null;
    }
}

二、Mathf类

Mathf里面的静态常量

Deg2Rad 角度转为弧度
Rad2Deg 弧度转为角度
Infinity 表示一个无限大的数字
NegativeInfinity 表示一个无限小的数字

Mathf中的Clamp限定方法

Abs 取绝对值
Ceil 向上取整
CeilToInt 向上取整返回int类型

Clamp: 把一个值限定在一个范围之内
Clamp(float value, float min, float max)
如果value比min小,则返回min
如果value比max大,则返回max
如果value在min和max中间,则返回value

Clamp01 把一个值限定在0和1之间

Mathf中的常用方法

ClosestPowerOfTwo 取得离2的次方最近的数
DeltaAngle 计算两个角度之间的最短差值,取两Floor 向下取整
FloorToInt 向下取整返回int
Max 取最大值
Min 取最小值
Pow Pow(float f, float p)
取f的p次方
Sqrt 取参数的平方根个角度最小夹角。

使用Lerp插值运算做变速运动(先快后慢)

在一定范围内,按照给定的比例来往中间插入一个值。是按比例来运动,所以才会越来越慢

float Lerp(float a, float b, float t);

属性
a 起始值
b 结束值
t 0-1的比例

使用MoveTowards做匀速运动

起始值按移动的间距进行移动
float MoveTowards(float current, float target, float maxDelta);
current 当前值
target 目标值
maxDelta 移动的间距

使用PingPong方法实现乒乓的来回匀速运动效果

默认值是0,取值范围在0 – length之间
float PingPong(float t, float length);
length 最大值
t 运动速度,一直增大的数

三、input类

这些方法的参数是KeyCode类型,返回值是Bool类型。
获取鼠标事件
Input.GetMouseButtonDown,
Input.GetMouseButtonUp
Input.GetMouseButton,某键被持续按下

这些方法的参数是Int类型,0是左键,1是右键,2是中键。

获取键盘事件
Input.GetKey,某键被持续按下
Input.GetKeyDown,某键被按下
Input.GetkeyUp,某键被弹起 Input.GetAxis
Input.GetAxis 用法

  • GetAxis(“Mouse X”)

  • GetAxis(“Mouse Y”),

  • GetAxis(“Mouse ScrollWheel”)

  • GetAxis(“Horizontal “)

        GetAxis 是个方法,需要传参数,参数为string类型,参数如下 
        一:触屏类 
        1.Mouse X , 鼠标沿着屏幕X移动时触发 
        2.Mouse Y , 鼠标沿着屏幕Y移动时触发 
        3.Mouse ScrollWheel,当鼠标滚动轮滚动时触发 
        二:键盘操作类 
        1.Vertical , 对应键盘上面的上下箭头,当按下上或下箭头时触,也可对应一个具体的KeyCode,需要“Edit”-“ProjectSettings”-“Input”里面配置的。 
        2.Horizontal ,对应键盘上面的左右箭头,当按下左或右箭头时触发,,也可对应一个具体的KeyCode,需要“Edit”-“ProjectSettings”-“Input”里面配置的。
    

四、Vector2类

Vector2类静态方法
Angle方法:两向量夹角
public static float Angle(Vector2 from, Vector2 to);
参数from为起始向量,to为结束向量。
此方法用于返回两个Vector2实例的夹角,单位为角度,返回值的取值范围为[0,180],并且当from和to中至少有一个向量为Vector2.zero的时候返回值为90。

ClampMagnitude方法:向量长度
public static Vector2 ClampMagnitude(Vector2 vector, float maxLength);
此方法用于返回向量的长度,且最大不超过maxLength。

Lerp方法:向量差值
public static Vector2 Lerp(Vector from, Vector2 to, float t);
参数from为插值的起始向量,参数to为插值的结束向量,参数t为插值系数。

MoveTowards方法:向量差值
public static Vector2 MoveTowards(Vector2 current, Vector2 target, float maxDistanceDelta);
参数current为移动起始点坐标,参数target为移动目标点,参数maxDistanceDelta为移动的参考系数。

Scale方法:向量放缩
public static Vector2 Scale(Vector2 a, Vector2 b);
此方法用于返回向量a按向量b进行放缩后的值,即向量a,b的乘积。

四、Vector3类

静态变量:

相比Vector2而言,Vector3多了z轴,所以多了两个方向 forward和 back

方法:

Cross(Vector3 lhs, Vector3 rhs) – cross product 叉乘
  叉乘的结果方向由左手法则确定,大拇指为a,食指为b,中指即为结果方向

Distance()
Dot()
Lerp()
LerpUnclamped()
Max/ Min()
MoveTowards()
Normalize()

OrthoNormalize(Vector3 normal, Vector3 tangent) – Normalize normal, Normalize tangent,   makes sure tangent is orthogonal (90 degrees) to normal.

Project(Vector3 v, Vector3 onNormal) – v对onNormal做投影后的结果向量

ProjectOnPlane(Vector3 v, Vector3 planeNormal) – v对planeNormal所表示的平面做投影

Reflect(Vector3 inDirection, Vector3 inNormal) – 返回入射光inDirection对于由inNormal所确定   的镜子平面的反射光

Slerp(Vector3 a, Vector3 b, float t) – spherically interpolates btw a and b (a and b are treated   as directions rather than points); the direction of the returned vector is interpolated by the   angle and its magnitude is interpolated btw the magnitudes of a and b
  常用于角色的转向

SlerpUnclamped()

对向量的加减乘除操作

  • :(a+b)即向量相接(可以用来做方向的叠加)
  • :(a-b)即b指向a的向量(可以用来指向)
      比如敌人指向Player的向量,就用Player的坐标所在向量-敌人的坐标所在向量)
  • :*数字 即magnitude变大,方向不变

/ :/数字 即magnitude变小,方向不变

== :三个值相对相同

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值