【问题标题】:How can I detect when changing a List size in Inspector?在 Inspector 中更改列表大小时如何检测?
【发布时间】:2019-05-17 02:21:07
【问题描述】:

这是 Inspector 中 List 3 项的当前大小:

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

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

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. 该按钮覆盖了我已经加载和保存的其他两个按钮,我希望此按钮“添加新项目”始终位于 Canvas 之前和最后一项之后。

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

现在的脚本:

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

【问题讨论】:

  • 可以通过使用自定义检查器中的按钮来实现这一点。与其手动设置数字,不如让按钮递增并使用函数添加一个新的空对象。
  • 否则,您可能必须创建一个脚本来循环并检查更新调用中的重复项。不太确定如何覆盖默认行为本身。
  • 哎呀,错过了。对不起!是的,如果这就是您在编辑器中的全部内容,那么“添加新项目”按钮就是您声明的全部内容。您的其他按钮声明发生了什么变化?
  • 我不确定这一点,但我认为如果它们都试图成为同一对象类型的编辑器,你想将它们结合起来。一个可能会覆盖另一个。至于您的“添加项目”按钮消失,我认为那是因为它被传递了错误的参数。我在 the documentation 中没有看到任何接受 Rectangle 作为参数的内容。
  • 啊,没听懂!谢谢你给我看!

标签: c# unity3d


【解决方案1】:

使用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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-17
    • 2021-12-08
    • 2016-12-31
    • 2011-03-30
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多