Search Unity

Accessing scene data from outside of scene

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, Mar 18, 2018.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    I'm trying to build a data-only "map" of each scene of my game which compiles some very basic data like the number of spawnpoints/idle locations, connections to other scenes, and the like. Doing this from inside a given scene is trivial, but I'd like to amass this data for every single scene in the game, and store it in a single interconnected graph in my database so various elements of game logic can "see" the world outside of the area the player is in.

    The obvious problem is that I can't actually query a given scene for its gameobjects unless it's actively loaded into memory. What I've been doing is using an editor script to assemble a given scene's data, parse it into JSON, and save the result as a text file. The first time a new game is started, it parses all of these files back into level data and forms the graph I need.

    I'm assuming this is about as clean as I'm going to be able to get it, but I hate how much manual work is required to maintain the system. Am I missing any way that this could be done programmatically?
     
  2. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    In a word ScriptableObject.

    That should be what you store data in inside unity. Easy to save to json as Unity already has json support, single line to convert a scriptable object to json. No need to load anything into Unity it's already there. The json export is just for external consumption.
     
  3. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    That's definitely a better storage solution, but I'm assuming there isn't any way I'm unaware of to explicitly check scene details from outside of a scene? Because ideally I'd like to just write an editor script that will iterate through every scene in the hierarchy and automatically load it and check whatever data it needs.
     
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Don't have the data local to the scene. The type of data you mentioned I would not store in the scene but in a global data store that scenes reference.
     
  5. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    The only reason I'm storing it in the scene right now is because the scene will change as it's developed; I want a relatively hands-off way to let our level designers do whatever they want without worrying about serialization, then trust the program to figure out what's been done and update the scene's footprint in data.

    What I have now works well enough that I don't think it's an issue, I was just hoping unity's API had features I was unaware of that allowed further automation in the editor than what I was already doing. ^^