Unity关于Dictionary的遍历问题

在字典中我们会调用到循环遍历,当我们用一个自定义字典然后里面存了有复数个数据结构,里面有Update更新方法我们就要在管理类调用,用foreach遍历启动每一个Update,当我们删除字典其中一个的时候就会报错。
因为foreach是个迭代器,循环时不能进行改动。
1.第一个解决方法用一个list在调用Update之前加进去

// 存储所有打开的界面, key-界面名称, value-窗口名称
    private Dictionary<string, BaseWindow> _windows = new Dictionary<string, BaseWindow>();
    List<BaseWindow> windows = new List<BaseWindow>();
 public void Update(float dt)
        //清空列表
        windows.Clear();
        foreach (BaseWindow wnd in _windows.Values)
            if (wnd != null)
                windows.Add(wnd);
        for (int i = 0; i < windows.Count; i++)
            windows[i].Update(dt);

慎用消耗性能过大
2.用for循环遍历字典

using System.Linq;
 for (int i = 0; i < _windows.Count; i++)
           //i为当前字典第i个 即使是string也会有放入顺序
            var item = _windows.ElementAt(i);
            Debug.Log(item.Value.ShowName);
            Debug.Log(item.Value.Name);

3.用List 根据list[i]查找相当于字典的key为int不过用不了string
4.自己封装字典 就是一个字典俩List对应

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
// 主要优化foreach
public class GameMap<TKey, TValue>
    private Dictionary<TKey, TValue> m_Map = new Dictionary<TKey, TValue>();
    public GameMap()
    List<TKey> m_base_key = null;
    List<TValue> m_base_TValue = null;
    public int Count { set; get; }
    public void Add(TKey key, TValue value)
        m_Map.Add(key, value);
        m_base_key = new List<TKey>(m_Map.Keys);
        m_base_TValue = new List<TValue>(m_Map.Values);
        Count = m_Map.Count;
    public void AddOrReplace(TKey key, TValue value)
        m_Map.Remove(key);
        m_Map.Add(key, value);
        m_base_key = new List<TKey>(m_Map.Keys);
        m_base_TValue = new List<TValue>(m_Map.Values);
        Count = m_Map.Count;
    public void TryAdd(TKey key, TValue value)
        if (getDataByKey(key) != null)
            return;
        m_Map.Add(key, value);
        m_base_key = new List<TKey>(m_Map.Keys);
        m_base_TValue = new List<TValue>(m_Map.Values);
        Count = m_Map.Count;
    public bool Remove(TKey key)
        bool ret = m_Map.Remove(key);
        m_base_key = new List<TKey>(m_Map.Keys);
        m_base_TValue = new List<TValue>(m_Map.Values);
        Count = m_Map.Count;
        return ret;
    public TValue getDataByIndex(int index)
        return m_base_TValue[index];
    public TKey getKeyByIndex(int index)
        return m_base_key[index];
    public void Clear()
        m_Map.Clear();
        if (m_base_key != null)
            m_base_key.Clear();
        if (m_base_TValue != null)
            m_base_TValue.Clear();
        Count = m_Map.Count;
    public TValue getDataByKey(TKey key)
        TValue _baseMsg;
        m_Map.TryGetValue(key, out _baseMsg);//  [key];
        return _baseMsg;
    public bool TryGetValue(TKey key, out TValue _baseMsg)
        return m_Map.TryGetValue(key, out _baseMsg);//  [key];
    public bool ContainsKey(TKey key)
        return m_Map.ContainsKey(key);//  [key];

遍历性能对比

using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System;
using System.Linq;
using System.Collections.Generic;
public class TextDicSpeed : MonoBehaviour
    Dictionary<int, int> dic;
    GameMap<int, int> dic1;
    // Use this for initialization
    void Start()
        dic = CreateArr(10000);
        dic1 = CreateArr1(10000);
        int k;
        int v;
        Stopwatch time = new Stopwatch();
        time.Start();
        //foreach (var item in dic) //时间为1
        //    k = item.Key;
        //    v = item.Value;
        //    if (k == 500)
        //    {
        //        //会报错就先不移除了
        //        // dic.Remove(k);
        //    }
        //for (int i = 0; i < dic.Count; i++)//时间为4370
        //    var item = dic.ElementAt(i);
        //    k = item.Key;
        //    v = item.Value;
        //    if (k == 500)
        //    {
        //        dic.Remove(k);
        //    }
        for (int i = 0; i < dic1.Count; i++)//时间为3
            k = dic1.getKeyByIndex(i);
            v = dic1.getDataByIndex(i);
            if (k == 500)
                dic1.Remove(k);
        time.Stop();
        UnityEngine.Debug.Log(time.ElapsedMilliseconds);
    /// <summary>
    /// 创建一个很大的Dic
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public static Dictionary<int, int> CreateArr(int v)
        Dictionary<int, int> arr = new Dictionary<int, int>();
        System.Random r = new System.Random();
        for (int i = 0; i < v; i++)
            arr.Add(i, r.Next(0, v));
        return arr;
    /// <summary>
    /// 创建一个很大的Dic1
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public static GameMap<int, int> CreateArr1(int v)
        GameMap<int, int> arr = new GameMap<int, int>();
        System.Random r = new System.Random();
        for (int i = 0; i < v; i++)
            arr.Add(i, r.Next(0, v));
        return arr;