2

I have many gameobjects spawned in my scene. Is there a way to create a mesh to connect all the dots to make a solid mesh? I have researched skinned meshed but i am not sure how to use it for this purpose.

Thank you.

Gameobjects I want to connect together with mesh

2
  • I have no experience in meshes with Unity, but have you checked this? docs.unity3d.com/ScriptReference/Mesh.html I think it could be a good starting point if you populate the vertices array with the center points of your objects. Commented Jul 17, 2018 at 7:31
  • You need to map the spawned units and their position (Vector3) and define which vectors represent a face in order to create the "outline" you need, then you can pass these parameters in the Mesh to create the dynamic mesh like the link describes that Andrea posted. Commented Jul 17, 2018 at 8:18

1 Answer 1

3

You can create one mesh from multiple meshes or GameObjects with the Mesh.CombineMeshes function. First, create a tag named "dots" and make sure those objects have this tag to make them easier to find. Find all the dot GameObjects by tag with GameObject.FindGameObjectsWithTag. Create array of CombineInstance and initialize each CombineInstance with the mesh and transform information of MeshFilter from each dot.

Create new GameObject to hold the new combined Objects then attach MeshFilter and MeshRenderer to it. Apply a material to it. Finally, use MeshFilter.CombineMeshes to combine all those meshes stored in CombineInstance.

void CombineDotMeshes(Material mat)
{
    //Find all the dots GameObjects
    GameObject[] allDots = GameObject.FindGameObjectsWithTag("dots");

    //Create CombineInstance from the amount of dots
    CombineInstance[] cInstance = new CombineInstance[allDots.Length];

    //Initialize CombineInstance from MeshFilter of each dot
    for (int i = 0; i < allDots.Length; i++)
    {
        //Get current Mesh Filter and initialize each CombineInstance 
        MeshFilter cFilter = allDots[i].GetComponent<MeshFilter>();

        //Get each Mesh and position
        cInstance[i].mesh = cFilter.sharedMesh;
        cInstance[i].transform = cFilter.transform.localToWorldMatrix;
        //Hide each MeshFilter or Destroy the GameObject 
        cFilter.gameObject.SetActive(false);
    }

    //Create new GameObject that will contain the new combined Mesh
    GameObject combinedMesh = new GameObject("CombinedDots");
    MeshRenderer mr = combinedMesh.AddComponent<MeshRenderer>();
    mr.material = mat;
    MeshFilter mf = combinedMesh.AddComponent<MeshFilter>();

    //Create new Mesh then combine it
    mf.mesh = new Mesh();
    mf.mesh.CombineMeshes(cInstance);
}

Usage:

public Material mat;

void Start()
{
    CombineDotMeshes(mat);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.