Search Unity

I am really struggling to find the right approach to dynamically load content in my game

Discussion in 'Editor & General Support' started by Shadoninja, May 5, 2018.

  1. Shadoninja

    Shadoninja

    Joined:
    Nov 12, 2013
    Posts:
    26
    I have yet to figure out the proper way to handle prefabs dynamically. Here is a concrete example of one of the problems I am trying to solve. A solution to this problem will likely help me in all the other parts of my game where I need to be smart about how I am loading in content:

    Outline of the task:
    I am going to have two overlays for my main game. One is going to be the main HUD while the game is unpaused, and the other is going to be the overlay used for the pause menu. Both of these overlays will exist on the same scene and the user will be able to transition between them very quickly if they want to. Only one will be visible/active at any given time.

    My old approach:
    So far, I have been using a very code-based approach to doing everything in my game. My main overlay is generated by Resources.Load()'ing all my TextMesh prefabs and instantiating them with an Overlay.cs script.

    This has been working perfectly fine! But as I have used Unity more and more, I have found that a lot of people (even the Unity documentation folks) say the Resources folder should be avoided in larger projects. I have read through their reasons why not to use it and I mostly agree.

    So I tried to circumvent the Resources folder entirely.

    My new approach:
    I made a Prefab that contains all of the information for my main HUD and I want to put that on an OverlayManager GameObject that exists in my scene. The OverlayManager can simply keep track of the game state and use a public GameObject reference to the HUD prefab (you know, where you drag the prefab to the object editor slot?) and Instantiate() it at runtime. When the game state changes to paused, I can destroy the old HUD and load up the pause overlay. With this approach, I would be avoiding the requirement for Resources.Load() by having a GameObject exist on the scene and keep track of all the relevant prefabs that need to be loaded.

    The problem:
    But this doesn't quite work. In order to keep a prefab loaded into a GameObject's public variable space, that prefab also needs to exist on the scene. But that defeats the entire purpose of everything I am trying to do. I don't want every single thing living on the scene at all times...

    Hopefully this all makes sense. I am dedicated to figuring out the right way to do this. Any advice? Also, I want to mention again that this post is for a discussion to happen around the grander issue of dynamically loading content in Unity. I am just giving the problem I am facing at this exact moment as a way to help describe my gap in knowledge on scalable Unity projects.
     
    Last edited: May 5, 2018
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Hi,

    For a HUD that will presumably be in most if not all scenes, there's no reason to avoid Resources.

    Avoid Resources for assets that only get loaded later in the game. For example, if the first half of your game needs to dynamically load assets for a jungle world, don't put the second half's ice world assets in Resources. Use AssetBundles. That's one of the reasons for them.

    In your concrete example, the best solution is what wayne1512 provided in your original thread (setting either of two canvases active/inactive). But that's because of the use case you described.