Sunday, July 10, 2016

unity - Game states & passing data between scenes


I'm not entirely sure if I'm doing this right, but I always considered the scene system in Unity as some sort of state management (rather than e.g. a level system) which allows to group the contents (entities, components) into the usual main states of the game (menu, ingame), which can then be invoked or replaced together.


Now I have a very specific concern and I have no clue how to deal with that in Unity, mostly because of it's abstractions from code and other restrictions:


My game features a procedurally generate terrain where as I'd like to tweak the generator before actually launching it. The tweaking is done in some sort of 'pre-game' state (as part of the main menu) where the values are stored in e.g. TerrainData model class. Usually in other engines, I'd to it like this:



  1. Providing an UI within the menu state to tweak the parameters of the game

  2. Storing the parameters in a view model (and data model)

  3. Instantiate the ingame state (which requires said data model in the .ctor

  4. Pass the data model to a generator and start the generation



Now, here are the main problems:



  • Most types in unity (scenes, monobehaviors) do not allow constructors because they use it for themselves

  • It doesn't seem possible to instantiate scenes manually (there's only e.g. Application.LoadLevel("") which only takes the name of the scene, not a instance reference etc.

  • it doesn't seem possible to pass data between scenes, without reverting to awkward workarounds such as singletons, playerprefs, DontDestroy etc. which are all not really suitable for this case.


So, how can I properly solve this? Is there a better way to manage states rather than scenes? Prefabs maybe?


Backlink to UntityAnswers question: http://answers.unity3d.com/questions/1149480/game-state-management-and-passing-data-between-sce.html




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...