为什么在“加载/保存”按钮上出现空异常?

时间:2019-09-24 16:57:37

标签: c# unity3d

如果我单击“保存对话”或“加载对话”按钮,则变量_conversationTrigger为空。

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

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

    [SerializeField] private ReorderableList conversationsList;

    private SerializedProperty _conversations;

    private int _currentlySelectedConversationIndex = -1;
    private int newSize = 0;
    private Vector2 scrollPos;

    private readonly Dictionary<string, ReorderableList> _dialoguesListDict = new Dictionary<string, ReorderableList>();
    private readonly Dictionary<string, ReorderableList> _sentencesListDict = new Dictionary<string, ReorderableList>();


    public override void OnInspectorGUI()
    {
        if (GUILayout.Button("Edit Conversations"))
        {
            ConversationsEditorWindow myWindow = CreateInstance<ConversationsEditorWindow>();

            myWindow.minSize = new Vector2(1500, 900);
            myWindow.maxSize = new Vector2(1500, 900);
            myWindow.Init(serializedObject);

        }

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

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

在ConversationsEdiotrWindow脚本的顶部,该脚本更长。这是我使用Init方法初始化_conversationTrigger的地方:

在ConversationsEdiotrWindow的_conversationTrigger上使用断点时可以,但是在ConversationTriggerEditor脚本中为空。

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

public class ConversationsEditorWindow : EditorWindow
{
    public SerializedObject conversation = null;

    private ConversationTrigger _conversationTrigger;

    [SerializeField] private ReorderableList conversationsList;

    private SerializedProperty _conversations;

    private int _currentlySelectedConversationIndex = -1;
    private int newSize = 0;
    private Vector2 scrollPos;

    private readonly Dictionary<string, ReorderableList> _dialoguesListDict = new Dictionary<string, ReorderableList>();
    private readonly Dictionary<string, ReorderableList> _sentencesListDict = new Dictionary<string, ReorderableList>();

    private SerializedObject itemcopy;
    private string searchString = "";

    public void Init(SerializedObject _item)
    {
        // Copy the Item targetObject to not lose reference when you
        // click another element on the project window.
        itemcopy = new SerializedObject(_item.targetObject);
        conversation = itemcopy;

        // Other things to initialize the window

        const int width = 1500;
        const int height = 900;

        var x = (Screen.currentResolution.width - width) / 2;
        var y = (Screen.currentResolution.height - height) / 2;

        var window = GetWindow<ConversationsEditorWindow>();
        window.position = new Rect(x, y, width, height);

        _conversationTrigger = (ConversationTrigger)_item.targetObject;
        _conversations = itemcopy.FindProperty("conversations");

1 个答案:

答案 0 :(得分:1)

您在两个不同的地方有两个ConverstationTrigger类型的不同对象。一个在ConverstationsEditorWindow中实例化,另一个在null中实例化,因为在ConversationTriggerEditor中没有分配。

从外观上讲,您只想使用一个ConverstationTrigger。也许有一种解决方案,您可以通过某种构造函数将ConverstationTrigger的实例注入所需的位置。