[Unity3D]InvalidOperationException: out of sync

详细信息:
InvalidOperationExceptio n: out of sync
System.Collections.Generic.Dictionary`2+Enumerator[UnityEngine.KeyCode,System.Boolean].VerifyState ()
System.Collections.Generic.Dictionary`2+Enumerator[UnityEngine.KeyCode,System.Boolean].MoveNext ()
System.Collections.Generic.Dictionary`2+KeyCollection+Enumerator[UnityEngine.KeyCode,System.Boolean].MoveNext ()
InputManager.CheckControleKyes () 
MMOController.FixedUpdate () 


从信息上看来,是用了DICTIONNARY的问题。经过查阅资料,发现问题出在迭代修改DICTIONARY的地方:

foreach (string key in dict.Keys)
{
        temp = dictAssetBundleRefs.TryGetValue(key, out value);
        修改temp的值
        或者删除temp
}

不可以迭代去修改dictionnary,正如我看到一个帖子写的:

You are modifying the dictionary while iterating over it. This is a big no-no.

批量修改或删除应当先将其作为BUFFER引出来再改,然后在一个个删除。

var buffer = new List<KeyKode>(Controlekeys.Keys); 
foreach(var key in buffer)
{
//修改你想要修改的东西
}


===================我直接获取引用来删除:如下记录=========================
AssetBundleRef[] abRefs = new AssetBundleRef[dictAssetBundleRefs.Count];
int i = 0;
foreach (string key in dictAssetBundleRefs.Keys)
{
         dictAssetBundleRefs.TryGetValue(key, out abRefs[i]);
         i++;  
}
i--;
for (int j = 0; j < abRefs.Length; j++)
{
         abRefs[j].assetBundle.Unload(false);
}

你可能感兴趣的:([Unity3D]InvalidOperationException: out of sync)