关于 int Mathf.PingPong(t, length); 原理,相当于
#include <iostream>
#include <vector>int test(int t, int length)
{
if(t / length % == )
return length - t % length;
else
return t % length;
}int main()
{
int length = ;
std::vector<int> vect{,,,,,,,,};
for(auto& item : vect)
{
std::cout<<test(item,length)<<",";
}
}
说明参见:http://forum.unity3d.com/threads/mathf-repeat-vs-mathf-pingpong.1388/
类似的还有 Color.Lerp / Vector3.Lerp
在 Update 里调用 Color32.Lerp(colorWhite, colorBlack, Time.time); 一般是指在1秒钟内完成变色,但如果一开始Time.time就不等于0,则结果不是这样,假设Time.time开始执行时就>1,则执行到此代码时,立即变色,如下:
float time = ;void Update () {
time += Time.deltaTime;
if(time>)
{
Color c = Color32.Lerp(Color.white, Color.black, Time.time);
Debug.Log(c.r + "," + c.g + "," + c.b);
gameObject.GetComponent<Image>().color = c;
}
}
如果要使用3秒时间完成变色,则需要如下代码:
float time = ;void Update () {
gameObject.GetComponent<Image>().color = Color32.Lerp(Color.white, Color.black, time);
time += Time.deltaTime / ;
}
Time.time是指
unity 协程
调用 StartCoroutine ,相当于创建并进入了另一个线程,并执行其中的函数,直到遇到第一个 yield return,此时协程暂停,然后将程序执行权返还给调用函数,什么时候恢复执行是根据 yield return 后面的参数决定的,如果是 yield return null; 表示一帧后恢复,如果是 yield return new WaitForSeconds(3); 表示3秒后恢复,如果后面跟一个对象,则该对象不为 nil 时恢复 。