Question by
PersianKiller · Dec 28, 2021 at 07:46 AM ·
meshcombine
Combine meshes with different materials
I'm using this code to combine meshes, it works fine but the problem is that it can only take one material for combined meshes, how can I fix this?
using UnityEngine;
using System.Collections;
// Copy meshes from children into the parent's Mesh.
// CombineInstance stores the list of meshes. These are combined
// and assigned to the attached Mesh.
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class yu : MonoBehaviour
{
void Start()
{
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int i = 0;
while (i < meshFilters.Length)
{
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].gameObject.SetActive(false);
i++;
}
GetComponent<MeshFilter>().mesh = new Mesh();
GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
}
}
untitled.png
(25.9 kB)
Comment
Your answer
Follow this Question
Related Questions
How to combine meshes seamlessly? 2 Answers
Is it possible to change a vertex (height) after combining meshes? 1 Answer
Unity shader vertex displace doesn't work 0 Answers
How do I displace mesh texture to always face camera? 0 Answers
How do I generate a grid of random shapes that tile perfectly? 0 Answers