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 Saving parameters from editor which have been generated from script?

Discussion in 'Editor & General Support' started by HookedWit, Aug 12, 2023.

  1. HookedWit

    HookedWit

    Joined:
    Dec 31, 2022
    Posts:
    31
    First I'll explain the context, then the question will make sense.

    My program has a simulated environment. Currently, at start-up, the code performs a one-off scan of the environment & generates data which is then used at runtime. This is a performance optimisation: the generated data is large (by human standards) but is quick access. As I develop the environment so the generated data changes - but (eventually) the environment (& hence the optimised generated data) will be fixed.

    This one-off scan currently takes about 20 seconds (it is the bulk of the program start-up time). What I'd really like to do is perform my one-off scan once, in the editor environment, and then save the scene - so that this optimised data is part of the scene load. If I can do that, then I don't have to run it at start time (I no longer suffer the 20 seconds...).

    So I've written a bit of script to add a menu option to the editor, which runs this code. And it works fine - the relevant parameters get updated in the inspector. So now I don't have to run the program itself to see the result of running this optimisation scan.

    So I do this, and I save the scene. Except these procedurally updated parameters don't get saved. Next time I load they are not there - the impacted parameters are back to their default values.

    If I was going to hazard a guess, it seems as if it only saves those parameters which have been altered by a soft fleshy thing using a keyboard in the editor - stuff written via a script doesn't get so flagged & hence doesn't get saved. Maybe this is an optimisation on saved scene file size (if the user hasn't altered it, don't save anything in the file, and use the default value on load).

    So, is there a way to force it to save such values to file when I do a save in the editor?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Post your code. ;)

    There‘s plenty of things that could be wrong here. Without seeing any code we can only guess, at best, which won‘t be too helpful.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Anything created via script can be saved, provided you dirty it correctly to inform Unity it has changes.

    Dirtying can be done with the
    Undo
    class or some methods within the
    EditorUtility
    class.

    NOTE: these are both editor-only classes in the
    UnityEditor
    namespace.
     
  4. HookedWit

    HookedWit

    Joined:
    Dec 31, 2022
    Posts:
    31
    That sounds just like what I need - thank you - I will check it out
     
  5. HookedWit

    HookedWit

    Joined:
    Dec 31, 2022
    Posts:
    31
    I'm still missing something.

    I looked up SetDirty, and came across EditorApplication.MarkSceneDirty() - that gave me a warning which told me to use EditorSceneManager.MarkAllScenesDirty(). When I ran my script, the scene got marked as changed, so I saved it. But when I reload the changes by script still are not there.

    So I reduced the complexity, to a single parameter:

    public Int16 testValue;

    Got the script to change this, save, reload, displays as zero again.

    There are many instances of this script, each attached to a different gameObject - it looks like none of them are saving this parameter.

    I did initially have an "= 0"; which I removed, in case this was zeroing the value after loading - but that made no difference.

    Ultimately I want to have (fixed size) arrays or Int32[] and string[] included in the save - but for now I can't even get a single integer-per-script instance to save.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Are you modifying a prefab or scriptable object?

    The usual method to use is
    EditorUtility.SetDirty()
    on a asset(s). This dirties the relevant scene if the object is a scene object.

    It's worth checking if the data is being serialised at all, mind you. This is easiest to check via your version control software.
     
    Kurt-Dekker likes this.
  7. HookedWit

    HookedWit

    Joined:
    Dec 31, 2022
    Posts:
    31
    I have a prefab which consists of a GameObject which has my script as a component, and a couple of child GameObjects. I then have a number of instances of that prefab. My editor script is updating individual instances of this (not the prefab definition).

    I can write (different values to each instance of) the parameter in multiple instances of the prefab via the Inspector, save, reload, and the values are correctly retained. Write those values via my script, and they are not.

    Now, I've just seen something weird - which I assume may be what you were hinting at with "if the data is being serialised at all". I was playing around with writing to various data via the inspector, and causing other data in the same script component to be written by script. Somewhere in the middle of all that some of the parameters written by script did get saved - but now I'm trying to reproduce how I did it, without success. So how do I ensure that various parameters get serialised?
     
  8. HookedWit

    HookedWit

    Joined:
    Dec 31, 2022
    Posts:
    31
    Ignore me - it's now working.

    I'd gone so far down the wrong hole I wasn't reading your reply properly.

    You are correct - by using EditorUtility.SetDirty() on each individual instance of my script component, then everything therein gets saved.

    Problem solved - thanks for your patience.
     
    Kurt-Dekker likes this.