代码 控制unity 暂停 编辑器_Unity3D 报错解决方案及常用功能收集
1.在Dictionary迭代器進(jìn)行修改:
var?_key?=?index2factionDic.Keys.GetEnumerator();
stringitem;
while(_key.MoveNext())
{
item?=?index2factionDic[_key.Current];
if(item.Equals(id))
{
tempDelete.Add(_key.Current);
//index2factionDic.Remove(_key.Current);
}
}此時(shí)會報(bào)錯(cuò):
InvalidOperationException:?out?of?sync報(bào)錯(cuò)原因:
在迭代過程中,Dictionary 變量及Value是只讀的,C#有保護(hù)機(jī)制,不允許在這個(gè)過程中修改這些變量。
解決方案:
創(chuàng)建一個(gè)列表List,來存儲需要進(jìn)行操作的key,然后在迭代器之外執(zhí)行操作而不直接操作迭代器,便可解決報(bào)錯(cuò)問題:
List?tempDelete?=newList();
//Index發(fā)生變化,移除舊的索引
if(index2factionDic.ContainsValue(id))
{
var?_key?=?index2factionDic.Keys.GetEnumerator();
stringitem;
while(_key.MoveNext())
{
item?=?index2factionDic[_key.Current];
if(item.Equals(id))
{
tempDelete.Add(_key.Current);
//index2factionDic.Remove(_key.Current);
}
}
}
for(inti?=?0;?i?
{
index2factionDic.Remove(tempDelete[i]);
}
tempDelete.Clear();
2.UITable排序問題:
一般將Item添加到UITable的子節(jié)點(diǎn)上之后,給每個(gè)Item取名,這里假設(shè)是在一個(gè)List循環(huán)中進(jìn)行添加的,取名根據(jù)循環(huán)指針i來取,例如:
//顯示列表
for(inti?=?0;?i?
{
nameStr?=?"card"i;
Transform?itemtrans?=?cardTable.transform.Find(nameStr);
if(itemtrans?==null)
{
item?=?CreateOneCard(cardTable.transform);
}
else
{
item?=?itemtrans.gameObject;
item.SetActive(true);
}
item.name?=?nameStr;
}
記得勾選UITable排序的Sorted選項(xiàng):
運(yùn)行UITable.Reposition()?方法,會根據(jù)item的名稱后面得序號來進(jìn)行排序,但是有個(gè)問題,假如i大于10,如下圖為生成后在Hierarchy中:
這里我一行顯示5個(gè)Item,按照正常順序應(yīng)該如下:
結(jié)果出現(xiàn)了如下排序穿插:
原因:UITable在進(jìn)行排序的時(shí)候,會從item命名字符串中取第一個(gè)數(shù)字來作為排序參考,所以“Card2”和“Card10”去到的第一個(gè)數(shù)字分別是“2”和“1”,所以“Card2”排到了“Card10”后面。
解決方案:命名小于10的item進(jìn)行修改,“Card2”改成"Card02",修改代碼:
//顯示列表
for(inti?=?0;?i?
{
if(i?
{
nameStr?=?"card0"i;
}
else
{
nameStr?=?"card"i;
}
Transform?itemtrans?=?cardTable.transform.Find(nameStr);
if(itemtrans?==null)
{
item?=?CreateOneCard(cardTable.transform);
}
else
{
item?=?itemtrans.gameObject;
item.SetActive(true);
}
item.name?=?nameStr;
}如此,排序的問題就解決了。
3.每次將預(yù)制拖到Hierachy窗口中就錯(cuò):
“NullReferenceException: Object reference not set to an instance of an object
PVPMatchPlayerScene..ctor ()
UnityEditorInternal.InternalEditorUtility:HierarchyWindowDrag(HierarchyProperty, Boolean, HierarchyDropMode)
UnityEditor.DockArea:OnGUI()”
報(bào)錯(cuò)原因:大致原因是同一個(gè)UI預(yù)制中,物理碰撞框的數(shù)量太多,所以導(dǎo)致繪制報(bào)錯(cuò),但不會影響功能
解決方案:對UI預(yù)制進(jìn)行分割,減少單個(gè)Prefab的碰撞框數(shù)量,盡量降低UI預(yù)制的復(fù)雜度
4.NGUI中,使用UILabel,假如在創(chuàng)建諸如聊天輸出窗口或者展示窗口,希望超出設(shè)定區(qū)域的內(nèi)容,以省略號代替,可以使用一下方式來完成,前提是當(dāng)前的NGUI版本的UILabel提供了Wrap這個(gè)接口。
5.修改粒子特效染色的簡單方法:
///?
///?修改粒子特效染色
///?
///?
///?
publicstaticvoidSetParticleSystemToColor(GameObject?gameObj,?Color?color)
{
var?partSyses?=?gameObj.GetComponentsInChildren(true);
foreach(ParticleSystem?_partSysinpartSyses)
{
_partSys.startColor?=?color;
}
}
6.修改Shader中的指定屬性,例如Tint Color:
通過以下代碼獲取Mesh Renderer組件,然后設(shè)置對應(yīng)的屬性:
MeshRenderer?mesh_renders?=?gameObj.GetComponentsInChildren();
mesh_renders.material.SetColor("_TintColor",selfcolor);
7.在一個(gè)繼承自MonoBehaviour的腳本中調(diào)用gameObject或者transform時(shí),出現(xiàn)了如下錯(cuò)誤:
“NullReferenceException UnityEngine.Component.get_transform()”或者“NullReferenceException UnityEngine.Component.get_gameObject()”,可以先判斷this是否為空再調(diào)用gameObject或者transform,如下:
if(this==null||?gameObject?==null||?transform?==null)
{
return;
}
8.用代碼控制Unity編輯器的運(yùn)行和暫停狀態(tài):
運(yùn)行:UnityEditor.EditorApplication.isPlaying?=?true;
暫停:UnityEditor.EditorApplication.isPaused?=?true;
停止:UnityEditor.EditorApplication.isPlaying?=false;
總結(jié)
以上是生活随笔為你收集整理的代码 控制unity 暂停 编辑器_Unity3D 报错解决方案及常用功能收集的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果手机怎么刷机苹果手机如何在电脑上刷机
- 下一篇: 怎样修改文字文件的格式什么格式的文件可以