在Inspector中更改列表大小时如何检测?

时间:2019-05-16 16:22:34

标签: c# unity3d

这是检查器中列表3项的当前大小:

List in Inspector

例如,将大小更改为4时,它会复制最后一个项目,但我希望它添加一个新的空对话项目: 锁定房间有两个物品:

Last item is duplicated instead creating a new conversation item

这就是我声明列表的方式:

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>();

到目前为止我尝试过的事情:

使用按钮创建编辑器脚本:

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"))
        {

        }
    }
}

但是现在我有两个问题:

  1. 该按钮将覆盖我已经具有“加载”和“保存”的其他两个按钮,并且希望该按钮“添加新项目”始终位于“画布”之前和最后一个项目之后。

  2. 如何在编辑器脚本的按钮事件内增加列表?

after changes

现在的脚本:

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());
        }
    }
}

我将按钮更改为GUI.Button而不是GUILayout.Button来设置按钮的位置。

但是现在该按钮消失了,而我的第一个顶部屏幕截图中的另外两个按钮“加载保存”也消失了。

这是加载和保存按钮的脚本:

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();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

使用MonoBehaviour.OnValidate()方法

根据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;
    }
}