This commit is contained in:
MuHua-123
2025-01-13 16:19:03 +08:00
parent 8f387837db
commit 4f7a5d76ca
42 changed files with 328 additions and 83 deletions
+21
View File
@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class LoopIndexTool {
/// <summary> 头尾循环标准化索引 </summary>
public static Data LoopIndex<Data>(this List<Data> list, int index) {
return list[LoopIndex(index, list.Count)];
}
/// <summary> 头尾循环标准化索引 </summary>
public static Data LoopIndex<Data>(this Data[] array, int index) {
return array[LoopIndex(index, array.Length)];
}
/// <summary> 头尾循环标准化索引 </summary>
public static int LoopIndex(int index, int maxIndex) {
if (maxIndex == 0) { Debug.LogError("错误索引:maxIndex = 0"); return 0; }
if (index < 0) { return LoopIndex(index + maxIndex, maxIndex); }
if (index >= maxIndex) { return LoopIndex(index - maxIndex, maxIndex); }
return index;
}
}