您好,登錄后才能下訂單哦!
我們接著系列二的問題繼續(xù)講解,系列二中的問題是如果多個材質(zhì)是相同的,它沒有去優(yōu)化,接下來我們將其優(yōu)化一下,首先要找到在哪里去優(yōu)化,我們看下面的for循環(huán)語句也是系列二的代碼,如下:
for (int s = 0; s < meshFilter.sharedMesh.subMeshCount; s++) { int materialArrayIndex = 0; for (materialArrayIndex = 0; materialArrayIndex < materials.Count; materialArrayIndex++) { if (materials[materialArrayIndex] == meshRenderer.sharedMaterials[s]) break; } if (materialArrayIndex == materials.Count) { materials.Add(meshRenderer.sharedMaterials[s]); combineInstanceArrays.Add(new ArrayList()); } CombineInstance combineInstance = new CombineInstance(); combineInstance.transform = meshRenderer.transform.localToWorldMatrix; combineInstance.subMeshIndex = s; combineInstance.mesh = meshFilter.sharedMesh; (combineInstanceArrays[materialArrayIndex] as ArrayList).Add(combineInstance); }
這個代碼里它沒有判斷是否有相同的材質(zhì),接下來我們將其重新修改一下:
for (int s = 0; s < meshFilter.sharedMesh.subMeshCount; s++) { int materialArrayIndex = Contains(materials, meshRenderer.sharedMaterials[s].name); if (materialArrayIndex == -1) { materials.Add(meshRenderer.sharedMaterials[s]); materialArrayIndex = materials.Count - 1; } combineInstanceArrays.Add(new ArrayList()); CombineInstance combineInstance = new CombineInstance(); combineInstance.transform = meshRenderer.transform.localToWorldMatrix; combineInstance.subMeshIndex = s; combineInstance.mesh = meshFilter.sharedMesh; (combineInstanceArrays[materialArrayIndex] as ArrayList).Add(combineInstance); }
我們看到上面有個函數(shù)Contains它就是用于判斷是否有相同的材質(zhì),如果有就作為一張材質(zhì),Contains函數(shù)代碼如下所示:
private int Contains(ArrayList searchList, string searchName) { for (int i = 0; i < searchList.Count; i++) { if (((Material)searchList[i]).name == searchName) { return i; } } return -1; }
作用是用于返回相同材質(zhì)的索引,效果如下:
看上圖紅色的部分,有兩個材質(zhì)是一樣的,材質(zhì)只顯示一個了,將其優(yōu)化了。整個靜態(tài)對象的優(yōu)化就給大家解答完了。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。