Daniel Lip
Daniel Lip

Reputation: 11325

How can I detect when changing a List size in Inspector?

This is the current size of the List 3 items in the Inspector :

List in Inspector

When changing the size for example to 4 it's duplicating the last item but I want it to add a new empty Conversation item : There are two Locked Room items :

Last item is duplicated instead creating a new conversation item

This is how I declared the List :

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEditorInternal;

public class ConversationTrigger : MonoBehaviour
{
    public List<Conversation> conversations = new List<Conversation>();

What I tried so far :

Created a Editor script with a button :

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

[CustomEditor(typeof(ConversationTrigger))]
public class ConversationTriggerEditor : Editor
{
    private SerializedProperty _conversations;

    private void OnEnable()
    {
        _conversations = serializedObject.FindProperty("conversations");
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        if(GUILayout.Button("Add new item"))
        {

        }
    }
}

But now I'm having two problems :

  1. The button is override the other two buttons I already had Load and Save and I want this button the "Add new item" to be position always before the Canvas and after the last item.

  2. How do I increment the List inside the button event in the editor script ?

after changes

The script as it is now :

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

[CustomEditor(typeof(ConversationTrigger))]
public class ConversationTriggerEditor : Editor
{
    private ConversationTrigger conversationtrigger;

    private void OnEnable()
    {
        conversationtrigger = FindObjectOfType<ConversationTrigger>();
    }

    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        if(GUI.Button(new Rect(0.2f,0.3f,0,0), "Add new item"))
        {
            conversationtrigger.conversations.Add(new Conversation());
        }
    }
}

I changed the button to GUI.Button instead GUILayout.Button to set the position of the button.

But the button now is gone vanished and also the other two buttons from my first top screenshot the Load an Save also gone vanished.

This is the script for the load and save buttons :

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

[CustomEditor(typeof(ConversationTrigger))]
public class SaveConversationsButton : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        ConversationTrigger dialoguetrigger = (ConversationTrigger)target;

        if (GUILayout.Button("Save Conversations"))
        {
            dialoguetrigger.SaveConversations();
        }

        if(GUILayout.Button("Load Conversations"))
        {
            Undo.RecordObject(dialoguetrigger, "Loaded conversations from JSON");
            dialoguetrigger.LoadConversations();
        }
    }
}

Upvotes: 1

Views: 1346

Answers (1)

Eliasar
Eliasar

Reputation: 1074

Use MonoBehaviour.OnValidate() method

Edited from this blog post:

using UnityEngine;
using System.Collections;

public class OnValidateExample : MonoBehaviour
{
    public List<Conversation> conversations;
    private int previousConversationsCount;
    private int currentConversationsCount;
 
    void OnValidate()
    {
        currentConversationsCount = conversations.Count();

        if (previousConversationsCount != currentConversationsCount)
            Debug.Log($"Conversations count has changed: {previousConversationsCount} => {currentConversationsCount}");

        previousConversationsCount = currentConversationsCount;
    }
}

Upvotes: 3

Related Questions