Skip to content

Instantly share code, notes, and snippets.

@paprikka
Created December 6, 2016 17:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paprikka/eca94da83f4f5fed42a2d317ac54f2ad to your computer and use it in GitHub Desktop.
Save paprikka/eca94da83f4f5fed42a2d317ac54f2ad to your computer and use it in GitHub Desktop.
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