Scene Stack for Complex menus

I want to show an upgrade menu but I have that currently loading as it’s own scene. It’s not exactly complex but it doesn’t use GUI stuff for any of it, as the user can rotate an object and click on things they want to change. As far as I can tell there is no way to push and pop scenes easily (looking at you LoadLevelAdditive).

I have a level that is created on start up (2d procedural) and I haven’t written anything to save state of the main game scene yet as it has a 2d array that houses the bulk of the data. I don’t think trying to stream to the filesystem is going to be fast on an iPhone and it would be easier if I could show the upgrade scene and then remove it’s components when the user exits it.

One solution I thought of (other than saving of course, I’ll get around to it eventually :p) is to use an invisible game object and stuff move all the things from my upgrade scene as child objects of that node, then make it a prefab and just pause the game behind it, and then switch cameras. Not sure how to switch cameras though. I haven’t looked in the docs yet.

So the question forum is, how do you push and pop your scene stack? Do you ever do that? How do you manage your options and other menus that are more than buttons and text?

Hello, I never thought in use a stage-stack for menus, but it seems to be a good idea. I think that you can create a vector of strings and simulate a stack controlling the index when putting or popping its strings. These strings are the names of the stage from your menu.

Before you load a new menu scene, save the scene’s name in this “stack” and load the next menu. The function is something like:

private var stack : String[];
private static stackSize : int = 0;
private static STACK_MAX_SIZE : int = 100;

function pushSceneName( var s : String )
{
	if( stack.length < STACK_MAX_SIZE )
	{
		stack[ stackSize ] = s;
		stackSize++;
	}
}

function popSceneName() : String
{
	stackSize--;
	return stack[ stackSize+1 ];
}

The variables that must be passed between scenes can be stored in a DontDestroyOnLoad variables. One of these per scene, and in the end you can do a script that destroy all objects with this propriety (using a tag or a specific Component).

My problem with DontDestroyOnLoad is I am not sure what I need to save or exactly how I need to use it. In need to save all the variables in the scene. As far as I know you can’t just pass in the scene and call it a day. I would have to reload all the terrain data and it’s not exactly fast on iPhone lol. I have a large 2d array(50x(up to)10,000) and some player variables like energy and money but that is stuff I need to store off to player prefs anyway. I’ll give DontDestoryOnLoad a try and see if I can figure it.

I’d suggest making your menu pages as GameObject hierarchies within the existing scene, rather then entire scenes in their own right. You can then “stack” them by having each contain a reference to the menu that opened them and to any sub-menu which they may have opened themselves. But, more importantly, you’re not unloading and re-loading a bunch of stuff that you want to keep.

You should really only change scenes when you want to discard everything that’s already loaded, or at least nearly everything. It’s fine to bring along a select few objects when you change scenes, but if you find yourself looking for a way to bring everything across you’re going against the grain of how Unity likes to work, and it’s likely to cause pain later on (like when you want to do a different scene change and carry a different bunch of objects along with you, at which point Unity will start being decidedly unhelpful as you have to manage destroying things yourself that Unity would usually just take care of for you).

That is what I was thinking, I created another post about Singletons and dont destroy but I’m about to drop it, and use the game objects to do what I want.

I may spend an afternoon writing some code to manage ‘scenes’ or actual scene management. I hear people talking about boot strapping then loading the actual level with your singletons. But honestly I had about 4 hours of sleep yesterday and it was a long day at work so I had little patience for stuff that wasn’t working the way I wanted it too :slight_smile: