Search Unity

SettingsProvider - access custom settings

Discussion in 'Scripting' started by MaxRoetzler, Apr 8, 2020.

  1. MaxRoetzler

    MaxRoetzler

    Joined:
    Jan 3, 2010
    Posts:
    136
    Hi, I posted this question in the Editor/General Support section but don't think it was the right place for this. Sorry for the double post!

    It feels a bit stupid asking this, but how do you access the custom settings added via the SettingsProvider API? I didn't find anything about it. The old PreferenceItem (never used it) is used with EditorPrefs, but it seems a bit redundant if I already have an asset/json file containing the data.

    Is there no built-in way to access the project settings?
     
  2. marianomdq

    marianomdq

    Joined:
    Sep 29, 2015
    Posts:
    17
    Hello @MaxRoetzler.

    If you are talking about accessing the data, then I think it depends on how you've serialized the data. For example, if you are implementing something like the Official Docs Example, then you will have a ScriptableObject created that holds your data. You can access that data in many different ways.

    Some examples:
    • By having a reference to that ScriptableObject somewhere in the Editor.
    • By getting the reference using AssetDatabase.LoadAssetAtPath<>.
    • Adding some Getter methods to the Example's MyCustomSettings that exposes the private fields.
    If you are referring to how to access these settings through the Menu UI then it depends on the SettingsScope you have chosen. SettingsScope.User will display the settings on the Preferences Menu and SettingsScope.Project will display them on the ProjectSettings Menu.
     
    Deleted User likes this.
  3. MaxRoetzler

    MaxRoetzler

    Joined:
    Jan 3, 2010
    Posts:
    136
    Hi,

    I was looking to implement both user and project settings for a project. I ended up using a json file to store the settings data in the ProjectSettings directory, as the AssetDatabase-API is limited to the main Assets directory... And storing user (or any) settings in the Assets directory seems like the wrong idea.

    Since it is a new API, I thought it could do more than to display serialized data in the proper window. I was hoping there would be a solution for accessing the custom settings from other editor scripts without having to reference / find some asset or json file, and also the option to store assets in the ProjectSettings directory like Unity does :/

    But thanks for the feedback!
     
  4. marianomdq

    marianomdq

    Joined:
    Sep 29, 2015
    Posts:
    17
    @MaxRoetzler are you using the SettingsManager package for this purpose?

    If you are not, I think that would be a very nice solution for your problem. It automatically handles the serialization of your settings by creating a folder inside ProjectSettings and storing those settings in a json file (like you are doing, that's why I think you might be using it).

    But moreover, it doesn't need a ScriptableObject to work, you just need to "ask" for the setting key, making it more easy for other Editor scripts to interact with.
     
    Deleted User likes this.
  5. MaxRoetzler

    MaxRoetzler

    Joined:
    Jan 3, 2010
    Posts:
    136
    @marianomdq I tried the SettingsManager package (2020.2.0f1c1). Looking at the example that comes with the package, there is a noticeable lag when moving sliders or changing color values through the UI, compared to the other 'built-in' settings, or my own implementation (json + settings provider).

    Also, any reason the package is not part of the 'Unity Packages Registry', despite being out of preview?