- Home /
batch textures, shared materials, instantiation, sharedmaterial array
how do you batch textures on game objects in JavaScript? say you have GameObject cubePrefab and 9 textures to apply to it as shared material-
Answer by MountDoomTeam · Oct 15, 2012 at 09:21 AM
this took me about 4 hours to find out as i am noob, I couldn't seem to find an answer for it and it's pretty unintuitive so here goes:
declare global variables
var mymaterials = new Material[9];
var sw1 : UnityEngine.Texture;//do this for 9 textures
in start function fill the multi-material array with different materials and give them the texture:
var mat1 = cubePrefab.renderer.material;
mymaterials[0] = cubePrefab.renderer.material;
mymaterials[1] = new Material(mat1);
mymaterials[2] = new Material(mat1);
mymaterials[3] = new Material(mat1);
mymaterials[4] = new Material(mat1);
mymaterials[5] = new Material(mat1);
mymaterials[6] = new Material(mat1);
mymaterials[7] = new Material(mat1);
mymaterials[8] = new Material(mat1);
mymaterials[0].mainTexture = sw1;
mymaterials[1].mainTexture = su1;
mymaterials[2].mainTexture = sd1;
mymaterials[3].mainTexture = sw2;
mymaterials[4].mainTexture = su2;
mymaterials[5].mainTexture = sd2;
mymaterials[6].mainTexture = sw3;
mymaterials[7].mainTexture = su3;
mymaterials[8].mainTexture = sd3;
that's all you have to do! Afterwards, when you have a an instantiated prefab and you want to apply different batched textures to it do-
stpc.renderer.material = mymaterials[1];
stpc.renderer.material = mymaterials[9];
EDIT- draw calls 10, batched 468, FPS=120 :D
If you want to batch texture, I strongly suggest to use renderer.shared$$anonymous$$aterial.
Anyway your code does not batch anything at all. It just creates references to materials.
I Does!!!!! i was dubious at first, I am testing it on a map, for example just now i have 10 draw calls, 468 batched :D. i just tried shared$$anonymous$$aterial on the last two lines abive, it does the same draw calls. it's using 6 for a skybox and 4 for the map walls..
One thing that was a major fault though, is that when i did an array of 12 materials mymaterials[12] i got a major error whereas mymaterials[9] worked fine, so there is a limit of 9/10 materials for 1 gameobject.
It only works because your must have enabled dynamic batching. But your script is not the cause of it.
with either material or shared$$anonymous$$aterial, the combineChildren.cs script was able to merge hundreds of models into only one model per material, so it seems it re-uses the same material by either shared$$anonymous$$aterial and less likely by material, (it combinedchildren with both ) i tried to check if i ticked dynamic batching but i couldnt even find the option any more cos i started the project last year :S
Your answer
Follow this Question
Related Questions
Can you use a texture atlas as a colorpicker? 1 Answer
System.Type does not support slicing 1 Answer
more efficient = gameObject w. Material[5] or 5x GameObject w. material[1] 1 Answer
InvalidCastException error and logic error 1 Answer
Attempting to change SetTexture("_DetailAlbedoMap", randomTexture) In children? 0 Answers