无效操作异常:不同步.由在迭代字典时修改字典引起的.除了我没有修改它

本文关键字:修改 字典 除了我 操作 同步 无效 迭代 异常 | 更新日期: 2023-09-27 18:31:48

在这里,我在迭代字典之前复制了字典,它仍然给了我这个错误。如果有人能帮助我,那就太棒了。

IEnumerator HandleStaticObjects()
{
    handleStaticObjectsIsRunning = true;
    List<string> instantiatedObjects = new List<string>();
    Dictionary<string, staticObjectWaitingToBeInstantiated> currentStaticObjectsWaitingToBeInstantiated = staticObjectsWaitingToBeInstantiated;
    foreach (KeyValuePair<string, staticObjectWaitingToBeInstantiated> staticObject in currentStaticObjectsWaitingToBeInstantiated)
    {
        if (staticObject.Value.staticObject == null)
        {
            AssetBundleCreateRequest bundle = AssetBundle.LoadFromFileAsync(@"C:/gameAssets/" + staticObject.Value.name.Split('?')[0] + ".unity3d");
            UnityEngine.Debug.Log("loading " + staticObject.Value.name.Split('?')[0]);
            yield return bundle;
            AssetBundle assetBundle = bundle.assetBundle;
            var assetLoadRequest = assetBundle.LoadAssetAsync<GameObject>(staticObject.Value.name.Split('?')[0]);
            yield return assetLoadRequest;
            staticObject.Value.staticObject = assetLoadRequest.asset as GameObject;
            assetBundle.Unload(false);
        }
        staticObjects[staticObject.Key] = Instantiate(staticObject.Value.staticObject);
        staticObjects[staticObject.Key].transform.eulerAngles = staticObject.Value.eulerAngles;
        staticObjects[staticObject.Key].transform.position = staticObject.Value.position;
        staticObjects[staticObject.Key].transform.localScale = staticObject.Value.localScale;
        if (staticObjects[staticObject.Key].GetComponent<DataHolder>() == null)
        {
            staticObjects[staticObject.Key].AddComponent<DataHolder>();
        }  
        staticObjects[staticObject.Key].GetComponent<DataHolder>().objectName = staticObject.Value.name;
        instantiatedObjects.Add(staticObject.Key);
    }
    foreach (string instantiatedObject in instantiatedObjects)
    {
        staticObjectsWaitingToBeInstantiated.Remove(instantiatedObject);
    }
    handleStaticObjectsIsRunning = false;
}

堆栈跟踪:

InvalidOperationException: out of sync
System.Collections.Generic.Dictionary`2+Enumerator[System.String,CoOrdsHandler+staticObjectWaitingToBeInstantiated].VerifyState () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:912)
System.Collections.Generic.Dictionary`2+Enumerator[System.String,CoOrdsHandler+staticObjectWaitingToBeInstantiated].MoveNext () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:835)
CoOrdsHandler+<HandleStaticObjects>c__Iterator0.MoveNext () (at Assets/Scripts/CoOrdsHandler.cs:342)

无效操作异常:不同步.由在迭代字典时修改字典引起的.除了我没有修改它

抱歉无法发表评论,否则我会发表评论,但您不是在复制,而是创建对原始内容的引用。

改变:

Dictionary<string, staticObjectWaitingToBeInstantiated> currentStaticObjectsWaitingToBeInstantiated = staticObjectsWaitingToBeInstantiated;

Dictionary<string, staticObjectWaitingToBeInstantiated> currentStaticObjectsWaitingToBeInstantiated = new Dictionary<string, staticObjectWaitingToBeInstantiated> (staticObjectsWaitingToBeInstantiated);

如何在 C# 中将词典的内容复制到新词典?