using System.Collections.Generic; using SoftCircuits.Collections; using UnityEngine; namespace SLSFramework.General { public static class OrderedDictionaryExtension { /// /// 根据优先级将元素插入OrderedDictionary中对应的位置,保持字典按优先级从高到低排序。 /// public static void InsertByPriority(this OrderedDictionary 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 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); } /// /// 将字典中的元素根据优先级重新排序,保持字典按优先级从高到低排序。 /// public static void Sort(this OrderedDictionary dictionary) where T : IPrioritized { List> sortedItems = new List>(dictionary); sortedItems.Sort((pairA, pairB) => pairA.Value.CompareTo(pairB.Value)); dictionary.Clear(); foreach (KeyValuePair item in sortedItems) { dictionary.Add(item.Key, item.Value); } } } }