From 6992d12c6cd7b4358e526ef00ff545f542be446e Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Thu, 26 Oct 2023 13:12:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BE=AA=E7=8E=AF=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E4=B8=BB=E5=8A=A8GetItemByIndex=E5=92=8CGetItemList?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复循环列表主动GetItemByIndex和GetItemList的问题 --- .../GameLogic/Common/UI/Widget/UIListBase.cs | 2 +- .../Common/UI/Widget/UILoopGridWidget.cs | 15 ++- .../Common/UI/Widget/UILoopListViewWidget.cs | 11 +- .../Common/UI/Widget/UILoopListWidget.cs | 19 ++- .../DataStruct/GameFrameworkDictionary.cs | 120 ++++++++++++++++++ .../GameFrameworkDictionary.cs.meta | 3 + 6 files changed, 159 insertions(+), 11 deletions(-) create mode 100644 UnityProject/Assets/TEngine/Runtime/Core/DataStruct/GameFrameworkDictionary.cs create mode 100644 UnityProject/Assets/TEngine/Runtime/Core/DataStruct/GameFrameworkDictionary.cs.meta diff --git a/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UIListBase.cs b/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UIListBase.cs index c3ca2042..8a9773a5 100644 --- a/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UIListBase.cs +++ b/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UIListBase.cs @@ -277,7 +277,7 @@ namespace GameLogic item.SetSelected(false); } - item = GetItem(m_selectIndex) as IListSelectItem; + item = GetItem(selectIndex) as IListSelectItem; if (item != null) { item.SetSelected(true); diff --git a/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UILoopGridWidget.cs b/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UILoopGridWidget.cs index 423b9de7..381dd406 100644 --- a/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UILoopGridWidget.cs +++ b/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UILoopGridWidget.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using TEngine; using UnityEngine; namespace GameLogic @@ -17,7 +18,7 @@ namespace GameLogic /// /// Item字典 /// - private Dictionary m_itemCache = new Dictionary(); + private GameFrameworkDictionary m_itemCache = new GameFrameworkDictionary(); /// /// 计算偏差后的ItemList @@ -163,9 +164,19 @@ namespace GameLogic m_items.Clear(); for (int i = 0; i < m_itemCache.Count; i++) { - m_items.Add(m_itemCache[i]); + m_items.Add(m_itemCache.GetValueByIndex(i)); } return m_items; } + + /// + /// 获取Item。 + /// + /// 索引。 + /// TItem。 + public TItem GetItemByIndex(int index) + { + return m_itemCache.GetValueByIndex(index); + } } } diff --git a/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UILoopListViewWidget.cs b/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UILoopListViewWidget.cs index e2c1f3ae..c20af44d 100644 --- a/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UILoopListViewWidget.cs +++ b/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UILoopListViewWidget.cs @@ -8,7 +8,7 @@ namespace GameLogic { public LoopListView LoopRectView { private set; get; } - private Dictionary m_itemCache = new Dictionary(); + private GameFrameworkDictionary m_itemCache = new GameFrameworkDictionary(); public override void BindMemberProperty() { @@ -71,9 +71,8 @@ namespace GameLogic List list = new List(); for (int i = 0; i < m_itemCache.Count; i++) { - list.Add(m_itemCache[i]); + list.Add(m_itemCache.GetValueByIndex(i)); } - return list; } @@ -85,11 +84,11 @@ namespace GameLogic /// /// 获取Item。 /// - /// - /// + /// 索引。 + /// TItem。 public T GetItemByIndex(int index) { - return m_itemCache[index]; + return m_itemCache.GetValueByIndex(index); } } } \ No newline at end of file diff --git a/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UILoopListWidget.cs b/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UILoopListWidget.cs index a3e29c34..db7bea3e 100644 --- a/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UILoopListWidget.cs +++ b/UnityProject/Assets/GameScripts/HotFix/GameLogic/Common/UI/Widget/UILoopListWidget.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using TEngine; using UnityEngine; namespace GameLogic @@ -16,8 +17,9 @@ namespace GameLogic /// /// Item字典 + /// Key => GameObjectHashCode | Value => TItem. /// - private Dictionary m_itemCache = new Dictionary(); + private GameFrameworkDictionary m_itemCache = new GameFrameworkDictionary(); /// /// 计算偏差后的ItemList @@ -159,7 +161,10 @@ namespace GameLogic public List GetItemList() { m_items.Clear(); - m_items.AddRange(m_itemCache.Values); + for (int i = 0; i < m_itemCache.Count; i++) + { + m_items.Add(m_itemCache.GetValueByIndex(i)); + } return m_items; } @@ -171,5 +176,15 @@ namespace GameLogic { return LoopRectView.GetItemStartIndex(); } + + /// + /// 获取Item。 + /// + /// 索引。 + /// TItem。 + public TItem GetItemByIndex(int index) + { + return m_itemCache.GetValueByIndex(index); + } } } diff --git a/UnityProject/Assets/TEngine/Runtime/Core/DataStruct/GameFrameworkDictionary.cs b/UnityProject/Assets/TEngine/Runtime/Core/DataStruct/GameFrameworkDictionary.cs new file mode 100644 index 00000000..cb5e8a03 --- /dev/null +++ b/UnityProject/Assets/TEngine/Runtime/Core/DataStruct/GameFrameworkDictionary.cs @@ -0,0 +1,120 @@ +using System.Collections.Generic; + +namespace TEngine +{ + /// + /// 游戏框架字典类。 + /// + /// 指定字典Key的元素类型。 + /// 指定字典Value的元素类型。 + public class GameFrameworkDictionary + { + protected readonly List KeyList = new List(); + protected readonly Dictionary Dictionary = new Dictionary(); + + /// + /// 存储键的列表。 + /// + public List Keys => KeyList; + + /// + /// 存储字典实例。 + /// + public int Count => KeyList.Count; + + /// + /// 通过KEY的数组下标获取元素。 + /// + /// 下标。 + /// TValue。 + public TValue GetValueByIndex(int index) + { + return Dictionary[KeyList[index]]; + } + + /// + /// 通过KEY的数组下标设置元素。 + /// + /// 下标。 + /// TValue。 + public void SetValue(int index, TValue item) + { + Dictionary[KeyList[index]] = item; + } + + /// + /// 字典索引器。 + /// + /// TKey。 + public TValue this[TKey key] + { + get => Dictionary[key]; + set + { + if (!ContainsKey(key)) + { + Add(key, value); + } + else + { + Dictionary[key] = value; + } + } + } + + /// Removes all keys and values from the . + public void Clear() + { + KeyList.Clear(); + Dictionary.Clear(); + } + + /// Adds the specified key and value to the dictionary. + /// The key of the element to add. + /// The value of the element to add. The value can be for reference types. + public virtual void Add(TKey key, TValue item) + { + KeyList.Add(key); + Dictionary.Add(key, item); + } + + /// Gets the value associated with the specified key. + /// The key of the value to get. + /// When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the parameter. This parameter is passed uninitialized. + public bool TryGetValue(TKey key, out TValue value) + { + return Dictionary.TryGetValue(key, out value); + } + + /// Determines whether the contains the specified key. + /// The key to locate in the + public bool ContainsKey(TKey key) + { + return Dictionary.ContainsKey(key); + } + + public TKey GetKey(int index) + { + return KeyList[index]; + } + + public bool Remove(TKey key) + { + return KeyList.Remove(key) && Dictionary.Remove(key); + } + } + + /// + /// 游戏框架顺序字典类。 + /// + /// 指定字典Key的元素类型。 + /// 指定字典Value的元素类型。 + public class GameFrameworkSortedDictionary : GameFrameworkDictionary + { + public override void Add(TKey key, TValue item) + { + base.Add(key, item); + KeyList.Sort(); + } + } +} \ No newline at end of file diff --git a/UnityProject/Assets/TEngine/Runtime/Core/DataStruct/GameFrameworkDictionary.cs.meta b/UnityProject/Assets/TEngine/Runtime/Core/DataStruct/GameFrameworkDictionary.cs.meta new file mode 100644 index 00000000..74d41812 --- /dev/null +++ b/UnityProject/Assets/TEngine/Runtime/Core/DataStruct/GameFrameworkDictionary.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3df785fede30420dbfa6acb2ff48c166 +timeCreated: 1698296076 \ No newline at end of file