推进修改
This commit is contained in:
@@ -14,7 +14,8 @@ namespace Demo.Game
|
||||
{
|
||||
public class DDTConfig : ScriptLoadableConfig
|
||||
{
|
||||
public NativeArray<float> Datas = new(128, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
|
||||
[Content] public NativeArray<float> Datas = new(128, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
|
||||
[Content] public int Count = 0;
|
||||
|
||||
public override void Deserialize(BinaryReader reader)
|
||||
{
|
||||
@@ -33,22 +34,25 @@ namespace Demo.Game
|
||||
[Scriptable]
|
||||
public class DDT : ScriptableObject
|
||||
{
|
||||
protected override ConfigType.ScriptLoadableConfig MakeConfig()
|
||||
{
|
||||
return new ConfigType.DDTConfig();
|
||||
}
|
||||
protected override bool IsSelfEnableUpdate => false;
|
||||
public static DDT Make()
|
||||
{
|
||||
return new GameObject().AddComponent<DDT>();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (Count >= Datas.Length)
|
||||
Datas.ResizeArray(Mathf.FloorToInt(Count * 1.5f));
|
||||
Datas[Count] = value;
|
||||
Count++;
|
||||
int Count = GetConfig<ConfigType.DDTConfig>().Count;
|
||||
if (Count >= GetConfig<ConfigType.DDTConfig>().Datas.Length)
|
||||
GetConfig<ConfigType.DDTConfig>().Datas.ResizeArray(Mathf.FloorToInt(Count * 1.5f));
|
||||
GetConfig<ConfigType.DDTConfig>().Datas[Count] = value;
|
||||
GetConfig<ConfigType.DDTConfig>().Count++;
|
||||
}
|
||||
|
||||
[Convention.RScript.Variable.Attr.Method]
|
||||
@@ -60,27 +64,24 @@ namespace Demo.Game
|
||||
[Convention.RScript.Variable.Attr.Method]
|
||||
public float At(int index)
|
||||
{
|
||||
int Count = GetConfig<ConfigType.DDTConfig>().Count;
|
||||
if (index < 0)
|
||||
index = Count + index;
|
||||
if (index < 0 || index >= Count)
|
||||
throw new IndexOutOfRangeException($"{index} is out of [0, {Count})");
|
||||
return Datas[index];
|
||||
return GetConfig<ConfigType.DDTConfig>().Datas[index];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Convention.RScript.Variable.Attr.Method]
|
||||
public int GetCount()
|
||||
{
|
||||
return Count;
|
||||
return GetConfig<ConfigType.DDTConfig>().Count;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (Datas.IsCreated)
|
||||
Datas.Dispose();
|
||||
if (GetConfig<ConfigType.DDTConfig>().Datas.IsCreated)
|
||||
GetConfig<ConfigType.DDTConfig>().Datas.Dispose();
|
||||
}
|
||||
|
||||
#region Serialize
|
||||
@@ -91,36 +92,25 @@ namespace Demo.Game
|
||||
yield return this.ParseScript2Expr(scriptFile.LoadAsText());
|
||||
using var stream = File.OpenWrite(cacheFile.GetFullPath());
|
||||
using var writer = new BinaryWriter(stream);
|
||||
writer.Write(Count);
|
||||
for (int i = 0; i < Count; i++)
|
||||
writer.Write(GetConfig<ConfigType.DDTConfig>().Count);
|
||||
for (int i = 0, e = GetConfig<ConfigType.DDTConfig>().Count; i < e; i++)
|
||||
{
|
||||
writer.Write(Datas[i]);
|
||||
writer.Write(GetConfig<ConfigType.DDTConfig>().Datas[i]);
|
||||
}
|
||||
}
|
||||
protected override IEnumerator LoadFromImptCacheFile(ToolFile cacheFile)
|
||||
{
|
||||
using var stream = File.OpenRead(cacheFile.GetFullPath());
|
||||
using var reader = new BinaryReader(stream);
|
||||
Count = reader.ReadInt32();
|
||||
Datas.ResizeArray(Mathf.Max(128, Count));
|
||||
for (int i = 0; i < Count; i++)
|
||||
GetConfig<ConfigType.DDTConfig>().Count = reader.ReadInt32();
|
||||
GetConfig<ConfigType.DDTConfig>().Datas.ResizeArray(Mathf.Max(128, GetConfig<ConfigType.DDTConfig>().Count));
|
||||
for (int i = 0, e = GetConfig<ConfigType.DDTConfig>().Count; i < e; i++)
|
||||
{
|
||||
Datas[i] = reader.ReadSingle();
|
||||
GetConfig<ConfigType.DDTConfig>().Datas[i] = reader.ReadSingle();
|
||||
}
|
||||
yield break;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[Setting, SerializeField] private List<float> d_Datas = new();
|
||||
protected override IEnumerator DoSomethingDuringApplyScript()
|
||||
{
|
||||
yield return base.DoSomethingDuringApplyScript();
|
||||
for (int i = 0; i < Count; i++)
|
||||
d_Datas.Add(Datas[i]);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user