VeriorPies / ParrelSync

(Unity3D) Test multiplayer without building
MIT License
4.77k stars 328 forks source link

[BUG] ScriptableObject do not sync when being edited by custom editor #82

Closed Bl4ck-orig closed 2 years ago

Bl4ck-orig commented 2 years ago

Describe the bug When an asset created by a class deriving from ScriptableObject is changed by a custom editor, the changes are not synced.

To Reproduce Steps to reproduce the behavior:

  1. Create new Unity Project 2020.3.36f1.
  2. Create File "ParalSyncEditorBug.cs" and copy code into file at the bottom.
  3. Create asset by right clicking in Project Window -> create/ParalSyncEditorBug.
  4. Create entry in the created asset for "Test Variable".
  5. Clone Project.
  6. Click Clear Data Button.
  7. Save File & Project.

Expected behavior The asset is synced in both projects.

Screenshots/Video ParelSyncEditorBug

Enviroment (please complete the following information):

Additional context ParalSyncEditorBug.cs:

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CreateAssetMenu(fileName = "ParalSyncEditorBug", menuName = "ParalSyncEditorBug")]
public class ParalSyncEditorBug : ScriptableObject
{
    [SerializeField] private List<int> testVariable;

    public void ClearData() => testVariable = null;
}

[CustomEditor(typeof(ParalSyncEditorBug))]
public class ParalSyncEditorBugEditor : Editor
{
    public override void OnInspectorGUI()
    {
        ParalSyncEditorBug test = (ParalSyncEditorBug)target;

        DrawDefaultInspector();

        if (GUILayout.Button("Clear Data"))
            test.ClearData();
    }
}
314pies commented 2 years ago

Hi Bl4ck-orig,

It seems you didn't mark the scriptable object as dirty after you cleared the data. Can you try to add EditorUtility.SetDirty(test); after the line test.ClearData();?

The final result should be somehow like this:

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CreateAssetMenu(fileName = "ParalSyncEditorBug", menuName = "ParalSyncEditorBug")]
public class ParalSyncEditorBug : ScriptableObject
{
    [SerializeField] private List<int> testVariable;

    public void ClearData() => testVariable = null;
}

[CustomEditor(typeof(ParalSyncEditorBug))]
public class ParalSyncEditorBugEditor : Editor
{
    public override void OnInspectorGUI()
    {
        ParalSyncEditorBug test = (ParalSyncEditorBug)target;

        DrawDefaultInspector();

        if (GUILayout.Button("Clear Data")) {
            test.ClearData();
            EditorUtility.SetDirty(test);
        }
    }
}
Bl4ck-orig commented 2 years ago

Hello.

Did not know about this function! My bad.

Thank you!

Cheers.