Is there a way to use serializedObject in EditorWindow script ?

I have a script with a List :

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>();

And when using only Editor script I could do :

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");
    }

But now I want to do it in a EditorWindow script :

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");
    }
}

But I’m getting error on the line inside the OnGUI :

_conversations = serializedObject.FindProperty("conversations");

The error is on the serializedObject :

The name ‘serializedObject’ does not exist in the current context

If I’m changing it to SerializedObject I’m getting error :

An object reference is required for the non-static field, method, or property ‘SerializedObject.FindProperty(string)’

I tried to change the _conversations to be static but it didn’t help.

you need to define what the serializedObject is, seomthing like
SerializedObject serializedObject = new SerializedObject( //Find<Test>// );

When you use CustomEditors, this step is already done for you by the system

2 Likes

I’m talking about EditorWindow not CustomEditors
And I tried to use your idea without success so far.

yeah, in your example when you did use a custom editor, this step is handled for you.
In an editor window, you need to set the reference serializedObject yourself
So you can either use the Find functions to get your Test component, or maybe use an ObjectField

1 Like