Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to reduce enter Play Mode time?

Discussion in 'General Discussion' started by Gua, Jul 20, 2017.

  1. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    455
    In an almost empty project, when I hit play button I enter play mode almost instantly. Unfortunately this isn't the case for empty scene in my real project. Is there something I can do to reduce time that it takes unity to enter Play Mode?
     
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    Caution: thinking out loud ahead. I didn't see the source code, I have no idea how Unity player is written.

    When you click play everything sill needs to compile to some half-performace debug mode.
    And if you had control over which parts compile (i.e. cutting out pieces of the engine),
    I recon you could speed the build process up a bit.
     
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
  4. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    Try putting your core scripts in Plugins/YourProjectName/ (for runtime stuff) and Plugins/YourProjectName/Editor/. If that's not helping, pre-compile assemblies with code you're not likely to change very often.

    Recent threads on compile time have informed me that there are compiler passes Unity runs through every time you click Play, but the Plugins folder is only compiled when you actually change something. It seems a bit weird that this isn't the case for all scripts, but that's how I read it.
     
  5. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    455
    I've recently moved most of the code into Standard Assets folder, to reduce compilation time. Is Standard Assets folder have same effect on enter Playmode time as Plugins folder? Because for code compilation both of those folders should produce same result.

    http://pekalicious.com/blog/unity3d-reduce-compile-time-with-standard-assets/
    https://docs.unity3d.com/412/Docume...ce/index.Script_compilation_28Advanced29.html
    https://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html
     
  6. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
  7. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    455
    I've seen your post before and I did profile it. But results were too cryptic and google didn't helped me to narrow it.

    When I first enter Playmode it take very long. When I enter it second/third/fourth... time it takes around 10 sec. And I'm mostly focused on reducing time to enter Playmode for the second/third/fourth... time.

    Here's a profiling of second launch.

    Unfortunately I wasn't able to find good info on how to reduce reload assemblies time.
     
    Last edited: Jul 22, 2017
  8. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Try to enable "Deep Profile" in the Profiler window and repeat the profile. It will take a lot longer with Deep Profile, but provides more information. For example, perhaps something in the project uses [InitializeOnLoad] attribute which is called during ReloadAssembly.

    Other than that, ReloadAssembly is really slow and gets slower with the amount of source code files in the project.
     
  9. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    455
    And moving that code into special folders like Plugins doesn't help reduce ReloadAssembly time?
     
  10. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Not that I know of. According to my tests, splitting up code to special folders affects compilation time only.
     
  11. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    591
    Here's a little write up I did of some problems recently introduced and about the general problem of how unity doesn't scale when having many scripts in your project:

    https://forum.unity3d.com/threads/buildpipelineinterfaces-causes-streaming-hickups-5-6.484552/

    That 5 second long AssemblyReload looks really rough @Gua , really hope unity gets to optimizing compilation / playmode someday. The assembly definition files in 2017.2 will help a little for compile times but that's only half the story
     
    hippocoder likes this.
  12. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    If all the tricks do nothing it's possible you have a good test case for a bug report which they can hand over to the script engine team.
     
  13. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    455
    I moved all my plugins from Standard Assets to Plugins. At least in an empty scene, enter Playmode time and script compilation time didn't changed.
     
  14. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    Do you use a Resources folder? If so, is there a lot in it?
     
  15. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    455
    I have 3 textures there, but I do use a large number of plugins and they do use that folder quite often.
     
  16. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    455
    I've copy pasted all folders with name resources. Deleted all *.meta files. Final results.

    Resources size: 24.5 mb
    Number of files in resources: 637 files.
     
  17. Besus

    Besus

    Joined:
    Mar 9, 2014
    Posts:
    226
    I had this happening in a project as well and found it was due to the sprite packing setting. In the Editor settings, check if sprite packing is "Always Enabled" and change it to "Enabled for Builds".
     
    nirvanajie, Num0n1 and Peter77 like this.
  18. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    455
    Thanks for the tip! Unfortunately it didn't not have noticeably impacted on enter playmode time for my project.
     
  19. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    I have massive problems with this. The compile/playmode loop takes far too long.

    I switched to a multi scene composite approach and its even slower now :'(
     
  20. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Do resource folder usages increase time to enter playmode?
     
  21. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Take a look at EnterPlaymode in the Profiler to check if you find many Loading.LockPersistentManager calls. I had this in pretty much every Components' Awake() method, see below:
    EnterPlaymode_LockPersistentManager.png

    I ran into the issue when I switched from a single-scene to multi-scenes approach (to improve EnterPlaymode time :) ) that loading scenes asynchronously took way longer to EnterPlaymode.

    However, it was caused by loading several scenes in parallel. Once I changed this to load scenes sequentially (only one scene can load at a time), it got rid of most Loading.LockPersistentManager calls and improved EnterPlaymode performance significantly.

    Not sure about EnterPlaymode. But it does take the runtime longer to start up as described here.
     
    frosted likes this.
  22. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,943
    Yes.
     
    frosted likes this.
  23. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    Ryiah and frosted like this.
  24. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    AFAIK Resources is a secret asset bundle that gets rebuilt every single time you press play. Unity doesn't want people using Resources folder, so you are going against the engine developer's recommendations.

    It will be in your best interest to engineer the project so you do not use it at all, period. If asset authors are using it then simply remove their assets or change them.
     
    Kiwasi, frosted, Ryiah and 1 other person like this.
  25. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    I created a new project where I added 600 1024x1024 dtx1 textures, 600 text files and 600 wav files to a Resources directory. I created an new scene where I added a component that references one texture, text and audio asset and Resources.Load's another texture, text and audio asset inside its Awake() method.

    I couldn't spot a difference in the Profiler for EnterPlaymode, whether I tested with 10 or 1800 assets in a Resources directory.

    This leads me to the conclusion that the number of assets in a Resources directory does not seem to affect EnterPlaymode.
     
    Last edited: Jul 22, 2017
  26. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Reviewing my usage of the resource folder, the majority of them are in standard assets.

    Another large set comes from the Post Processing stack.

    I won't argue that usage is going against the engine developer's recommendations.

    But the engine developers themselves should start to more rigidly follow their own recommendation.

    It seems to be unity standard practice (by UT themselves) to always include shaders in resources. Why?
     
  27. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    did you test the number of resource folders?
     
  28. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Well, Unity staff said it. It's none of my business if Unity contradicts itself.
     
  29. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    I duplicated the Resources folder 10 times. So I ended up having 18000 assets spread across 11 Resources folders. I couldn't really spot a difference in EnterPlaymode.

    I recorded a video that shows EnterPlaymode with 1800 and 18000 assets. The time being spent in EnterPlaymode is not 100% stable, it had a difference of about 50-100 ms with every time I profiled.



    EDIT: As mentioned a few times already, EnterPlaymode is an editor cost only. However, the amount of files in the Resources folder does affect the application startup time of the Player (a.k.a the built game). It's most likely better to keep the Resources folder small, to avoid that the Player freezes for several seconds after the Unity splash-screen. Don't miss to read the pros and cons in Unity's The Resources folder best practices documentation.
     
    Last edited: Jul 23, 2017
  30. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,624
    I don't know. Unity has been pushing asset bundles for a while (even though they didn't have a decent workflow until recently) and keep saying that the Resources folder is the source of all evil, but... I haven't seen a ton of evidence towards that.

    The closest to demonstrating some benefit is this : https://blogs.unity3d.com/2017/04/12/asset-bundles-vs-resources-a-memory-showdown/

    But... Even that is not very convincing. If you need several paragraphs to explain how asset bundles use less memory (under certain circumstances), I don't see how Resources folder is as bad as they say it is.

    Also, I am not sure Unity themselves know the best practices.

    You know how moving Static Colliders (colliders without rigidbodies) was a big no-no?

    The manual still thinks so : https://docs.unity3d.com/Manual/CollidersOverview.html
    Even though that's complete false? Because, moving static colliders is either much faster (if there aren't any rigidbodies in the scene), or they perform more or less the same. So...

    So, yeah. It'll take a little more than Unity saying the Resources folder is bad to convince me that it's actually bad.
     
    Schneider21 likes this.
  31. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I think there's a current slight perf penalty these days still, would have to benchmark. Honestly, I gave up worrying about what was fast or not, will just find out when it's too late and band aid it.
     
    Kiwasi likes this.
  32. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,624
    We talking colliders? Because I don't think that's true. I haven't tested all and every case, but I haven't yet found a situation where adding a kinematic rigidbody makes anything faster.
     
  33. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Because it will get rebuilt every single time you press play, and all the contents will be loaded into ram, not just what is used. If people put big media in there, it will cost them. I agree, it's super convenient, and Unity needs to figure out why people are allergic to asset bundles. Never touched em myself.

    I read somewhere once a few thousand are involved it matters, I don't recall where but the when was after that blog. Could've been a bug.

    Here, have some ancient boredmormon coolness with some superpig rashers on top: https://forum.unity3d.com/threads/end-of-the-resources-folder.363800/

    @superpig posts:
    I suspect that time is probably drawing near.
     
  34. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,624
    @Peter77 's post above shows that the difference when you press play is within margin of error. And I'm not sure if they're really loaded into ram. I mean, how can I double check this? The memory profiler (this one: https://bitbucket.org/Unity-Technologies/memoryprofiler ), doesn't show them all being loaded into ram.

    I did a test with 2000 colliders, like just now. Also a few months ago. There wasn't a case where adding more kinematic rigidbody colliders was faster.
     
  35. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,624
    I've read that thread in the past and I skimmed it again now.

    Unless I missed something the tl;dr; is. Resources folder is a big asset bundle. And since it's one big bundle, it's a bit slower than multiple small bundles, since it has to build one huge index (and going through a bigger index is slower than going through a smaller index) and...

    That's it?

    I mean okay, I can see how asset bundles can be a little faster, but I don't get why everyone keeps saying "omg the resources folder is the worst, everyone please move to asset bundles whose workflow is a nightmare!" (or at least it was until recently).

    I mean.

    If I take my resources folder, and make it all one big asset bundle and replace all my Resources.Load calls with AssetBundle.LoadFromFile calls, will I see any benefit?

    If not, I still don't see why Resources folder is a terrible thing that should be deprecated and Asset Bundles are god's gift to humanity.
     
    Arkade likes this.
  36. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    522
    @Gua,
    did you fix your issue ?

    If not, I had a similar thing when I started to use Substance textures years ago : they were re-created on each play start and it was slow as hell. Solution : I stopped to use Substance textures as it was only for place holders.

    Maybe you ran in a similar problem...


    @AcidArrow,
    I'm with you on the "Resources folder is slow" silliness. For my game, I put everything I have in it, which is around 700MB in thousand of files, and there's no noticeable slowdown. If anything, my game loads much faster than most Unity games I know likely because I used custom format instead of Unity built-in scenes... :p
     
  37. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    455
    I did not. :(
     
  38. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    I didnt know the Resource folder was a anti pattern, we use it for Static stuff like configurations etc

    Code (CSharp):
    1.  
    2. public class MaterialManager : MonoBehaviour
    3. {
    4.     private static MaterialManager instance;
    5.     public static MaterialManager Instance
    6.     {
    7.         get
    8.         {
    9.             if (!instance)
    10.                 instance = Instantiate(Resources.Load<MaterialManager>("MaterialManager"));
    11.  
    12.             return instance;
    13.         }
    14.     }
    15.    ...
    16. }
    17.  
     
  39. Bip901

    Bip901

    Joined:
    May 18, 2017
    Posts:
    71
    Open the profiler and check what requires the most CPU usage when you press play.
     
  40. Num0n1

    Num0n1

    Joined:
    Aug 19, 2016
    Posts:
    8
    You are such a life saver!
     
  41. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    455
    In Unity 2018.3 in a empty scene it takes around 4 seconds to enter play mode, that's a pretty huge improvement compared to 10 second that it took me in Unity 5. Especially considering that project is even more complex.
     
  42. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    455
    I need to make one clarification. Originally I didn't properly measured the time when game really starts working. While it seemed that in Unity 2018.3 it's much faster, in reality when editor switches to gameview in 2018.3, the editor stays unresponsive for some time after the switch and that switch is what I originally measured. So here's results after proper measurement.

    For empty scene in my big project on Unity 2018.3 it's 8 second.
    For empty scene in my big project on Unity 5.6.5 it's 9 second.

    At least in an empty scene, there's no significant improvement. :(
     
    Last edited: Jul 26, 2019
  43. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,943
  44. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Since it hasn't been brought up in this thread yet. Starting with Unity 2019.3, Unity Technologies implemented a new feature to significantly reduce the "enter play mode time".

    Link to the official post that points to the documentation
    https://forum.unity.com/threads/preview-of-2019-3-features.678820/#post-4691099

    Link to thread about the new feature
    https://forum.unity.com/threads/insanely-fast-enter-to-play-mode.701930/
    https://forum.unity.com/threads/editor-domain-reload-and-or-scene-reload.680347/
     
    Last edited: Jul 28, 2019
  45. f5fairy

    f5fairy

    Joined:
    Nov 21, 2012
    Posts:
    10
    2019.3 alpha constantly crashes on my pc, so meanwhile I switched from 2018.3 to 2019.1. Entering time doubled.

    By the way I wish there was comparison of entering playmode time with different CPUs.
     
  46. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589