EditorWindow

I am trying to access [SerializabeFields] from a custom editor Window of typeOf(BoidSpawn) (my script which includes these fields… boidCount for example)

But I am unsure to access them from the editor window

In custom editor window, you can create a new SerializedObject(target) and put a reference to an instance of the BoidSpawn script.Then you have a serializedObject that you can use the same way you would in any custom editor.

Does this help? Or would you like to see an example?

An example would be great (I just always find it much easier to understand) thank you for your help

Here you go: Given a simple component that lives on a prefab or GameObject in a scene:

using UnityEngine;

public class BoidSpawn : MonoBehaviour
{
    public int boidCount;
}

You can then edit an instance of this component with a custom editor window like so:

using UnityEditor;

public class BoidSpawnEditorWindow : EditorWindow
{
    [MenuItem("Tools/Boid Spawn Window")]
    public static void ShowWindow()
    {
        GetWindow<BoidSpawnEditorWindow>("Boid Spawn");
    }

    private SerializedObject serializedObject;

    private void OnEnable()
    {
        // When the editor window opens, try to find a
        // target to edit from the current selection.
        serializedObject = FindTarget();
    }

    private void OnSelectionChange()
    {
        // Update the target when the selection changed.
        serializedObject = FindTarget();

        // Windows only refresh if the mouse moves
        // or other actions are performed by the user/
        // So, manually refresh in this case.
        Repaint();
    }

    private SerializedObject FindTarget()
    {
        // Here we find the target instance to edit via the editor selection.
        // This could also be set via an ObjectField control or other sources.
        // We could pass 'this' editor window instance as a target if the fields
        // would live within this class.
        if (Selection.activeGameObject != null)
        {
            BoidSpawn target = Selection.activeGameObject.GetComponentInChildren<BoidSpawn>();
            if (target != null)
                return new SerializedObject(target);
        }
        return null;
    }

    private void OnGUI()
    {
        if (serializedObject != null)
        {
            // Load data from the script instance to the serialized object.
            serializedObject.Update();

            // Find simple properties and draw them one by one:
            SerializedProperty boidCountProp = serializedObject.FindProperty("boidCount");
            EditorGUILayout.PropertyField(boidCountProp);
        
            // Apply data from the serialized object back to the script instance.
            serializedObject.ApplyModifiedProperties();
        }
    }
}

If you simply want to draw a complete default inspector as shown in the normal inspector window, you can use Unity’s editor like this:

using UnityEngine;
using UnityEditor;

public class BoidSpawnEditorWindow : EditorWindow
{
    [MenuItem("Tools/Boid Spawn Window")]
    public static void ShowWindow()
    {
        GetWindow<BoidSpawnEditorWindow>("Boid Spawn");
    }

    private Editor boidEditor;

    private void OnEnable()
    {
        RefreshEditor();
    }

    private void OnSelectionChange()
    {
        RefreshEditor();
        Repaint();
    }

    private void RefreshEditor()
    {
        Object target = FindTarget();
        if (target != null)
            Editor.CreateCachedEditor(target, editorType: null, ref boidEditor);
        else
            boidEditor = null;
    }

    private Object FindTarget()
    {
        if (Selection.activeGameObject != null)
            return Selection.activeGameObject.GetComponentInChildren<BoidSpawn>();

        return null;
    }

    private void OnGUI()
    {
        if (boidEditor != null)
            boidEditor.OnInspectorGUI();
    }
}

6389840--712274--upload_2020-10-6_21-25-46.png

1 Like