Search Unity

Best way to store custom metadata for scenes - Library?

Discussion in 'Scripting' started by Xarbrough, Jun 14, 2019.

  1. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    I have this recurring problem: we regurarly need metadata about scenes at runtime, e.g. a level name or total scene bounds and similar things. This data needs to be available at runtime without having the scene itself. I’m working in a team with version control, so the workflow for saving data automatically should not interfere with collaboration.

    Currently, I use the OnPostprocessScene callback to calculate things such as a scenes bounds and save the data to a prefab or ScriptableObject asset in the project. The callback is invoked by Unity before play mode and before building the player. This ensures that the data is always up to date.

    However, there are two drawbacks: First, when automatically saving data to an asset in the project, which is then comitted to source control it sometimes creates merge conflicts or generally confuses other team members why certain assets have changed automatically. Secondly, the data processing is always performed without caching, which slows down playmode-enter and the build process. I would rather cache my results and only generate new data when required, but since a Unity scene does not have a hash or version number for modifications, I can’t check, if the current scene state is already cached. I tried using callbacks such as EditorSceneManager.sceneSaved and OnWillSaveAssets, etc to track modifications myself and update the metadata after saving the scene instead of before play mode. However, this was rather cumbersome. Doable, but error-prone for special cases like moving assets or reverting changes through version control. And it still has the issue of dirtying assets for multiple team members.

    So, I thought about saving the metadata to the local Library folder instead and including the data only in the build. Is this possible and feasible? Does anyone have experience with such an approach?
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    AFAIK, Library folder is for editor use only.

    This one is good. But ideally you'd want some kind of dialog message asking if it should be saved.
    Also, do not override same values with the same values.

    It might be that imprecision is causing modifications, or something else.
    Look into whats constantly changing via git, and prevent it.
    Once you'd figure out this one, rest of the issues will go away.

    Teach your team members how to use rebase as well. And that they do not need to push every change done.

    You're storing data into scriptable objects, right? Add a field to it, and check against it. Problem solved.
     
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    If your project is on source control, why don't you track scene file changes with source control?

    Code (CSharp):
    1. git log --pretty=oneline --abbrev-commit --follow <fiename>
    2. git status
    Anyway, editor uses Library folder for caching too. So why you can't, sure you can. This will work of course. Or you may add another folder to gitignore file for that.