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

Question load data saved in json before anything in game and set the controls with these values

Discussion in 'Scripting' started by bisewski, Sep 7, 2023.

  1. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    120
    Hi.

    I need to save the settings data from my app. Not score or games sets but an app settings that need to be read before anything because these settings will change the layout of my app.

    So I am using PlayerPrefs to use at runtime and saving and loading data from a json file.

    The problem:
    I have one slider that when I change the value, I call a script that save the new data in json.
    The problem is that when I am opening the app I set the slider value to the saved value in json file but when I do it the script that save in json is called but in fact I doenst changed the value, I just set to the saved value.

    How is the correct way to deal with it? Maybe a public static bool appStarted = false and turn it to true after Start() called?

    sds
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Normally you would do all this stuff in a bootstrap scene that does everything before loading into the first actual runtime scene in your application.

    Then when the UI loads, it can just configure itself based on the loaded data. This assume that you have a pure data layer that's separated from the the actual visualisation of that data, of course.
     
    Bunny83 and bisewski like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    This type of iterative layering is common in save systems. See Step #2 of the loading steps in the first link below:

    Load/Save steps:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    An excellent discussion of loading/saving in Unity3D by Xarbrough:

    https://forum.unity.com/threads/save-system.1232301/#post-7872586

    Loading/Saving ScriptableObjects by a proxy identifier such as name:

    https://forum.unity.com/threads/use...lds-in-editor-and-build.1327059/#post-8394573

    When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls
    new
    to make one, it cannot make the native engine portion of the object.

    Instead you must first create the MonoBehaviour using AddComponent<T>() on a GameObject instance, or use ScriptableObject.CreateInstance<T>() to make your SO, then use the appropriate JSON "populate object" call to fill in its public fields.

    If you want to use PlayerPrefs to save your game, it's always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

    https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

    Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide
     
    Bunny83 and bisewski like this.
  4. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    120
    holly...Really thank you for your attention...I will first read everything and study the links and after it I will post the progress.
     
  5. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    120

    That is a book, not a answer...Thank you a lot.