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

How should I save preferences data for a package?

Discussion in 'Package Manager' started by craigjwhitmore, Jun 24, 2021.

  1. craigjwhitmore

    craigjwhitmore

    Joined:
    Apr 15, 2018
    Posts:
    135
    I'm writing a package for people do download from the asset store. The package has user preferences. How should I go about saving this.

    Currently, I'm saving the preferences to a .json file, so, is it viable to save this to "Packages/MyProject/Data.json"

    I'm a little bewildered by this, as I hear that the packages folder is cached, so it will be reset upon next compilation, in which case the above example would fail.
     
  2. Epsilon_Delta

    Epsilon_Delta

    Joined:
    Mar 14, 2018
    Posts:
    205
    If the preferences are project bound and should be shared between users (and visible to version control):
    Use json or scriptableObject or whatever else in the Assets/ folder. But think about updating your package and rewriting the stored preferences on import of new version. The solution might be dynamically create preferences scriptable object in Resources folder, first checking if it already exists.

    If the preferences are project bound and should not be shared between users (and invisible to version control):
    Use EditorPrefs https://docs.unity3d.com/ScriptReference/EditorPrefs.html. Put a project specific prefix in the key name and ideally also use some custom package prefix like EditorPrefs.SetInt(PlayerSettings.companyName + PlayerSettings.productName + "_MyCustomPackageName_fontSize", 3);.
    You can also use PlayerPrefs or saving data to Application.persistentDataPath, but I recommend the EditorPrefs.

    If the preferences are not project bound, but should be shared across all projects (in the same Unity Editor version) on one machine (and invisible to version control):
    Use EditorPrefs without project specific prefix.

    You can also take a look at this new feature: https://docs.unity3d.com/ScriptReference/SettingsProvider.html
    I never used it, but it seems to be related.
     
  3. craigjwhitmore

    craigjwhitmore

    Joined:
    Apr 15, 2018
    Posts:
    135
    Thank you for the info. I think a settings provider is much of a sledge hammer for a peanut approach. I'm just storing the colour and size of a few gizmos.

    Where would the Resources folder be located, would that be a sub-folder within my package perhaps?
     
  4. Epsilon_Delta

    Epsilon_Delta

    Joined:
    Mar 14, 2018
    Posts:
    205
    So I presume you are going for the first option. If it is a UPM package, that probably wouldn't work, because it is stored in packages folder as you mentioned. You have to create the settings file in Assets/Resources/ or Assets/YourPackageName/Resources/ or Assets/Settings/Resources/ or whatever you like.
    If it is not UPM package but a .unitypackage, it's already inside Assets folder. If the settings file is dynamically created after import, it will not get overwritten on reimport if the existence of such file is checked in code.
     
  5. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,015