Sou*_*k21 2 c# variables buffer unity-game-engine
我需要知道10帧前变量的值.我想过制作一个数组,但每帧的偏移值似乎有点过分.
有什么想法/想法吗?
您可以基于a创建数据结构System.Collections.Generic.Queue<T>来存储每个帧的变量.
优于a的优点Array是您不需要在每个帧上移动每个变量,只需添加最新的变量即可.这使它成为一个O(1)操作,而不是O(n).
class History<T>
{
Queue<T> data;
public int MaxCapacity { get; private set; }
public History(int maxCapacity)
{
MaxCapacity = maxCapacity;
data = new Queue<T>(maxCapacity);
}
public void AddEntry(T newData)
{
if (data.Count >= MaxCapacity)
{
data.Dequeue();
}
data.Enqueue(newData);
}
public T Peek()
{
return data.Peek();
}
}
Run Code Online (Sandbox Code Playgroud)
var h = new History<float>(10);
//on each frame
h.AddEntry(0.12345f);
//get the value 10 frames ago (or the earliest one recorded)
Console.WriteLine(h.Peek());
Run Code Online (Sandbox Code Playgroud)
我将把它留给读者来实现进一步的实用方法,如Clear().
| 归档时间: |
|
| 查看次数: |
467 次 |
| 最近记录: |