Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Creating and Managing Scenes from Code

Discussion in 'Scripting' started by lingumigeorge, Sep 22, 2018.

  1. lingumigeorge

    lingumigeorge

    Joined:
    Nov 21, 2016
    Posts:
    7
    This is more an open-ended, exploratory question: what is the recommended way to auto-create/update scenes?

    We're relatively new to Unity (2 years), and have not necessarily approached things in the traditional way. So a bit of context first:

    We use a multi-scene approach that stores initial load configuration in one scene. I.e. we load a configuration scene, which subsequently will:
    • load 1-4 other scenes with specific configurations
    • hydrate an initial state to be shared across the other scenes.

    The kinds of configuration include:

    - pure data (such as settings)
    - initialization and creation of GameObjects/Prefabs/Components (and the configuration of each of those)
    - custom assets/sprites

    Which will be passed to the other scenes via a shared state.

    We use the scene as configuration instead of:

    - code because we want to make the most of Unity's in-built editors (Inspector) and load system (for assets), and provide the customization opportunity (mainly for tweaking) to non-coders
    - ScriptableObject because it's not pure data - we want to initialize monobehaviours/prefabs/GameObjects, link them together, etc

    However, ultimately we'd like to move to a "configuration as code" approach that doesn't have the code only legible via the Unity Editor - but also in a code editor (however we want to continue to support the use-case where a non-developer can tweak the configuration via the Unity Editor - so the final product we want to keep as a scene). We'd like to be able to run validation/testing on the final configuration as well.

    I can think of two approaches:

    1. Scenes-As-YAML: Unity already does the whole "scene as configuration" thing when you use the YAML version of scenes. We could build tools that directly manipulate (read, write) that YAML file, and learn to read it
      • Pros: most direct approach - only one configuration file per scene
      • Cons: not sure it's possible without extensive knowledge of the object referencing system, or robust to changes in the file format (out of our hands)
    2. Config -> Middleware -> Scenes: We could develop our own config file (e.g. in JSON or YAML) and build tools that can parse that YAML and create and update scenes from it
      • Pros: decouples away from code/format we do not control (e.g. the Scene YAML format)
      • Cons: would require a lot of extra tooling to perform

    Does anyone have experience with a similar problem? E.g.
    • Auto-creation and updating of scenes
    • Using scenes as configuration
    • Manipulating/reading the scenes as YAML?
    • Validating/Auto-testing scenes
    Are we entering a rabbit hole we should not go down (e.g. we should not be using scenes, but a completely custom configuration format and build the GUIs ourselves)?

    Cheers!
     
  2. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
  3. lingumigeorge

    lingumigeorge

    Joined:
    Nov 21, 2016
    Posts:
    7
    Thanks! Good to know others have taken this approach. Cheers for the resources.