Created
December 6, 2016 17:16
-
-
Save paprikka/eca94da83f4f5fed42a2d317ac54f2ad to your computer and use it in GitHub Desktop.
Poor man's prefabs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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