- Home /
[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);
}
}
Answer by MakeCodeNow · Oct 08, 2014 at 04:25 AM
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.
I'm unsure of how to use ScriptableObjects inside of a list. I'll have a look at this approach anyway. Thanks for the input :)
No, you want a ScriptableObject that contains the list.
Please mark answer as accepted when you are ready.
understood, when I get it working and others haven't put any other input in. Then I shall mark it :)
There has to be another way of doing it.
The main issue is that your list of Trophies has to be saved somewhere. It could be to X$$anonymous$$L or JSON or an attribute in a component, but you can't just have a list floating in memory and expect it to be saved. All of the members of EditorWindow are ephemeral by design, because they are just tools for editing, not the source of the data to be edited.
Ah now I understand, thanks for that. Going to see what I come up, starting from scratch and using ScriptableObject ins$$anonymous$$d.
Your answer

Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
NPC Spawning after unlocking 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Bind to OnWillSaveAssets and force to save my scene 0 Answers