Unity API常用方法和类详细讲解2
019-使用Coroutine实现颜色动画渐变
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(Fade());
}
}
IEnumerator Fade()
{
for (float i = 0; i <= 1; i += 0.1f)
{
cube.GetComponent().material.color = new Color(i, i, i,i);
yield return new WaitForSeconds(0.1f);
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(Fade());
}
}
IEnumerator Fade()
{
while (true)
{
// cube.GetComponent<MeshRenderer>().material.color = new Color(i, i, i,i);
Color color = cube.GetComponent<MeshRenderer>().material.color;
Color newColor = Color.Lerp(color,Color.red,0.02f);
cube.GetComponent<MeshRenderer>().material.color = newColor;
yield return new WaitForSeconds(0.02f);
print(1);
if (Mathf.Abs(Color.red.g-newColor.g)<=0.01f)
{
break;
}
}
}
020-Coroutine协程的开启和关闭
StopCoroutine()与StartCoroutine()的内容必须相同.
private IEnumerator ie;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{ie = Fade();
StartCoroutine(ie);
}
if (Input.GetKeyDown(KeyCode.S))
{
StopCoroutine(ie);
}
}
IEnumerator Fade()
{
while (true)
{
// cube.GetComponent<MeshRenderer>().material.color = new Color(i, i, i,i);
Color color = cube.GetComponent<MeshRenderer>().material.color;
Color newColor = Color.Lerp(color,Color.red,0.02f);
cube.GetComponent<MeshRenderer>().material.color = newColor;
yield return new WaitForSeconds(0.02f);
print(1);
if (Mathf.Abs(Color.red.g-newColor.g)<=0.01f)
{
break;
}
}
}
023-Mathf中的clamp限定方法
Ceil 向上取整返回Float类型
CeilToInt 向上取整返回Int类型
Clamp 加紧
Clamp01限定在01之间
来源:CSDN
作者:是甜的橙啊
链接:https://blog.csdn.net/chengmika/article/details/104516685