Files
Convention-Unity-Demo/Assets/Scripts/Framework/DDT.cs

105 lines
3.1 KiB
C#
Raw Normal View History

2025-11-25 17:04:19 +08:00
using System.Collections;
2025-09-25 19:04:05 +08:00
using System.Collections.Generic;
using Convention;
using UnityEngine;
using System;
2025-12-04 15:58:38 +08:00
using Unity.Collections;
using UnityEngine.Rendering;
using System.IO;
2025-09-25 19:04:05 +08:00
namespace Demo.Game
{
2025-12-04 15:58:38 +08:00
public class DDT : ScriptableObject
2025-09-25 19:04:05 +08:00
{
2025-11-29 00:44:05 +08:00
protected override bool IsSelfEnableUpdate => false;
2025-09-25 19:04:05 +08:00
public static DDT Make()
{
return new GameObject().AddComponent<DDT>();
}
2025-12-04 15:58:38 +08:00
public NativeArray<float> Datas = new(128, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
public int Count { get; private set; } = 0;
2025-09-25 19:04:05 +08:00
2025-11-12 10:09:26 +08:00
[Convention.RScript.Variable.Attr.Method]
public void Add(float value)
{
2025-12-04 15:58:38 +08:00
if (Count >= Datas.Length)
Datas.ResizeArray(Mathf.FloorToInt(Count * 1.5f));
Datas[Count] = value;
Count++;
2025-11-12 10:09:26 +08:00
}
[Convention.RScript.Variable.Attr.Method]
public void Add(int barSplitTimes, int barCount, int tickCount)
{
2025-12-04 15:58:38 +08:00
Add((barCount + tickCount / (float)barSplitTimes) * OneBarTime);
2025-11-12 10:09:26 +08:00
}
2025-11-25 17:04:19 +08:00
[Convention.RScript.Variable.Attr.Method]
public float At(int index)
{
if (index < 0)
2025-12-04 15:58:38 +08:00
index = Count + index;
if (index < 0 || index >= Count)
throw new IndexOutOfRangeException($"{index} is out of [0, {Count})");
2025-11-25 17:04:19 +08:00
return Datas[index];
}
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
/// </summary>
/// <returns></returns>
[Convention.RScript.Variable.Attr.Method]
public int GetCount()
{
2025-12-04 15:58:38 +08:00
return Count;
}
2025-12-05 16:26:42 +08:00
private void OnDestroy()
{
if (Datas.IsCreated)
Datas.Dispose();
}
#region Serialize
protected override bool IsImptSerialize => true;
protected override IEnumerator CreateAndLoadingImptCacheFile(ToolFile scriptFile, ToolFile cacheFile)
{
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(Datas[i]);
}
}
protected override IEnumerator LoadFromImptCacheFile(ToolFile cacheFile)
{
2025-12-09 15:12:25 +08:00
using var stream = File.OpenRead(cacheFile.GetFullPath());
using var reader = new BinaryReader(stream);
2025-12-09 15:12:25 +08:00
Count = reader.ReadInt32();
Datas.ResizeArray(Mathf.Max(128, Count));
2025-12-09 15:12:25 +08:00
for (int i = 0; i < Count; i++)
{
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
2025-09-25 19:04:05 +08:00
}
}