- Home /
static batching on dynamically instantiated content
I generate lots of identical geometry on runtime with Instantiate() (from two static prefabs atm) but the renderer does not use static batching although static batching is enabled in the renderer settings. using unity pro / deferred rendering path.
do i have to make additional steps (other then marking the prefab static) to enable batching.
the user should be able to add additional instances of the prefabs and also destroy some of them. would this even work with batching?
edit: non of the objects are animated or moved just plain static meshes with identical materials
Answer by Julien-Lynge · Aug 17, 2011 at 04:06 PM
To enable static batching on instantiated objects, you need to use the static batching utility. The way I would suggest doing it (which as worked for me) is to add the GameObject reference returned by the Instantiate function to an array and pass that array to the combine function.
Here's some code to explain what I mean:
GameObject[] objCreated = new GameObject[10] for (int i = 0; i < 10; i++) objCreated[i] = Object.Instantiate(blah);
StaticBatchingUtility.Combine(objCreated, someOtherObjectForTheRoot);
As for adding and removing objects, I don't see why not. You'll just run the StaticBatchingUtility again for the new objects created (you don't have to re-pass the original objects, it will add the new ones to any existing ones).
@Julien.Lynge so you can unload/load asset bundles that you want to batch and then just run the StaticBatchingUtility? And the unloaded ones are just realized to be unloaded? Thanks
@Julien.Lynge
void Start() { var gos = new GameObject[10]; for (int i = 0; i < 10; i++) { var go = Instantiate(Prefab) as GameObject; go.transform.parent = transform; go.transform.position = new Vector3(Random.Range(-10, 10.0f), Random.Range(-10, 10.0f), Random.Range(-10, 10.0f)); gos[i] = go; }
StaticBatchingUtility.Combine(gos, Root.gameObject);
}
player setting with static batching on. Prefab is static.
not work.
Answer by OhNoDinos · Mar 26, 2014 at 07:15 AM
This is such a huge problem, batching in Unity is way too finicky.
You're right about objects instantiated in script posing a problem. No idea why, but I found a workaround on the forums!
gameObject.SetActive( false );
gameObject.SetActive( true );
When you create the objects in your code, set their parent to an any gameobject. Put a script on that gameobject, and in the Start() function, use the code written above. All it does is quickly turn all your objects off and then back on, but for some reason this works! It refreshes your scene, and suddenly batching behaves as intended again.
Using deferred lighting poses a bunch of new obstacles, but I just recently managed to get it working as well by setting all the objects I wanted batched to static, as well as turning off light-maps for the scene, and not using any transparent materials.
Hope this helps anyone else who's having trouble with this!
This is not working for me. I got a prefab set up so it worked with batching, if added to the scene in the editor.
Then I set up a script on a parent game objects: Instantiate(the_prefab, this.transform, true); this.gameObject.SetActive(false); this.gameObject.SetActive(true);
Dynamic batching did not work on the dynamically loaded prefab.
I am using deferred rendering too. Also did all the above things this post said. Not working.
If you actually got this working in the latest version of Unity, please make a working demo project and link to it.
Answer by loopyllama · May 18, 2011 at 07:06 AM
you need to have the same material on objects that you wish to have batched and they need to not move. from the look of your scene, I am betting that you have animation. static batching only works on static things.
No animations. No movement. Just static meshes. 2 prefabs with 2 seperate $$anonymous$$aterials. So 500 instances of material one and 500 instances of material two in this example.
click on one of your instances at runtime. make sure the material is shared and not duplicated, make sure the static box is checkmarked, then report back here...
Answer by jsr2k1 · Jun 09, 2011 at 04:16 PM
I have the same problem. When I add objects to the editor and run the batching works correctly, but when I use my script for instancing objects then there is no batching at all.
Any solution? Someone said that it's related to shader, any idea?
Joel