I have fought with this for some time and done lots of googling yet i have failed to succeed in this. In my game the player can create alot of objects at runtime and i want him to be able to change what color the created objects are. Some could be blue, some red, etc. and i had it working in a way that it always created new instance of material, which later on now caused me quite a bottleneck on dynamic batching.
I have created a material/color storage class system and am able to save them there succesfully but i can’t apply them to the objects Renderer.sharedMaterial for some reason (it doesn’t change anything) and if i use Renderer.material it creates new instance. Most likely the problem here is my lack of understanding how the materials/renderer works. Any help appreciated.
Heres the code im running:
private void CreateObject()
{
if (!GetInput.cursorOverHud)
{
foreach (Transform t in Placeholders)
{
Transform o = UnityEngine.Object.Instantiate(t, t.position, t.rotation) as Transform;
foreach (Collider col in o.GetComponents<Collider>())
col.isTrigger = false;
o.gameObject.layer = 9;
o.GetComponent<Rigidbody>().isKinematic = false;
o.GetComponent<DynamicObject>().Set();
Renderer oRend = o.GetComponent<Renderer>();
Renderer tRend = t.GetComponent<Renderer>();
for (int i = 0; i < oRend.sharedMaterials.Length; i++)
{
// Nothing happens
oRend.sharedMaterials <em>= MatColStorage.GetMaterial(tRend, tRend.sharedMaterials<em>.color, i);//mat_;//tRend.sharedMaterials*;*_</em></em>
// But if i were to assign into ‘oRend.materials*’ i get lots of instantiated materials*
}
}
}
}
And the storage class:
public static class MatColStorage
{
private class MaterialColor
{
private Material mat;
private Color col;
public Material material
{
get
{
return mat;
}
}
public Color color
{
get
{
return col;
}
}
public MaterialColor(Material material, Color color)
{
mat = material;
mat.color = color;
col = color;
}
}
private static List instantiatedMaterials = new List();
public static Material GetMaterial(Renderer rend, Color col, int whichMat)
{
foreach(MaterialColor matcol in instantiatedMaterials)
{
if(matcol.color == col)
{
return matcol.material;
}
}
Material refMat = new Material(rend.materials[whichMat]);
refMat.color = col;
instantiatedMaterials.Add(new MaterialColor(refMat, col));
return instantiatedMaterials[instantiatedMaterials.Count - 1].material;
}
}
bump. Any ideas, insight, anything?
– RiQQ92