Search Unity

LoadSceneAsync, LoadScene and Resources.UnloadUnusedAssets

Discussion in 'Scripting' started by Megalithic, Aug 28, 2017.

  1. Megalithic

    Megalithic

    Joined:
    Apr 21, 2014
    Posts:
    61
    When I load my scene using loadsceneasync, the loaded scene becomes very laggy on my S4. Fps is around 25 - 33. But when I load my scene with loadscene. Fps is quite high, in the 41 - 50 fps region. I thought it could be something to do with memory usage, So I called Resources.UnloadUnusedassets() on scripts that might be storing memory but its still not working.
    I would have called Resources.UnloadUnusedassets() on all scripts but I fear it might crash my scene. So I avoid that. I would like to know your practice for calling Resources.UnloadUnusedassets().
    If you also know what might be causing the problem of low fps when using loadlevelasync and no drops when using loadscene, please do tell me.
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Is it only temporarily laggy? Are you watching the AsyncOperation that's returned from your LoadSceneAsync method to make sure the loading is complete? It's possible that the scene loading is not even complete yet.
     
  3. Megalithic

    Megalithic

    Joined:
    Apr 21, 2014
    Posts:
    61
    What happens is, I have an intro scene where the loadlevelasync is called for the next scene but the isallowedtoloadlevel is set to false. When the player presses the start button isallowedtoloadlevel is set to true and the next level is loaded. In that level I am facing fps issues.
     
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Would it be possible instead, using LoadSceneAsync and getting the AsyncOperation, to then set the allowSceneActivation property to false? Then when the user clicks the button, toggle that to True.

    https://docs.unity3d.com/ScriptReference/AsyncOperation-allowSceneActivation.html

    https://docs.unity3d.com/ScriptReference/AsyncOperation.html

    See if that makes a difference. It should accomplish pretty much the same thing, except using the newer SceneManager bits.
     
  5. Megalithic

    Megalithic

    Joined:
    Apr 21, 2014
    Posts:
    61
    I must apologise. Instead of isallowedtoloadlevel I should have said allowsceneactivation. That is how I am doing it. Referring the AsyncOperation and then accessing the allowsceneactivation variable. Once the player presses the start button I set allowSceneActivation to true.

    Excuse me, using my mobile so had to define those words from the top of my head.
     
  6. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    No problem. I'm not sure.... Are there streaming assets like asset bundles that might still be loading? The way you're doing it, isDone will never be true... but once the progress hits 0.9 it should be done, at which point toggling that flag shouldn't be causing lag spikes.
     
  7. Megalithic

    Megalithic

    Joined:
    Apr 21, 2014
    Posts:
    61
    How do I check for streaming assets like asset bundles?
     
  8. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    You probably don't have them then. :)
     
  9. Megalithic

    Megalithic

    Joined:
    Apr 21, 2014
    Posts:
    61
    Ugh...I get the feeling I have to through long frustrating nights again! :D
     
    Dustin-Horne likes this.
  10. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Sorry I couldn't be of any help.
     
  11. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I don't think you're removing your intro assets. When you load async, it does not get rid of anything. So likely, that's still rendering/playing in the background?
     
    Dustin-Horne likes this.
  12. Megalithic

    Megalithic

    Joined:
    Apr 21, 2014
    Posts:
    61
    Oh no buddy! Really appreciate your assistance!

    I checked the hierarchy tab, there is nothing from the previous scene once it loads. Unless, they remain in the memory. Should I try and blast that scene with Resources.unloadunusedassets in the scripts.:D
     
    ROBYER1 likes this.