lib-unity-tutorial-m2h-2-Assets-Standard-Assets-Scripts-CombineChildren.cs / cs
using UnityEngine; using System.Collections; /* Attach this script as a parent to some game objects. The script will then combine the meshes at startup. This is useful as a performance optimization since it is faster to render one big mesh than many small meshes. See the docs on graphics performance optimization for more info. Different materials will cause multiple meshes to be created, thus it is useful to share as many textures/material as you can. */ [AddComponentMenu("Mesh/Combine Children")] public class CombineChildren : MonoBehaviour {
// We have a maximum of one material, so just attach the mesh to our own game object if (materialToMesh.Count == 1) { // Make sure we have a mesh filter & renderer if (GetComponent(typeof(MeshFilter)) == null) gameObject.AddComponent(typeof(MeshFilter)); if (!GetComponent("MeshRenderer")) gameObject.AddComponent("MeshRenderer"); MeshFilter filter = (MeshFilter)GetComponent(typeof(MeshFilter)); filter.mesh = MeshCombineUtility.Combine(instances, generateTriangleStrips); renderer.material = (Material)de.Key; renderer.enabled = true; } // We have multiple materials to take care of, build one mesh / gameobject for each material // and parent it to this object else { GameObject go = new GameObject("Combined mesh"); go.transform.parent = transform; go.transform.localScale = Vector3.one; go.transform.localRotation = Quaternion.identity; go.transform.localPosition = Vector3.zero; go.AddComponent(typeof(MeshFilter)); go.AddComponent("MeshRenderer"); go.renderer.material = (Material)de.Key; MeshFilter filter = (MeshFilter)go.GetComponent(typeof(MeshFilter)); filter.mesh = MeshCombineUtility.Combine(instances, generateTriangleStrips); } } } }
(C) Æliens 04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.