Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Poor man's prefabs
public class GUIScreenLoader
{
private Transform container;
private MainNavGUIConfig config; // a ScriptableObject with a list of easily editable components
// it also contains a GUI Scene reference, for more control over
// resource loading.
public GUIScreenLoader(MainNavGUIConfig config, Transform container)
{
this.config = config;
this.container = container;
}
public void LoadViews()
{
MonoBehaviourBridge.instance // used to avoid creating unnecessary MonoBehaviours whilst having acccess to the API
.StartCoroutine(LoadSceneAsynchronously(config.sourceSceneName));
}
private IEnumerator LoadSceneAsynchronously(string sceneName) {
AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
while (!async.isDone)
{
yield return null;
}
config.GUIScreens.ForEach(AddView);
SceneManager.UnloadScene(config.sourceSceneName);
yield return null;
}
private void AddView(GUIViewConfig viewConfig)
{
var go = GameObject.Find(viewConfig.screenName);
Debug.Assert(go != null, "viewConfig.screenName should point to an existing view instead of: " + viewConfig.screenName);
if (!viewConfig.startEnabled)
{
go.SetActive(false); // There's a separate ScreenManager class used for view transitions and perf.
}
go.transform.SetParent(container, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.