Unity报错 Unity报错InvalidOperationException: out of sync的解决
Czhenya 人气:3Unity 报错 之 InvalidOperationException: out of sync
报错原文:
InvalidOperationException: out of sync
System.Collections.Generic.Dictionary2+Enumerator[System.Int32,UnityEngine.Transform].VerifyState () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:912) System.Collections.Generic.Dictionary2+Enumerator[System.Int32,UnityEngine.Transform].MoveNext () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:278)
System.Collections.Generic.Dictionary`2+KeyCollection+Enumerator[System.Int32,UnityEngine.Transform].MoveNext () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:1028)
BigHandCard+c__Iterator6.MoveNext () (at Assets/Scripts/Public/HandCards.cs:781)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
在网上查了下是在迭代器中直接修改引起的。c#是不允许你在迭代器中直接修改。
大概是这个意思【这是个错误示范
public void ShowMyCard(int[] card) { if (myCardsDic.Count > 0) { foreach (int k in myCardsDic.Keys) { if(满足一个条件) { //删除或者修改此元素 //myCardsDic.Remove(k); } } } }
若有这样的需求,应该这样写,【大致逻辑:遍历字典存储满足条件的元素,然后操作你刚存储的元素】
public void ShowMyCard(int[] card) { if (myCardsDic.Count > 0) { List<int> myCardsList = new List<int>(); foreach (int k in myCardsDic.Keys) { if(满足一个条件) { //保存这个元素 myCardsList.Add(myCardsDic[k]); } } foreach (int item in myCardsList) { //做你要做的事情 } } }
而我遇到的问题,和上面的问题不太一样
我的问题:是在遍历字典的时候使用了协程,然后有其他的情况下调用了这个字典,从而导致了上面的这个错误的产生,报错代码大致如下:
public IEnumerator ShowMyCard(int[] card) { if (myCardsDic.Count > 0) { foreach (int k in myCardsDic.Keys) { float x = myCardsDic[k].localScale.x; myCardsDic[k].DOScaleX(0, 0.02f).OnComplete(() => { myCardsDic[k].DOScaleX(x, 0.02f); }); yield return new WaitForFixedUpdate(); } } }
我遇到问题的解决办法:在使用过程中不使用协程的方式去操作,发现效果也还不错,修改代码如下:
public void ShowMyCard(int[] card) { if (myCardsDic.Count > 0) { foreach (int k in myCardsDic.Keys) { float x = myCardsDic[k].localScale.x; myCardsDic[k].DOScaleX(0, 0.02f).OnComplete(() => { myCardsDic[k].DOScaleX(x, 0.02f); }); } } }
You are modifying the dictionary while iterating over it. This is a big no-no.
您正在修改字典,同时遍历它。这是一个大大的禁忌。
加载全部内容