整合SLSUtilities

This commit is contained in:
SoulliesOfficial
2026-01-17 11:35:49 -05:00
parent d94241f36c
commit 7ee2894a63
1338 changed files with 3051541 additions and 507034 deletions

View File

@@ -0,0 +1,48 @@
using System.Collections.Generic;
using SoftCircuits.Collections;
using UnityEngine;
namespace SLSFramework.General
{
public static class OrderedDictionaryExtension
{
/// <summary>
/// 根据优先级将元素插入OrderedDictionary中对应的位置保持字典按优先级从高到低排序。
/// </summary>
public static void InsertByPriority<T>(this OrderedDictionary<string, T> dictionary, string key, T value) where T : IPrioritized
{
if (dictionary.ContainsKey(key))
{
Debug.LogWarning($"Key '{key}' already exists in the dictionary. Insertion skipped.");
return;
}
foreach (KeyValuePair<string, T> pair in dictionary)
{
if (value.Priority > pair.Value.Priority)
{
int index = dictionary.IndexOf(pair.Key);
dictionary.Insert(index, key, value);
return;
}
}
dictionary.Add(key, value);
}
/// <summary>
/// 将字典中的元素根据优先级重新排序,保持字典按优先级从高到低排序。
/// </summary>
public static void Sort<T>(this OrderedDictionary<string, T> dictionary) where T : IPrioritized
{
List<KeyValuePair<string, T>> sortedItems = new List<KeyValuePair<string, T>>(dictionary);
sortedItems.Sort((pairA, pairB) => pairA.Value.CompareTo(pairB.Value));
dictionary.Clear();
foreach (KeyValuePair<string, T> item in sortedItems)
{
dictionary.Add(item.Key, item.Value);
}
}
}
}