How to design a reusable menu script that loads another scene after running some other logic?

Hi there,
I want to try out a new framework and started with a Minesweeper clone. There are 3 scenes to consider

  • Main Menu

  • Play Button => Load the board configuration scene

  • Options Button ( … not yet … ) => Load the options scene

  • Quit Button => Quit the game

  • Board Configuration

  • Start game with specific settings button ( it’s a prefab for 3 different settings ) => Load the game scene

  • Back Button => Load the main menu scene

  • Game

  • Back To Board Configuration Button => Load the board configuration scene

  • Back To Main Menu Button => Load the main menu scene

  • Quit Button => Quit the game

As you can see there is some similiar logic. I don’t want to create a script “MainMenu” and hardcode the logic because I can also reuse it somewhere else.

When creating a script, e.g. “LoadScene”, configure the scene variable and attach it to a button this works fine BUT I won’t be able to run some additional logic ( e.g. assign the selected board configuration, play an animation before changing the scene etc. )

How did you guys solve this? Do you hardcode the menus?

Thanks in advance!

So far in my experience with UI it’s been half/half with regards to hard-coded-ness and reusable components/overarching systems. Particularly when I’m finding myself wanting/using a UI feature more than once, that’s the time to turn into something more generic and reusable.

In your case, a good starting point would be a reusable way to load scenes. I can suggest encapsulating scenes in scriptable objects - whether it just have a field for the scene name/build index, or an addressables reference if you’re using that - and throw the logic into loading a scene into it as well. Then you can just reference those in a Button’s OnClick event, and call the appropriate method to load the level.

1 Like