Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么在加载/保存按钮上出现空异常?_C#_Unity3d - Fatal编程技术网

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

C# 为什么在加载/保存按钮上出现空异常?,c#,unity3d,C#,Unity3d,如果单击Save Conversations(保存对话)或Load Conversations(加载对话)按钮,变量_conversationTrigger为空 using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEditorInternal; using UnityEngine; [CustomEditor(typeof(ConversationTrigger))]

如果单击Save Conversations(保存对话)或Load Conversations(加载对话)按钮,变量_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");

在两个不同的位置有两个类型为ConverstationStrigger的不同对象。一个在ConversationsEditorWindow中实例化,而另一个为null,因为ConversationTriggerEditor中没有赋值


从外观上看,您只需要使用一个ConverstationStrigger。也许有一种解决方案,您可以通过某种构造函数将conversationTrigger的实例注入到您需要的地方。

这些行上只有一个值可以为null:\u conversationTrigger,您永远不会初始化它。