Search Unity

Can You Create A Scene From INSIDE Your Game?

Discussion in 'Getting Started' started by gamecreatorc1, Aug 27, 2020.

  1. gamecreatorc1

    gamecreatorc1

    Joined:
    Dec 12, 2019
    Posts:
    64
    If I'm running my game, can I place a block (model) that will still be in the scene after I exit the game? I'm looking to do an in-game level editor that also adds to the Unity scene. If this is possible, how can I both add and remove models? Thanks.
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    To the best of my knowledge, you cannot create a scene from within Play mode. There are definitely ways to edit one while in Play mode, such as using ScriptableObjects to store the state of the level. You could also setup shortcuts or UI buttons to serialize the level into a file that you load back in Edit mode after exiting.

    Bear in mind that this is all assuming you're running inside the Editor, still, not a standalone build of the game.

    Edit: It seems I was wrong about creating scenes at runtime! This is news to me. Something new every day and such!
     
    Last edited: Aug 27, 2020
  3. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Create a Scene at runtime like this:

    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    2. [...]
    3. Scene tempScene = SceneManager.CreateScene("temp");
    However, you just need to call
    DontDestroyOnLoad(transform)
    to keep something around between scenes.

    Edit: ..and the easiest way to work with that is to assign that transform to a static variable somewhere, i.e.
    public static Transform interSceneContent;
    . Then you can easily access that reference via TheClass.interSceneContent in each scene's code or generate the content, assign the reference and call DontDestroyOnLoad on that if the interSceneContent is null.

    Edit: Sorry - misunderstood what you were asking :rolleyes:
     
    Last edited: Aug 27, 2020
  4. gamecreatorc1

    gamecreatorc1

    Joined:
    Dec 12, 2019
    Posts:
    64
    My bad. I should have said edit scene, not create. Thank you both for the help and polemical for the code. I'll give that a shot.
     
    ApexPredator343 likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can't edit a scene itself. You just record whatever changes took place in your favorite format, and restore those when you want to return to that state. Google for Unity save/load systems for ideas.
     
    Ryiah likes this.
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,147
    This. There have been assets on the store in the past that would automate this, but the basic idea is to have an editor script that on demand will scan everything in your scene and serialize the changes to an external file. Once play mode has ended you fire off another command to read the changes from file and apply them to the scene.

    Alternatively, and with significantly more effort, you could bypass the scene system altogether and simply use your own formats to store your scenes. There are advantages to this starting with players being able to fully mod the game world.

    If I were going to build a system like this it would be based off of the tier database approach used by Bethesda. Simply put there is a master database, a patch database, and a save game database. Master databases are the game as it exists at release. Patch databases contain any mods to the master by either the developer (actual patches) or the players. Save game databases are changes made to the world by the player.

    Saving and loading from the databases would be handled via custom editor scripts.
     
    Last edited: Aug 28, 2020
    Joe-Censored likes this.