Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by pragmascript · May 18, 2011 at 01:41 AM · instantiatestatic-batching

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

alt text

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Julien-Lynge · Aug 19, 2011 at 04:52 AM 1
Share

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).

avatar image bpears · Sep 26, 2013 at 11:51 PM 0
Share

@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

avatar image lizs4ever · Jul 23, 2015 at 10:44 AM 1
Share

@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.

avatar image
1

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harinezumi · Aug 02, 2018 at 09:35 AM 0
Share

Can someone confirm if this really works?

avatar image kavika · May 31, 2019 at 09:48 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pragmascript · May 19, 2011 at 07:19 AM 0
Share

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.

avatar image loopyllama · May 19, 2011 at 08:39 AM 0
Share

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...

avatar image pragmascript · May 19, 2011 at 09:50 AM 0
Share

material is shared and static box is ticked.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

Photon unity issues 2 Answers

setting variables on a clone's component instead of the prefab? 1 Answer

Spawn from array 2 Answers

Instantiate and Scaling 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges