Search Unity

How can I handle this? Optional loading of data at level start

Discussion in 'Editor & General Support' started by Marscaleb, Jan 26, 2020.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    I'm trying to set up a system to play some in-game cutscenes within my game. They will play only once on a play-through.
    The way I have things set up right now, I have code that gets called at the very start of certain levels. One of the first thing it does is check if certain events have occurred, in this case, if these cutscenes have played. If not, it starts the cutscene (which currently is just a placeholder text box) and if so it just does nothing.

    Right now I'm setting up my scripts to actually perform the cutscene, and there is an issue I am running into.
    When the cutscene actually performs, it is going to display a number of graphics and things that are only used for this event. If/when the player ever returns to this level, the cuscene won't play, and thus all those special graphics and other assets won't be needed.
    While I could have all those various assets sitting in my scene, ready to be used, this will cause the level to take longer to load if/when the player returns to the level. It seems to me that it would be wiser to NOT load those various assets, and if the initial script determines that the cutscene needs to play, THEN it loads all those extra assets and plays the cutscene.

    I am wondering the best way to go about this.
    (Or what effective methods are possible, really.)

    At first my thought was to tie in these assets with a prefab, and then I can have a reference in my script to all these assets that it can import into the scene just when it is called for. But would that actually work? Or when I have an object in my scene with a public variable referencing that prefab, will the scene automatically load that prefab when the scene loads, thus defeating my plan to avoid loading it?

    And as I think about it, right now these cutscenes consist of just large images to use like splash screens, but as my game develops these will be replaced with more complex entities. They may come to contain more data than I would feel comfortable putting into a single prefab. I'd like to use a system that can work with calling all the data I need when I need it.

    I know there is the Resources folder that I can put assets into that I can call via scripts, but I am concerned about putting too much into that folder. Do the contents of that folder create any extra overhead in my game? It doesn't sound reasonable to put things in that folder that are only ever used once.

    The only other thought I have is to stream in multiple scenes, so I could put all of the extra assets into a separate scene and then load it just when I need it. Now it's been a while since I've looked into that, but I remember that used to be functionality locked behind the paid version of Unity. Has that changed? Can I stream multiple scenes into my game with just the basic version of Unity?

    Also, all this has gotten me to wonder what kind of memory costs these (and other parts) are adding to my game. I'm wondering about memory management in general; how I can monitor how much memory my game is using, how I can control the loading/unloading of various assets to keep the memory requirements down, if it comes to it. I suppose its a different subject altogether, but it seems like something I would want to keep in mind.

    But at any rate, what are some effective ways I can structure my system for loading assets so that I don't have to load a bunch of things I won't need every time my level loads?
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Yes.

    Unity Technologies recommend to avoid the Resources folder for pretty much everything except a few specific use cases:
    https://learn.unity.com/tutorial/assets-resources-and-assetbundles#5c7f8528edbc2a002053b5a7

    The async scene loading API is available in at least 2017.1 and newer versions.

    Unity's Memory Profiler:
    https://forum.unity.com/threads/new...e-for-unity-2018-3-and-newer-versions.597271/

    I think the Addressable Asset System could help with that, because it also handles ref-count and unload of unneeded assets:
    https://docs.unity3d.com/Packages/com.unity.addressables@0.8/manual/index.html
     
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    Oh, ohhh... fffffff...
    Yeah, I've been using the resource folder for a lot of stuff. It's where I put the prefabs for my characters, particle systems, everything used in my HUD, all of my sounds...
    There's a lot of stuff in there because it was the only method I was aware of to be able to load assets through code, as everything else I'd have to make a public variable for that and drop a reference in the inspector. That can require a lot of variables, and well, there are a lot of things a game could be called to spawn at any moment. And I like having my game's code automatically populate things rather than setting up individual objects in my scene that could be forgotten.

    So... Not what i originally made this thread for, but... If I'm not supposed to be using the resources folder, then is there some other way I can load assets via code?
    EDIT: Oh, maybe I can do that with this Addressable Asset System you linked to. I'll take a look at. Though I'm still open to other ideas, if there are better ways to do it.
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    You can also use private variables, but these need to be decorated them with the [SerializeField] attribute in order to appear in the Inspector.
     
  5. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    Good to know. But the point is still that I need a variable that I fill in the editor just to ever have one item I'm looking to use.
    For example, when my level starts I need to spawn the player, but there are three different prefabs for each different player class. I currently just have those classes in the resources folder and my game manager script just grabs the class that needs to spawn. If I did that traditionally, I would need my game manager to have three variables just to contain each player prefab. It may not be much for just that one example, but I also have animation controllers I swap out at run-time, different sounds that have to be selected and played at run time, prefab effects, and other things that all can be different options depending on what happens within the game. That's a lot of variables that require dropping assets into through the inspector for each individual character, enemy, and etcetera. So I just used the resources folder and wrote a single line of code that directly gives the name/path of the asset that is needed.

    This is especially true for my HUD system. Good hell, if I had to create a variable for every single graphic that shows up on the HUD and UI, I'd have a monstrous-sized item in the inspector that would become difficult to navigate, easy to screw up, and take two or three times longer to set up even if everything went right. Plus, wouldn't that require more assets to be loaded at all times instead of when needed?
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,637
    Not sure about "streaming". Are you simply talking about additive scene loading? That's certainly available in the free version (As far as I know, there are no barred engine features anymore). It's super useful and should do what you want.
     
    Last edited: Jan 27, 2020