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

Resolved Save/Load easy to find and edit JSON file.

Discussion in 'Editor & General Support' started by rothpletz5, Oct 14, 2021.

  1. rothpletz5

    rothpletz5

    Joined:
    Dec 2, 2020
    Posts:
    27
    Greetings,

    I have a project which saves and loads data from a JSON file. Currently, the file is stored using the path

    path = Application.persistentDataPath + "/saveData.json";


    This works, it saves and loads the data just fine. However, I would like its data to be stored within the project's build folder (So it is easy to access/edit outside the program, and its data is saved in the project itself, not the local device).
    What is the best path to use/ way to go about this? I am on Windows, but a solution that works cross-platform would be preferable. I understand this is not good standard practice, but it would be best for the given situation.
    Thanks for your help and have a great day!
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    There is none. You should use the persistentDataPath. They are giving it for you to use it.
    It is a very rouge behavior and suspicious to write inside an application folder. Especially on Windows.
     
    Kurt-Dekker likes this.
  3. rothpletz5

    rothpletz5

    Joined:
    Dec 2, 2020
    Posts:
    27
    This is for a personal project, not something that would be widely released. I would use Resources folder, but as far as I am aware it is read-only.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,872
    It sounds like you might want to make this an Editor extension, only able to save and load to the Assets/ folder. You could also make an Editor script that ran some other tool, which can write pretty much anywhere you want within your Editor-using credentials allowed.

    Once you Build, the resulting files of the Build are essentially completely divorced from Unity and no longer have any connection to the project, the project folder, or anything else. It's just an executable. To have a capability to write anywhere beyond the persistentDataPath would be suspicious, and the Unity runtimes actively work to avoid that.
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    You can try the https://docs.unity3d.com/ScriptReference/Application-dataPath.html but it may be read only due to OS restrictions and you are risking removing the saved data if the application gets deleted.
     
    rothpletz5 likes this.
  6. rothpletz5

    rothpletz5

    Joined:
    Dec 2, 2020
    Posts:
    27
    ah ha! That's perfect for my scenario! Thanks for your help!