[BUG] InventorySystemManagerInspector cannot save m_Database

WeiYiHua

Member
I found that when changing the Database of InventorySystemManager in the prefab, it cannot be saved.

The m_Database in the Prefab file is always empty, which leads to the fact that the m_Database of the Prefab in the scene is also empty.

Here's the simple fix:
Code:
/// <summary>
/// Create the Inspector.
/// </summary>
/// <param name="container">The parent container.</param>
protected override void ShowFooterElements(VisualElement container)
{
    var horizontalLayout = new VisualElement();
    horizontalLayout.AddToClassList("horizontal-layout");

    m_DatabaseField = new ObjectField("Database");
    m_DatabaseField.objectType = typeof(InventorySystemDatabase);
    m_DatabaseField.style.flexGrow = 1;
    m_DatabaseField.style.flexShrink = 1;
    
    if (m_InventorySystemManager.Database == null)
    {
        var inventorySystemDatabase = InventoryMainWindow.InventorySystemDatabase;
        if (inventorySystemDatabase != null)
        {
            m_InventorySystemManager.Database = inventorySystemDatabase;
            EditorUtility.SetDirty(m_InventorySystemManager);
        }
    }
    m_DatabaseField.value = m_InventorySystemManager.Database;

    m_DatabaseField.RegisterValueChangedCallback(evt =>
    {
        if (m_InventorySystemManager.Database == evt.newValue) { return; }
        DatabaseChanged();
    });
    horizontalLayout.Add(m_DatabaseField);

    var button = new Button();
    button.text = "Update Scene";
    button.tooltip = "Updates the scene to use the specified database.";
    button.clicked += DatabaseChanged;
    horizontalLayout.Add(button);
    container.Add(horizontalLayout);
}

/// <summary>
/// Update the scene when a new database is set.
/// </summary>
private void DatabaseChanged()
{
    m_InventorySystemManager.Database = m_DatabaseField.value as InventorySystemDatabase;
    EditorUtility.SetDirty(m_InventorySystemManager);

    if (m_InventorySystemManager.Database == null) {
        Debug.LogWarning("The database cannot be set to null.");
        return;
    }

    if (EditorUtility.DisplayDialog("Update Scene Database",
        $"This process will use the database within the Inventory System Manager '{m_InventorySystemManager.Database.name}' " +
        "from the scene component and cannot be reversed. " +
        "Are you sure you want to update the objects to use the selected database? ",
        "Yes",
        "No")) {

        ReplaceDatabaseInSelectedScene(m_InventorySystemManager.Database, 0, 1);
        EditorUtility.ClearProgressBar();
        EditorUtility.DisplayDialog("Scene Database Updated",
            "The scene database was updated. Check the console for any errors.\n\n" +
            "Ensure you also update all of the other scenes and prefabs.",
            "Ok");
    }
}
 
Top