Unity:2021.1.14flc1
在UnityAR开发的过程中,需要给每一个我生成的GameObject加一个单独的计时器对其进行控制,我写了两个类,TimeCount(计时器)和TimeObject(带计时器的GameObject),在主控件的Update()中,利用TimeOn(int a)函数逐帧的计算时间,并且利用其中的a变量做倍速效果
TimeCount
public class TimeCount
{
private float timer = 0f;
public float Timer { get { return timer; } set { timer = value; } }
public int H { get { return h; } set { h = value; } }
public int M { get { return m; } set { m = value; } }
public int S { get { return s; } set { s = value; } }
private int h = 0;
private int m = 0;
private int s = 0;
// Update is called once per frame
public void TimeOn(int a)
{
timer += Time.deltaTime*a*1f;
if (timer >= 1f)
{
s++;
timer = 0;
}
if (s >= 60)
{
m++;
s = 0;
}
if (m >= 60)
{
h++;
m = 0;
}
if (h >= 99)
{
h = 0;
}
}
public void TimeStop()
{
h = 0;
m = 0;
s = 0;
}
}
TimeObject
public class TimeObject
{
/// <summary>
/// 种植物模型对象
/// </summary>
public GameObject Plant;
/// <summary>
/// 计时器
/// </summary>
public TimeCount Time
{
get
{
return time;
}
set
{
time = value;
}
}
/// <summary>
/// 计时器
/// </summary>
private TimeCount time;
public TimeObject(GameObject Plant)
{
this.Plant = Plant;
time = new TimeCount();
}
public TimeObject()
{
Plant = new GameObject();
time = new TimeCount();
}