【发布时间】:2019-05-14 04:01:09
【问题描述】:
我有一个带有列表的脚本:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class Test : MonoBehaviour
{
public List<Conversation> conversations = new List<Conversation>();
当只使用编辑器脚本而不是 EditorWindow 时,我可以这样做:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
[CustomEditor(typeof(Test))]
public class TestTriggerEditor : Editor
{
private SerializedProperty _conversations;
private void OnEnable()
{
_conversations = serializedObject.FindProperty("conversations");
}
但现在我想在 EditorWindow 脚本中执行此操作:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
public class TestingEditorWindow : EditorWindow
{
private SerializedProperty _conversations;
[MenuItem("Testing/Editor")]
private static void ConversationsSystem()
{
const int width = 340;
const int height = 420;
var x = (Screen.currentResolution.width - width) / 2;
var y = (Screen.currentResolution.height - height) / 2;
GetWindow<TestingEditorWindow>().position = new Rect(x, y, width, height);
}
private void OnGUI()
{
_conversations = serializedObject.FindProperty("conversations");
}
}
但我在 OnGUI 内的行上出现错误:
_conversations = serializedObject.FindProperty("conversations");
错误出现在 serializedObject 上:
当前上下文中不存在名称“serializedObject”
如果我将其更改为 SerializedObject,则会出现错误:
非静态字段、方法或属性“SerializedObject.FindProperty(string)”需要对象引用
我尝试将 _conversations 更改为静态,但没有帮助。
我也尝试过:
SerializedObject serializedObject = new SerializedObject(
但不确定如何使用它。为什么在 Editor 脚本中它工作正常,但在 EditorWindow 中却不行?
【问题讨论】:
-
@RetiredNinja 首先
EditorWindow继承自ScriptableObject,后者已经是可序列化的。其次,这并没有改变 EditorWindow 根本没有这样的属性的事实。原因见我的answer