异步加载已修复, Config更新正推动
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Convention;
|
||||
using Demo.Editor.UI;
|
||||
using Demo.Game.ConfigType;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
@@ -115,39 +116,9 @@ namespace Demo.Game
|
||||
public DataType Position = default;
|
||||
public MathExtension.EaseCurveType easeCurveType = MathExtension.EaseCurveType.Linear;
|
||||
}
|
||||
[Serializable]
|
||||
public struct UpdatementCompiledEntries: IDisposable
|
||||
{
|
||||
public NativeArray<float> TimePoints;
|
||||
public NativeArray<DataType> Positions;
|
||||
public NativeArray<MathExtension.EaseCurveType> EaseCurveTypes;
|
||||
public readonly int Count;
|
||||
|
||||
public UpdatementCompiledEntries(NativeArray<float> timePoints,
|
||||
NativeArray<DataType> positions,
|
||||
NativeArray<MathExtension.EaseCurveType> easeCurveTypes,
|
||||
int count)
|
||||
{
|
||||
TimePoints = timePoints;
|
||||
Positions = positions;
|
||||
EaseCurveTypes = easeCurveTypes;
|
||||
Count = count;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (TimePoints.IsCreated)
|
||||
TimePoints.Dispose();
|
||||
if (Positions.IsCreated)
|
||||
Positions.Dispose();
|
||||
if (EaseCurveTypes.IsCreated)
|
||||
EaseCurveTypes.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public int Content = 0;
|
||||
private readonly List<UpdatementEntry> Entries = new();
|
||||
public UpdatementCompiledEntries CompiledEntries;
|
||||
protected abstract void UpdateData(DataType data);
|
||||
protected abstract DataType Lerp(DataType begin, DataType end, float t);
|
||||
|
||||
@@ -170,18 +141,15 @@ namespace Demo.Game
|
||||
private void BuildupCompiledEntriesAndReleaseEntries()
|
||||
{
|
||||
Entries.Sort((x, y) => x.TimePoint.CompareTo(y.TimePoint));
|
||||
CompiledEntries = new(
|
||||
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
|
||||
);
|
||||
GetConfig<UpdatementConfig<DataType>>().TimePoints = new NativeArray<float>(Entries.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
|
||||
GetConfig<UpdatementConfig<DataType>>().Positions = new NativeArray<DataType>(Entries.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
|
||||
GetConfig<UpdatementConfig<DataType>>().EaseCurveTypes = new NativeArray<MathExtension.EaseCurveType>(Entries.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
|
||||
int index = 0;
|
||||
foreach (var item in Entries)
|
||||
{
|
||||
CompiledEntries.TimePoints[index] = item.TimePoint;
|
||||
CompiledEntries.Positions[index] = item.Position;
|
||||
CompiledEntries.EaseCurveTypes[index] = item.easeCurveType;
|
||||
GetConfig<UpdatementConfig<DataType>>().TimePoints[index] = item.TimePoint;
|
||||
GetConfig<UpdatementConfig<DataType>>().Positions[index] = item.Position;
|
||||
GetConfig<UpdatementConfig<DataType>>().EaseCurveTypes[index] = item.easeCurveType;
|
||||
index++;
|
||||
}
|
||||
Entries.Clear();
|
||||
@@ -190,10 +158,10 @@ namespace Demo.Game
|
||||
private void UpdateEntry(int start, float percent)
|
||||
{
|
||||
int head = start;
|
||||
int tail = Mathf.Min(start + 1, CompiledEntries.Count - 1);
|
||||
UpdateData(Lerp(CompiledEntries.Positions[start],
|
||||
CompiledEntries.Positions[tail],
|
||||
MathExtension.Evaluate(Mathf.Clamp01(percent), CompiledEntries.EaseCurveTypes[head])));
|
||||
int tail = Mathf.Min(start + 1, GetConfig<UpdatementConfig<DataType>>().TimePoints.Length - 1);
|
||||
UpdateData(Lerp(GetConfig<UpdatementConfig<DataType>>().Positions[start],
|
||||
GetConfig<UpdatementConfig<DataType>>().Positions[tail],
|
||||
MathExtension.Evaluate(Mathf.Clamp01(percent), GetConfig<UpdatementConfig<DataType>>().EaseCurveTypes[head])));
|
||||
}
|
||||
|
||||
protected override IEnumerator DoSomethingDuringApplyScript()
|
||||
@@ -209,7 +177,7 @@ namespace Demo.Game
|
||||
public override void ResetEnterGameStatus()
|
||||
{
|
||||
base.ResetEnterGameStatus();
|
||||
if (CompiledEntries.Count <= 1)
|
||||
if (GetConfig<UpdatementConfig<DataType>>().TimePoints.Length <= 1)
|
||||
return;
|
||||
UpdateEntry(0, 0);
|
||||
}
|
||||
@@ -217,7 +185,9 @@ namespace Demo.Game
|
||||
public override IEnumerator UnloadScript()
|
||||
{
|
||||
Content = 0;
|
||||
CompiledEntries = default;
|
||||
GetConfig<UpdatementConfig<DataType>>().TimePoints.Dispose();
|
||||
GetConfig<UpdatementConfig<DataType>>().Positions.Dispose();
|
||||
GetConfig<UpdatementConfig<DataType>>().EaseCurveTypes.Dispose();
|
||||
yield return base.UnloadScript();
|
||||
}
|
||||
|
||||
@@ -227,12 +197,12 @@ namespace Demo.Game
|
||||
|
||||
float GetPercentValue()
|
||||
{
|
||||
if (Content + 1 == CompiledEntries.Count)
|
||||
if (Content + 1 == GetConfig<UpdatementConfig<DataType>>().TimePoints.Length)
|
||||
return 1;
|
||||
return (currentTime - CompiledEntries.TimePoints[Content]) / (CompiledEntries.TimePoints[Content + 1] - CompiledEntries.TimePoints[Content]);
|
||||
return (currentTime - GetConfig<UpdatementConfig<DataType>>().TimePoints[Content]) / (GetConfig<UpdatementConfig<DataType>>().TimePoints[Content + 1] - GetConfig<UpdatementConfig<DataType>>().TimePoints[Content]);
|
||||
}
|
||||
|
||||
if (CompiledEntries.Count <= 1)
|
||||
if (GetConfig<UpdatementConfig<DataType>>().TimePoints.Length <= 1)
|
||||
return;
|
||||
switch (tickType)
|
||||
{
|
||||
@@ -240,17 +210,17 @@ namespace Demo.Game
|
||||
case TickType.Start:
|
||||
{
|
||||
Content = 0;
|
||||
while (Content + 1 < CompiledEntries.Count && CompiledEntries.TimePoints[Content + 1] < currentTime)
|
||||
while (Content + 1 < GetConfig<UpdatementConfig<DataType>>().TimePoints.Length && GetConfig<UpdatementConfig<DataType>>().TimePoints[Content + 1] < currentTime)
|
||||
Content++;
|
||||
UpdateEntry(Content, GetPercentValue());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (CompiledEntries.TimePoints[0] > currentTime)
|
||||
if (GetConfig<UpdatementConfig<DataType>>().TimePoints[0] > currentTime)
|
||||
return;
|
||||
if (Content + 1 < CompiledEntries.Count && CompiledEntries.TimePoints[Content + 1] < currentTime)
|
||||
if (Content + 1 < GetConfig<UpdatementConfig<DataType>>().TimePoints.Length && GetConfig<UpdatementConfig<DataType>>().TimePoints[Content + 1] < currentTime)
|
||||
Content++;
|
||||
if (Content + 1 > CompiledEntries.Count)
|
||||
if (Content + 1 > GetConfig<UpdatementConfig<DataType>>().TimePoints.Length)
|
||||
return;
|
||||
UpdateEntry(Content, GetPercentValue());
|
||||
break;
|
||||
@@ -259,21 +229,22 @@ namespace Demo.Game
|
||||
|
||||
public DataType Evaluate(float time)
|
||||
{
|
||||
if (CompiledEntries.Count == 0)
|
||||
if (GetConfig<UpdatementConfig<DataType>>().TimePoints.Length == 0)
|
||||
return default;
|
||||
if (CompiledEntries.Count == 1)
|
||||
return CompiledEntries.Positions[0];
|
||||
if (time < CompiledEntries.TimePoints[0])
|
||||
return CompiledEntries.Positions[0];
|
||||
for (int i = 1; i < CompiledEntries.Count; i++)
|
||||
if (GetConfig<UpdatementConfig<DataType>>().TimePoints.Length == 1)
|
||||
return GetConfig<UpdatementConfig<DataType>>().Positions[0];
|
||||
if (time < GetConfig<UpdatementConfig<DataType>>().TimePoints[0])
|
||||
return GetConfig<UpdatementConfig<DataType>>().Positions[0];
|
||||
for (int i = 1; i < GetConfig<UpdatementConfig<DataType>>().TimePoints.Length; i++)
|
||||
{
|
||||
if (CompiledEntries.TimePoints[i - 1] <= time && CompiledEntries.TimePoints[i] > time)
|
||||
if (GetConfig<UpdatementConfig<DataType>>().TimePoints[i - 1] <= time && GetConfig<UpdatementConfig<DataType>>().TimePoints[i] > time)
|
||||
{
|
||||
return Lerp(CompiledEntries.Positions[i - 1], CompiledEntries.Positions[i],
|
||||
(time - CompiledEntries.TimePoints[i - 1]) / (CompiledEntries.TimePoints[i] - CompiledEntries.TimePoints[i - 1]));
|
||||
return Lerp(GetConfig<UpdatementConfig<DataType>>().Positions[i - 1], GetConfig<UpdatementConfig<DataType>>().Positions[i],
|
||||
(time - GetConfig<UpdatementConfig<DataType>>().TimePoints[i - 1]) /
|
||||
(GetConfig<UpdatementConfig<DataType>>().TimePoints[i] - GetConfig<UpdatementConfig<DataType>>().TimePoints[i - 1]));
|
||||
}
|
||||
}
|
||||
return CompiledEntries.Positions[^1];
|
||||
return GetConfig<UpdatementConfig<DataType>>().Positions[^1];
|
||||
}
|
||||
|
||||
[Content] public GameObject UpdateTarget;
|
||||
@@ -293,15 +264,20 @@ namespace Demo.Game
|
||||
/// <param name="item">实例在父类中控制</param>
|
||||
protected override void SetupTimelineItem(TimelineItem item)
|
||||
{
|
||||
if (CompiledEntries.Count == 0)
|
||||
if (GetConfig<UpdatementConfig<DataType>>().TimePoints.Length == 0)
|
||||
return;
|
||||
|
||||
item.SetupDuration(new(CompiledEntries.TimePoints[0], CompiledEntries.TimePoints[^1]), GetTimelineItemColor());
|
||||
item.SetupDuration(new(GetConfig<UpdatementConfig<DataType>>().TimePoints[0], GetConfig<UpdatementConfig<DataType>>().TimePoints[^1]), GetTimelineItemColor());
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
CompiledEntries.Dispose();
|
||||
if (GetConfig<UpdatementConfig<DataType>>().TimePoints.IsCreated)
|
||||
GetConfig<UpdatementConfig<DataType>>().TimePoints.Dispose();
|
||||
if (GetConfig<UpdatementConfig<DataType>>().Positions.IsCreated)
|
||||
GetConfig<UpdatementConfig<DataType>>().Positions.Dispose();
|
||||
if (GetConfig<UpdatementConfig<DataType>>().EaseCurveTypes.IsCreated)
|
||||
GetConfig<UpdatementConfig<DataType>>().EaseCurveTypes.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user