- Home /
How to make sharedMaterial work when I duplicate an object?
Hey Guys! You must have answered several of these questions, but all I want to know is how to make a material on a duplicated object change itself to another material which is on the original object. I am using a script to make a city with buildings of random heights. Here is a code snippet pertaining to setting the sharedMaterial.
for(int x = 0; x < BuildingHeight; x++)
{
var Cube = Instantiate(BuildingTransform) as GameObject;
Cube.transform.position = new Vector3(BuildingPos.x, BuildingPos.y + BuildingScale.y * x * 2 , BuildingPos.z);
Cube.isStatic = true;
// Cube.rigidbody.isKinematic = false;
Cube.tag = "Building";
Cube.transform.parent = transform;
Cube.renderer.sharedMaterial = BuildingTransform.renderer.sharedMaterial;
}
The materials of the others don't change when I change the material of the building transform object. I just need to know if I'm setting the shared material correctly? Can anyone tell me how to implement it for a simple duplicate operation and then for this script? Does sharedMesh come into play here? Thanks.
That doesn't seem to work. There are still too many draw calls, approximately equal to the number of Cubes.
What version of Unity are you using? What material are you using? Is the BuildingTransform object a primitive cube or imported mesh?
Just instantiating the same object should dynamically batch. If you instantiate different prefabs, make sure they reference the same material already. Setting the isStatic flag only works prior to building. If you want to static batch the instantiated objects use the StaticBatchingUtility.
You were saying buildings of different height. Unless you were talking about different prefabs you didn't show you cannot batch them if you don't scale them uniformly. You can however batch buildings with the same uneven scale.
@Jessespike: I'm using 4.6.3f1. $$anonymous$$aterial is a normal material with a mobile/Diffuse Detail shader. The Building Transform is an imported mesh. @hexagonius: So you mean to say that the high number (around 200) will not affect the performance if it batches dynamically? I have set this script to [ExecuteInEdit$$anonymous$$ode]. The city is not built in runtime. But I will try that StaticBatchingUtility though.
Answer by varunvp · Apr 09, 2015 at 11:41 AM
Hey Guys, I used Neodrop's script 'Combine Children Extented' to combine the children into one large mesh. This was able to bring the draw calls to a minimum(around 24 from 200). But be warned, this script will not be able to batch more than 64k vertices. Here is the link: http://forum.unity3d.com/threads/combine-children-extented-sources-to-share.37721/
Thanks Guys!
Your answer
Follow this Question
Related Questions
City making help 6 Answers
Isometric City Building Game 0 Answers
Finding and replacing all "instanced" materials 2 Answers
Spout: shared texture breaks model UVs 0 Answers
cityengine 0 Answers