运行时缓存优化

This commit is contained in:
2025-12-04 15:58:38 +08:00
parent 4ff275946d
commit 9bb253d5ae
2 changed files with 17 additions and 23 deletions

View File

@@ -3,10 +3,12 @@ using System.Collections.Generic;
using Convention;
using UnityEngine;
using System;
using Unity.Collections;
using UnityEngine.Rendering;
namespace Demo.Game
{
public class DDT : ScriptableObject, IEnumerable<float>
public class DDT : ScriptableObject
{
protected override bool IsSelfEnableUpdate => false;
public static DDT Make()
@@ -14,27 +16,31 @@ namespace Demo.Game
return new GameObject().AddComponent<DDT>();
}
public List<float> Datas = new();
public NativeArray<float> Datas = new(128, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
public int Count { get; private set; } = 0;
[Convention.RScript.Variable.Attr.Method]
public void Add(float value)
{
Datas.Add(value);
if (Count >= Datas.Length)
Datas.ResizeArray(Mathf.FloorToInt(Count * 1.5f));
Datas[Count] = value;
Count++;
}
[Convention.RScript.Variable.Attr.Method]
public void Add(int barSplitTimes, int barCount, int tickCount)
{
Datas.Add((barCount + tickCount / (float)barSplitTimes) * OneBarTime);
Add((barCount + tickCount / (float)barSplitTimes) * OneBarTime);
}
[Convention.RScript.Variable.Attr.Method]
public float At(int index)
{
if (index < 0)
index = Datas.Count + index;
if (index < 0 || index >= Datas.Count)
throw new IndexOutOfRangeException($"{index} is out of [0, {Datas.Count})");
index = Count + index;
if (index < 0 || index >= Count)
throw new IndexOutOfRangeException($"{index} is out of [0, {Count})");
return Datas[index];
}
@@ -45,19 +51,7 @@ namespace Demo.Game
[Convention.RScript.Variable.Attr.Method]
public int GetCount()
{
return Datas.Count;
return Count;
}
public IEnumerator<float> GetEnumerator()
{
return ((IEnumerable<float>)Datas).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)Datas).GetEnumerator();
}
public int Count => Datas.Count;
}
}

View File

@@ -63,9 +63,9 @@ namespace Demo.Game
{
Entries.Sort((x, y) => x.TimePoint.CompareTo(y.TimePoint));
CompiledEntries = new(
new NativeArray<float>(Entries.Count, Allocator.Persistent),
new NativeArray<DataType>(Entries.Count, Allocator.Persistent),
new NativeArray<MathExtension.EaseCurveType>(Entries.Count, Allocator.Persistent),
new NativeArray<float>(Entries.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory),
new NativeArray<DataType>(Entries.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory),
new NativeArray<MathExtension.EaseCurveType>(Entries.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory),
Entries.Count
);
int index = 0;