Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SerializedObject and ScriptableObject connection, update file without closing Unity

Discussion in 'UI Toolkit' started by cemleme, Oct 24, 2019.

  1. cemleme

    cemleme

    Joined:
    Mar 24, 2017
    Posts:
    30
    Hello,

    I am writing a custom editor using EditorWindow and UIElements

    I pick a scriptableobject file from path, assign it into a SerializedObject, make changes, ApplyModifiedProperties to the serializedObject and everything works fine.

    I am following the process on git for file changes.

    What happens is when I close the Unity Editor, everything is written into the scriptable object file so it works perfectly.

    the thing is the changes are not applied to the file before closing the Unity Editor.

    it changes when I rename the file (as expected it's a new file with same values)

    I just want to learn why it works this way and if I missed a step.

    Thanks
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    This is independent of UIElements but you have to explicitly call AssetDatabase.SaveAssets() manually if you want your changes to the ScriptableObject (that represents an asset) to save to disk without the user actually pressing Ctrl-S.

    Also, have you tried the binding system in UIElements? You can used `PropertyField` element with a bindingPath to a data member in the ScriptableObject, and then call Bind(yourScriptableObject) on the root element to bind the UI and auto-generate all data fields. This will remove the need to manually register for ChangeEvents and worrying about ApplyModifiedProperties().

    https://docs.unity3d.com/Manual/UIE-Binding.html