虽然笔者目前还不知道 BindableDictionary 能用在什么使用场景下,但是还是应童鞋的要求实现了 BindableDictionary。
基本使用如下:
using System.Linq;
using UnityEngine;
namespace QFramework.Example
{
    public class BindableDictionaryExample : MonoBehaviour
    {
        private BindableDictionary<string, string> mDictionary = new BindableDictionary<string, string>();
        private void Start()
        {
            mDictionary.OnCountChanged.Register(count =>
            {
                print("count:" + count);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            mDictionary.OnAdd.Register((key, value) =>
            {
                print("add:" + key + "," + value);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            mDictionary.OnRemove.Register((key, value) =>
            {
                print("remove:" + key + "," + value);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            
            mDictionary.OnReplace.Register((key, oldValue,newValue) =>
            {
                print("replace:" + key + "," + oldValue + "," + newValue);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            mDictionary.OnClear.Register(() =>
            {
                print("clear");
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }
        private string mKeyToDelete = null;
        private void OnGUI()
        {
            IMGUIHelper.SetDesignResolution(640,360);
            
            GUILayout.Label("Count:" + mDictionary.Count);
            GUILayout.BeginVertical("box");
            
            foreach (var kv in mDictionary)
            {
                GUILayout.BeginHorizontal("box");
                GUILayout.Label($"{kv.Key},{kv.Value}");
                if (GUILayout.Button("-"))
                {
                    mKeyToDelete = kv.Key;
                }
                GUILayout.EndHorizontal();
            }
            if (GUILayout.Button("add"))
            {
                var key = "key" + Random.Range(0, 100);
                if (!mDictionary.ContainsKey(key))
                {
                    mDictionary.Add("key" + Random.Range(0,100),"value" + Random.Range(0,100));    
                }
            }
            if (mDictionary.Count > 0)
            {
                if (GUILayout.Button("remove"))
                {
                    mDictionary.Remove(mDictionary.Keys.First());
                }
                if (GUILayout.Button("replace"))
                {
                    mDictionary[mDictionary.Keys.First()] = "replaced value" + Random.Range(0, 100);
                }
                if (GUILayout.Button("clear"))
                {
                    mDictionary.Clear();
                }
            }
            GUILayout.EndVertical();
            
            
            
            
            if (mKeyToDelete.IsNotNullAndEmpty())
            {
                mDictionary.Remove(mKeyToDelete);
                mKeyToDelete = null;
            }
        }
    }
}
运行结果如下:
 
输出结果如下:
 
本文由 《QFramework 教程年会员》赞助,298 元,9 套教程一年内随便看。










![[Linux]基础操作指令](https://img-blog.csdnimg.cn/img_convert/2477bfb7bb1a58399ce078152cc04023.png)


![[深度学习]神经网络](https://i-blog.csdnimg.cn/direct/eb05c3e086784774967e66557c57366b.png)





