Search Unity

Static batching: some questions

Discussion in 'Editor & General Support' started by vigoAtWork, Feb 16, 2018.

  1. vigoAtWork

    vigoAtWork

    Joined:
    Apr 25, 2016
    Posts:
    12
    We're having problems getting static batching to work reliably* (both in Editor-Play Mode and in actual builds), and much of this seems to be related to timing problems. Hence, some questions:

    1) In Play Mode, when exactly are the combined meshes created, and when are they "put in place", replacing the non-combined ones?

    2) For builds, the combined meshes are most likely created during build time, when the "static" flags are still available - but when exactly are they "put in place"?

    3) Does unchecking PlayerSettings/Other Settings/Static Batching disable static batching completely, or does it only disable automatic static batching (i.e. does StaticBatchingUtility still work)?

    4) StaticBatchingUtility allows to set a "staticBatchRoot" (which can be moved at runtime), while combined meshes created via automatic static batching seem to always be rooted at the scene. Is there a way to define "staticBatchRoots" for automatic static batching?

    5) Is automatic static batching restricted to enabled "static" GameObjects**, or does it have similar restrictions?

    6) Does StaticBatchingUtility not work for pre-existing scene objects in general, or does it only fail after some specific time during scene loading and activation?

    Regards, _

    *) we have a scene with multiple "towns", and only one of them is enabled at a given time - for us, static batching only works if we artificially enable all "towns", and fixup at scene start (e.g. disabling scripts, toggling towns off again, and re-enabling scripts). StaticBatchingUtility helps in the editor, but only when called at very specific times (?) - otherwise, if doesn't combine anything. Generally, static batching either works as expected, or not at all.

    **) like StaticBatchingUtility.Combine(staticBatchRoot), which doesn't include any disabled GameObjects in the combined mesh, while StaticBatchingUtility.Combine(gos, staticBatchRoot) does
     
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,821
    I was having the same issue before. And I found that, at least in my case, the meshes needed to be enabled before Unity called any Start() on the MonoBehaviours in the scene. I don't know if this is just a coincidence or what is happening when first entering playmode.
    So in my scene controller script during Awake() I make sure all gameObjects which I want to be statically batched in in the editor are SetActive(true). Then during Start() I go back and set them to their original state. This ensured the gameObjects are active when the Awake() was finished. This seemed to do the trick.


    There's also some other cases where static batching won't work. If gameobjects use different lightprobes, they can't be statically batched.
     
    adasda8 likes this.