unity關於網格合併優化

建個父物體掛載腳本數組

下面對應是子物體不用掛腳本orm

using UnityEngine;
using System.Collections;it

public class CombineMeshes : MonoBehaviour
{
void Start()
{
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];io

MeshRenderer[] meshRenderer = GetComponentsInChildren<MeshRenderer>(); //獲取自身和全部子物體中全部MeshRenderer組件
Material[] mats = new Material[meshRenderer.Length]; //新建材質球數組form

for (int i = 0; i < meshFilters.Length; i++)
{
mats[i] = meshRenderer[i].sharedMaterial; //獲取材質球列表class

combine[i].mesh = meshFilters[i].sharedMesh;
// combine[i].transform= meshFilters[i].transform.localToWorldMatrix; //局部轉世界
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].gameObject.SetActive(false);
}transform

transform.GetComponent<MeshFilter>().mesh = new Mesh();
transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine, false);//爲mesh.CombineMeshes添加一個 false 參數,表示並非合併爲一個網格,而是一個子網格列表腳本

transform.GetComponent<MeshRenderer>().sharedMaterials = mats; //爲合併後的GameObject指定材質vi

transform.gameObject.SetActive(true);
}
}co

相關文章
相關標籤/搜索