Lerp,就是返回两个值之间的插值,一般有三个参数。第一个参数为初始值,第二个参数为最终值,插值为0~1d的一个浮点数值,为0时为初始值,1时为最终值,为0到1之间的数值时返回一个混合数值。若第三个参数为Time.daltaTime,这返回随时间推移的一个值。

以Color.Lerp为例

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

public class ColorChange : MonoBehaviour
{

    public Image image;
  public float speed;
    private Color old;
    private Color change;
	// Use this for initialization
	void Awake ()
    {
        old = Color.white;
        change = Color.green;
        image.color = old;
        speed=0.5f;
	}
    void Update()
    {
       
            float a = speed * Time.deltaTime;
       
        if (this.GetComponent<Image>().color.b > 0.1f)
            {
                this.GetComponent<Image>().color = Color.Lerp(this.GetComponent<Image>().color, Color.black, a);
            }
            else
                this.GetComponent<Image>().color = change;
    }
}

  运行之后,白色ugui Image会由白色逐渐变成灰黑色

除了Color之外还有

Material.Lerp

public void Lerp(Material start, Material end, float t); 

通常,您想要差值的两个材质是相同的(使用相同的着色器和纹理) 除了颜色和浮点数。然后您可以使用 Lerp 做它们之间的混合。 

Color32.Lerp

Quaternion.Lerp

Interpolates between a and b by t and normalizes the result afterwards. The parameter t is clamped to the range [0, 1].

通过t值from向to之间插值,并且规范化结果。

This is faster than Slerp but looks worse if the rotations are far apart.

这个比Slerp更快但是如果旋转较远看起来就比较差。

Mathf.Lerp

Interpolates a towards b by t. t is clamped between 0 and 1.

基于浮点数t返回a到b之间的插值,t限制在0~1之间。

When t = 0 returns from. When t = 1 return to. When t = 0.5 returns the average of a and b.

当t = 0返回from,当t = 1 返回to。当t = 0.5 返回from和to的平均值。

Vector3.Lerp

可以用来控制游戏对象大小,位置什么的,因为,transfrom中的position跟scale,rotation都是一组Vector3

Vector2.Lerp

Vector4.Lerp

posted on 2017-03-22 16:23  懒人起烂名  阅读(2005)  评论(0编辑  收藏  举报