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();
}
本文介绍了一种在Unity中为每个GameObject实现独立计时器的方法。通过创建TimeCount和TimeObject两个类,实现了GameObject上的时间管理和控制功能。TimeCount类负责计时逻辑,而TimeObject类则用于绑定GameObject和计时器。
765

被折叠的 条评论
为什么被折叠?



