Таким образом, вопрос, похоже, на самом деле: почему список в окне Editor не обновляется?
Как я полагаю, я уже говорил вам однажды, прежде чем использовать SerializedProperty
в пользовательских скриптах редактора, а не изменять напрямуюЗначения компонентов без маркировки объекта как dirty .
[CustomEditor(typeof(ConversationTrigger))]
public class ConversationTriggerEditor : Editor
{
private Vector2 scrollPos;
private SerializedProperty conversations;
private ConversationTrigger conversationTrigger;
private void OnEnable()
{
conversations = serializedObject.FindProperty("conversations");
conversationTrigger = (ConversationTrigger)target;
}
public override void OnInspectorGUI()
{
scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Height(250));
DrawDefaultInspector();
EditorGUILayout.EndScrollView();
// Load the current values from the real component into the serialized copy
serializedObject.Update();
if (GUILayout.Button("Add new conversation"))
{
conversations.arraySize++;
}
GUILayout.Space(10);
if (conversations.arraySize != 0)
{
if (GUILayout.Button("Remove conversation"))
{
if (conversations.arraySize > 0) conversations.arraySize--;
}
}
GUILayout.Space(100);
if (GUILayout.Button("Save Conversations"))
{
conversationTrigger.SaveConversations();
}
GUILayout.Space(10);
if (GUILayout.Button("Load Conversations"))
{
// Depending on what this does you should consider to also
// change it to using the SerializedProperties!
Undo.RecordObject(conversationtrigger, "Loaded conversations from JSON");
conversationTrigger.LoadConversations();
}
serializedObject.ApplyModifiedProperties();
}
}
Как вы можете видеть, прокрутка отлично работает для меня:
![](https://i.stack.imgur.com/9YWcO.gif)
И еще раз, я могу только настоятельно рекомендовать использовать ReorderableList
для того, что вы делаете.Его немного сложнее настроить, но он чрезвычайно мощный:
[CustomEditor(typeof(ConversationTrigger))]
public class ConversationTriggerEditor : Editor
{
private Vector2 scrollPos;
private SerializedProperty conversations;
private ConversationTrigger conversationTrigger;
private ReorderableList conversationList;
private void OnEnable()
{
conversations = serializedObject.FindProperty("conversations");
conversationTrigger = (ConversationTrigger)target;
conversationList = new ReorderableList(serializedObject, conversations)
{
displayAdd = true,
displayRemove = true,
draggable = true,
drawHeaderCallback = rect =>
{
EditorGUI.LabelField(new Rect(rect.x, rect.y, 100, rect.height), "Conversations", EditorStyles.boldLabel);
var newSize = EditorGUI.IntField(new Rect(rect.x + 100, rect.y, rect.width - 100, rect.height), conversations.arraySize);
conversations.arraySize = Mathf.Max(0, newSize);
},
drawElementCallback = (rect, index, isActive, isSelected) =>
{
var element = conversations.GetArrayElementAtIndex(index);
var name = element.FindPropertyRelative("Name");
// do this for all properties
var position = EditorGUI.PrefixLabel(rect, new GUIContent(name.stringValue));
EditorGUI.PropertyField(position, name);
},
elementHeight = EditorGUIUtility.singleLineHeight
};
}
public override void OnInspectorGUI()
{
// Load the current values from the real component into the serialized copy
serializedObject.Update();
scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Height(250));
GUILayout.Space(10);
conversationList.DoLayoutList();
EditorGUILayout.EndScrollView();
GUILayout.Space(100);
if (GUILayout.Button("Save Conversations"))
{
conversationTrigger.SaveConversations();
}
GUILayout.Space(10);
if (GUILayout.Button("Load Conversations"))
{
Undo.RecordObject(conversationtrigger, "Loaded conversations from JSON");
conversationTrigger.LoadConversations();
}
serializedObject.ApplyModifiedProperties();
}
}
![enter image description here](https://i.stack.imgur.com/C6Mt9.gif)
И используйте это, чтобы исправить EditorWindow
public class ConversationsEditorWindow : EditorWindow
{
private ConversationTriggerEditor editor;
[MenuItem("Conversations/Conversations System")]
static void Init()
{
const int width = 800;
const int height = 800;
var x = (Screen.currentResolution.width - width) / 2;
var y = (Screen.currentResolution.height - height) / 2;
var window = GetWindow<ConversationsEditorWindow>();
var ff = FindObjectOfType<ConversationTrigger>();
window.position = new Rect(x, y, width, height);
window.editor = (ConversationTriggerEditor)Editor.CreateEditor(ff);
}
void OnGUI()
{
editor.OnInspectorGUI();
}
}