public T LoadPrefab <T> (string prefabName) where T : TBase { // check if the profile already exists PrefabProfile profile = null; if (_profiles.TryGetValue(prefabName, out profile)) { return(profile.Prefab.GetComponent <T>()); } // attempt to load the prefab GameObject prefab = (GameObject)Resources.Load("Prefabs/" + _prefabDirectory + "/" + prefabName); if (prefab != null) { // check if the GameObject has the TBase, base-type component of specified type attached to it T component = prefab.GetComponent <T>(); if (component != null) { profile = new PrefabProfile(typeof(T), prefab); _profiles[prefabName] = profile; Debug.Log("Loaded prefab of type: " + component.GetType().ToString()); return(component); } else { throw new Exception("Couldn't find " + typeof(TBase).ToString() + " component of explicit type '" + typeof(T).ToString() + "' attached to '" + prefabName + "'."); } } // attempted to load a prefab that doesn't exist throw new Exception("Unable to load prefab: " + prefabName); }
public PrefabProfile GetPrefabProfileByName <T> (string prefabName) where T : TBase { // check if the profile already exists PrefabProfile profile = null; _profiles.TryGetValue(prefabName, out profile); return(profile); }
public void ReleasePrefab(string prefabName) { PrefabProfile profile = null; if (_profiles.TryGetValue(prefabName, out profile)) { _profiles.Remove(prefabName); } }
protected T Instantiate <T> (string prefabName, Transform spawnTransform) where T : TBase { PrefabProfile profile = null; // attempt to get the prefab profile if (!_profiles.TryGetValue(prefabName, out profile)) { // load the prefab, and attempt again LoadPrefab <T>(prefabName); if (!_profiles.TryGetValue(prefabName, out profile)) { // report the error, and return Debug.LogError("Trying to instantiate " + typeof(T).ToString() + " with a prefab that doesn't exist: " + prefabName); return(null); } } return(Instantiate <T>(profile.Prefab, spawnTransform)); }