如果我单击“保存对话”或“加载对话”按钮,则变量_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");
答案 0 :(得分:1)
您在两个不同的地方有两个ConverstationTrigger
类型的不同对象。一个在ConverstationsEditorWindow
中实例化,另一个在null
中实例化,因为在ConversationTriggerEditor
中没有分配。
从外观上讲,您只想使用一个ConverstationTrigger
。也许有一种解决方案,您可以通过某种构造函数将ConverstationTrigger
的实例注入所需的位置。