Get vertices a material is assigned to

As the title states I am after a solution on how i can get all the vertices that a material is assigned to. This because I want to be able to change the vertex colors just for that part. The reason being is that I’m creating a mobile game with cars, where I only want 1 draw call for the body, but i still want the ability to change color of the car at runtime, except windows, lights, grill etc (so I can’t use a shader with a main color, and i don’t want 2 materials for the body). So if I somehow can save a list with these vertices would be great.

Thanks in advance

Your question relates to sub meshes…

In order to have multiple materials, your mesh will have multiple sub-meshes - one for each material. On that basis, you could use the function GetTriangles to obtain all the triangles for a given sub-mesh. You would then loop through and identify all unique vertex indices used within the array.

See Unity - Scripting API: Mesh.GetTriangles for more information

TY for the help, here is a working solution

using UnityEngine;
using System.Collections;

public class ChangeColor : MonoBehaviour {

	// This code replaces the indexed submesh and corresponding material color to a vertex color. Then it combines all materials to a single material (Only tested for 2 materials)

	Color newColor;
	public Shader vertexColoredShader;
	public Texture lightmap;
	public int subMeshNumber = 1;

	// Use this for initialization
	void Start () {
		Mesh mesh = GetComponent<MeshFilter>().mesh;

		newColor = renderer.materials[subMeshNumber].GetColor("_Color");

		Debug.Log("Submeshes: " + mesh.subMeshCount);
		int[] t = mesh.GetTriangles(subMeshNumber);
		Vector3[] vertices = mesh.vertices;
		Color[] oldColors = mesh.colors;
		Color[] colors = new Color[vertices.Length];
		for (int i = 0; i < vertices.Length; i++)

			bool foundSubMeshV = false;

			foreach (int index in t)

				if (i == index)
					colors *= newColor;*
  •  			foundSubMeshV = true;*
  •  		}*
  •  	}*
  •  	if(foundSubMeshV)*
  •  		continue;*

colors = oldColors*;*
* }*

* mesh.colors = colors;*
* mesh.triangles = mesh.triangles;*

* DestroyImmediate(renderer);*
* gameObject.AddComponent();*

* Material newMat = new Material (vertexColoredShader);*
* renderer.material = newMat;*
* if(lightmap)*
* renderer.material.mainTexture = lightmap;*

* }*