[Editor Extensions] Editing Lists and Saving the Data

Hi all,

I’ve been developing an Editor Extension to edit the contents of a list, in my case I’m aiming to have it so designers can easily create trophies for the game.

here is my code for the window, for some reason the data comes up as null. Can someone people explain to me how to achieve this? Think I’m just having a break freeze.

EDIT:

Okay I’m getting somewhere, so far I’ve produced a text box and assigned a value. However it’s data isn’t currently being saved when you close the window and re-open, but rather it becomes something else.

My current code is as follows.

Trophy.cs

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Trophy {

    public int id = 0;
    public string name = "";
    public string description = "";
    public TrophyType type = TrophyType.Bronze;

    public Trophy(int _id, string _name, string _description, TrophyType _type) {
        id = _id;
        name = _name;
        description = _description;
        type = _type;
    }
}

[System.Serializable]
public enum TrophyType {
    Bronze,
    Silver,
    Gold,
    Platinum
}

TrophyEditor.cs

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class TrophyEditor : EditorWindow {

    public List<Trophy> trophies = new List<Trophy>();
    int index = 1;

    [MenuItem("Tools/Trophy Editor")]
    static void ShowWindow() {
        EditorWindow.GetWindow<TrophyEditor>();
    }

    void Awake() {
        trophies.Add(new Trophy(0, "Test", "Description", TrophyType.Bronze));
    }

    void OnGUI() {
        EditorGUILayout.HelpBox("Simple dynamic trophy editor.", MessageType.Info);

        EditorGUILayout.BeginHorizontal();
        foreach (Trophy t in trophies) {
            t.id = EditorGUILayout.IntField(t.id);
            t.name = EditorGUILayout.TextField(t.name);
            t.description = EditorGUILayout.TextField(t.description);
            t.type = (TrophyType)EditorGUILayout.EnumPopup(t.type);
        }
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginVertical();
        if (GUILayout.Button("Add")) {
        }
        EditorGUILayout.EndVertical(); 
        
        Debug.Log(trophies[index - 1].name);
    }
}

You should create a ScriptableObject subclass that contains your list of Trophies and then create an instance of that asset. Then make a custom inspector for that class type. It’ll work very similarly to your existing code, but the ScriptableObject is something that Unity knows how to save. You’ll just want to either use EditorGUI.PropertyField along with SerializedObject’s updateProperties calls or use EditorUtility.SetDirty() if you modify the SerializedObject directly so that Unity knows it’s changed and needs saving.