Search Unity

Question Set object static after instantiated

Discussion in 'Scripting' started by shuskry, Mar 29, 2023.

  1. shuskry

    shuskry

    Joined:
    Oct 10, 2015
    Posts:
    462
    Hi everybody :D

    i'm trying to make a mobile game where the player can place some objects to decorate a room.

    There is a way to set a object "Static" like in the editor ? and re-actualize/calculate the static batching by scripting?

    I imagine if there is 50 object added, or loaded , all these objects will increase draw calls drasticaly no?

    There is a other way to do that ?


    Have a good day :D Thansk for helping me!
     
    Last edited: Mar 29, 2023
    Bunny83 likes this.
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    451
    Setting something to being static - especially for batching - is an editor thing.
    Your added Objects are subject to dynamic batching, which happens automatically, as long Materials are shared. This is what you should optimize for (using a texture atlas and using sharedMaterial instead of material when setting up via script). See here -> Unity - Manual: Dynamic batching (unity3d.com)
     
    shuskry likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    Yes, there is :) It's called StaticBatchingUtility. Just call the Combine method on the parent object. It will combine all child objects which share the same material. Once batched you can no longer change the individual object positions, but you can move the parent around.
     
    Last edited: Mar 30, 2023
  4. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Alternatively, you can have two versions of the object. One that's not static that the player can move around, and one that's static that replaces it once the player places it. If they click on the object to move it again, just replace it again so that they're dragging a non-static object. Also, static transforms can be relocated via script; they just can't be dragged around or move.
     
    shuskry likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    Not sure what you mean by that. If an child object is statically batched, it can not be moved at all. Of course you can more the object around, but it's mesh would not move along since it has been batched into a single mesh on the parent.
     
  6. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Perhaps I'm misunderstanding something about how static batching works. I have a game in which I use procedural generation with static objects. I instantiate a pool of the objects into an inactive transform off-screen at a location far away from the play field. They can't be moved manually, however, when I need one of them to appear off-screen just ahead of the player, I move it there by script. It has a mesh filter, mesh renderer, and colliders on it. The object shows up at the new position and its colliders also function at the new position. Once the player has moved past it and it's off-screen, I move it back to its home position far from the play field to be used again when called on. Since this is possible, my assumption has always been that when I move them via script, they un-batch for the move and then re-batch once placed in the new position. If they are not un-batching and re-batching, how is it possible that they are visible and their colliders still function after the move, and they can do this repeatedly?
     
    shuskry likes this.
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    There are two ways to benefit from static batching:
    • You can mark objects in the scene static (static batching) but that only works for objects authored in the editor and saved into a scene. The batching will take place when the game is build / started.
    • When you instantiate objects at runtime, it doesn't matter if they have the static checkbox checked or not. They won't be statically batched at all unless you use the StaticBatchingUtilty
    It's well explained in the manual on static batching:

    ps: you can not "un-batch" objects. Your only option here would be to completely destroy the parent and the children and recreate them.
     
    akareactor, shuskry and QuinnWinters like this.
  8. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Thanks, somehow I missed that in the documentation. I appreciate the info.
     
    fangye_Studio and Bunny83 like this.
  9. shuskry

    shuskry

    Joined:
    Oct 10, 2015
    Posts:
    462
    Thanks for all your answers :D!
    I can batch objects during runtime with your help ^^

    Have a good day :)