Today I got to a point where I wondered why Application.LoadLevelAdditive doesn’t give back a reference to the loaded scene that I can search through the new scene for a gameObject (the scenes root gameobject in my case). That seems to me a big disadvantage to Instantiate as this gives me back the GameObject I loaded (in this case the scenes root GameObject) and I have now a reference on it.
At the moment, I’m having in my scenes a hirarchy like:
and the root script will register itself to a static container, so I can later access the root script via this container. There are some disadvanteges to this solution, so I’m rather unhappy with it, but couldn’t find a better solution for now.
So what I basically want to achieve is something like:
class ParentController
{
private IEnumerable LoadChildLevel()
{
var newLevel = Application.LoadLevelAdditive("newLevel");
while(!newLevel.isLoaded)
yield return;
var sceneRootScript = newLevel.FindGameObject("sceneRootScript").GetComponent<SceneRootScript>();
sceneRootScript.parentController = this;
}
}
Is there a way to do this via the LoadLevelAdditive function? With Instantiate it would work, though it may lag as hell when instantiating big prefabs with many textures.
Ok, but how could my newly loaded scene register to its ParentController? My new scene depends on values (like a viewframe or size) of the ParentController? Either the child finds the parent (parents problem maybe, that there may be several equal parents) or the parent finds the child (which not works via LoadLevelAdditive).
My main problem is rather that the ChildController couldn’t find its ParentController to get info from it than the clean up of the old scene.
To clear up, heres what I want to do:
class ViewController
{
public List<ViewController> childControllers;
public ViewController parentController;
public Rect frameSize;
public void AddChildController(ViewController childController)
{
childController.parentController = this;
childControllers.Add(childController);
}
public void RemoveFromParent()
{
if (parentController != null)
parentController.childControllers.Remove(this);
}
}
Pretty basic MVC pattern start. But it has no the problem, that the connection between child and parent couldn’t be established.
The main problem with the instantiate way would be, that it could allocate too much memory at once, but if the prefabs are only small (using maybe one texture atlas) it shouldn’t be a problem!?