Search Unity

Strategies to store and load prefab at runtime

Discussion in 'Entity Component System' started by nantoaqui, Jan 11, 2020.

  1. nantoaqui

    nantoaqui

    Joined:
    Oct 1, 2014
    Posts:
    42
    Hello!

    I would like to know how do you store and load prefab at runtime using DOTS. I'm using hybrid approach and the conversion mode is set to "Convert and Inject Game Object", meaning that i need to keep mono object live on the scene.

    The simple case is to instantiate different types of enemies based on player actions/world level, etc.

    Here's my current approach:

    EnemiesSpawn's is a singleton scriptable object responsible to store spawn locations

    Code (CSharp):
    1.    
    2.     [CreateAssetMenu (fileName = "Enemies", menuName = "Rogue/Enemies")]
    3.     public class EnemiesSpawn : SingletonScriptableObject<EnemiesSpawn> {
    4.         [SerializeField]
    5.         public List<Vector3> Locations;
    6.     }
    7.  
    MutationTable's is a singleton scriptable object that holds a list of prefab objects and a "CharacterType" to be used as query

    Code (CSharp):
    1.    
    2.     [CreateAssetMenu (fileName = "Mutation Table", menuName = "Rogue/Mutation")]
    3.     public class MutationTable : SingletonScriptableObject<MutationTable> {
    4.         [Serializable]
    5.         public class Mutation {
    6.             public GameObject value;
    7.             public CharacterType key;
    8.         }
    9.  
    10.         [SerializeField]
    11.         private List<Mutation> references;
    12.  
    13.         private Dictionary<CharacterType, GameObject> _lookupTable;
    14.  
    15.         public void OnEnable () {
    16.             _lookupTable = new Dictionary<CharacterType, GameObject> ();
    17.             foreach (var entry in references) {
    18.                 _lookupTable.Add (entry.key, entry.value);
    19.             }
    20.         }
    21.  
    22.         public GameObject GetByType (CharacterType type) {
    23.             if (!_lookupTable.ContainsKey (type)) {
    24.                 throw new Exception ($"{type} not found");
    25.             }
    26.  
    27.             return _lookupTable[type];
    28.         }
    29.     }
    30.  
    SpawnNightCreatureSystem is the system responsible to spawn enemies, it uses the singleton objects listed above to query the data needed (location & prefab).

    Code (CSharp):
    1.    
    2. public class SpawnNightCreatureSystem : ComponentSystem {
    3.         EntityQuery queryNightPhaseEntered;
    4.  
    5.         protected override void OnCreate () {
    6.             queryNightPhaseEntered = GetEntityQuery (ComponentType.ReadOnly<NightPhaseEntered> ());
    7.         }
    8.  
    9.         protected override void OnUpdate () {
    10.             if (queryNightPhaseEntered.CalculateEntityCount () == 0) {
    11.                 return;
    12.             }
    13.  
    14.             var locations = EnemiesSpawn.Instance.Locations; //
    15.  
    16.             for (int i = 0; i < locations.Count; i++) {
    17.                 var location = locations[i];
    18.                 GameObject characterPrefab = MutationTable.Instance.GetByType (Characters.CharacterType.GHOST);
    19.                 Object.Instantiate (characterPrefab, new Vector3 (location.x, location.y, location.z), Quaternion.identity);
    20.             }
    21.         }
    22.     }
    23.  
    This code works fine but i would like to know if this is the way to go. With so many tools at our disposal how do you decide the most suitable for this job? I was reading about Resource.Load, AssetBundle.LoadFromFile, BlobAssets but i don't know which one to choose.

    Could you please share your strategies for this problem?

    Thanks a lot!