• 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
0
Question by darkhog · May 30, 2018 at 07:23 PM · performanceperformance optimizationinstantiate prefab

Why Instantiate is so slow?

 public static void SpawnObject(LevelObject lo, GameObject levelRoot){
 
         //Debug.Log("[" + System.DateTime.Now.ToLongTimeString() + " @ " + System.DateTime.Now.Millisecond +"ms] Spawning object " + Globals.categories[lo.CategoryID].objects[lo.ObjectID].prefabName);
         GameObject go = Instantiate(Globals.EditorObjectCache[Globals.categories[lo.CategoryID].objects[lo.ObjectID].prefabName], new Vector3(lo.posX, lo.posY, lo.posZ),Quaternion.Euler(lo.rotX, lo.rotY, lo.rotZ),levelRoot.transform);
         //Debug.Log("[" + System.DateTime.Now.ToLongTimeString() + " @ " + System.DateTime.Now.Millisecond + "ms] Instantiate done");
         go.transform.localScale = new Vector3(lo.scaleX,lo.scaleY,lo.scaleZ);
         LevelObjectData lod = go.GetComponent<LevelObjectData>();
         lod.setLevelObject(lo);
         
         //TODO: Stop being so humorous in game's code or there will be no humor left for actual game dialogs!
     }

The difference between outputs of the commented debug logs is about 2-3ms and that's instantiate alone. Frankly, the instantiate takes the bulk of time it takes to run this function and unfortunately I haven't been able to optimize it further. The rest of the logic takes 1ms at most. Not very good when said function is a part of custom load routine (for maximum modability, I've developed my own editor and use it instead of Unity's scene editor which is only used for menus) and as such can be called tens of thousands times in a row!

31.05.2018: Updated the function to closely resemble what it does look like now. Yep, definitely Instantiate is to blame, cut other cruft (some functions seemed to be called twice or even thrice), but it still made no difference. Still my level loads in about 1 minute (it has about 10k objects, not counting those that are in the scene to begin with like UIs and managers). Unity definitely needs to improve Instantiate's performance.

Comment
Add comment · Show 5
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 PizzaPie · May 30, 2018 at 08:49 PM 0
Share

For proper results use the System.Diagnostics.Stopwatch class to measure time and log the result afterwards and or the profiler, now you measure the first debug log, which is quite slow, along the creation of the string arguments for both.

avatar image darkhog PizzaPie · May 31, 2018 at 02:43 PM 0
Share

It checks out though, if I multiply the delay in ms by approximation of how many objects are there in the level, then divide number by 1000 to get seconds, I get a value that is in the ballpark of how much it actually takes to run, logs or not.

avatar image PizzaPie darkhog · May 31, 2018 at 03:45 PM 0
Share

$$anonymous$$y point was that you measure the Debug.Log too that way, which is a quite slow function (even more than instantiate), anyway instatiate can be slow but its overhead is directly connected with the Components of the GameObject it creates. Each Component will be initiated meaning more components more overhead, also the Awake() will be invoked on $$anonymous$$onoBehaviours, try to instatiate 10k GameObjects with no Components (only transform) and then instatiate 10k GameObjects with a $$anonymous$$onoBehaviour with a simple Awake() { Debug.Log("aaa"); } the difference will be huge.

avatar image b1gry4n · May 30, 2018 at 10:39 PM 0
Share

my guess is its whatever youre doing with "refresh color/material" assu$$anonymous$$g the way you get your objects prefab isnt the issue. also why arent you just plugging in the level objects pos/rot when you instantiate?

  GameObject go = Instantiate(Globals.EditorObjectCache[Globals.categories[lo.CategoryID].objects[lo.ObjectID].prefabName], lo.position, lo.rotation);

its impossible to help and see what is causing the delay because, if the instantiate is the slow down, you did not provide how you are getting the prefab object. Another test you can do is pass the prefab you want to instantiate into the "spawnobject" function (finding it prior to trying to instantiate and passing it in to this function). that way you could at least narrow it down.

avatar image darkhog b1gry4n · May 31, 2018 at 02:46 PM 0
Share

The cache is a dictionary which is filled by the prefabs on bootup with Resources.Load (was a direct Resources.Load call, but I've found out that it doubles the time to load the level). See the updated function. It's definitely Instantiate.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Unified2000 · May 30, 2018 at 09:38 PM

go.transform.rotation=Quaternion.Euler(lo.rotX,lo.rotY,lo.rotZ);

It's probably the loading of assets into video memory that's slow. If an object is referenced in the scene then Unity will load the object into video memory on startup and so a later instantiate should be much faster.

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 darkhog · May 31, 2018 at 02:41 PM 0
Share

$$anonymous$$aybe. But the objects can't be referenced in the scene to begin with as they're dynamically loaded from Resources on game's boot up, then cached, then instantiated from that. I've already shaved the time by half by caching alone, but obviously a $$anonymous$$ute of waiting isn't acceptable for me. Ideally it would be just few seconds.

avatar image Unified2000 darkhog · May 31, 2018 at 03:33 PM 0
Share

I've done something similar in the past. Another thing to consider is mesh colliders. They're horribly slow to generate and so if you have a mesh collider on your object then that will slow instantiate down considerably. I ended up disabling all the mesh colliders on my objects and then after instantiating them I had a script go through the scene and slowly enable the mesh colliders.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

85 People are following this question.

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

Related Questions

How to implement Jobs?,How can I increase performance with Jobs? 0 Answers

Problem with the performance when activating gameObject 0 Answers

Low Poly Alpha Cutout vs Full Models Performance 1 Answer

Optimization integer parameters with TextMeshPro 0 Answers

More than 20 instances of a certain script cause fps to skyrocket, is there something I'm doing wrong here? 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