Serialize properties of the custom editor itself
Goal
I have a CSV file which holds data for multiple fields, which names are in the header (1st line). My goal is to pass the CSV file as a TextAsset
to my Editor, let it parse the header, and create a popup menu below it in the inspector to select which field (aka column id) to choose to read. When the application plays, the columnId is used by the MonoBehavior script to read the data in that column at Start
, then print it at Update
:
Example
public class DataReader: MonoBehaviour {
public string[] lineArray; // one string for each line of the csv file
public int columnId = 0; // defines which column aka field to read
private List<float> columnValues; // values for the selected field
private int lineId = 0; // defines which line aka value to read
void Start() {
// Fill up columnValues given the array of lines and the id of the column to read
columnValues = new List<float>(lineArray.Length - 1);
for (lineId = 1; lineId < lineArray.Length; ++lineId {
string[] values = linesArray.Split(',');
string value = values[columnId];
columnValues.Add(float.Parse(value));
}
}
void Update() {
// Just loop through the values for the selected field and print them
if (lineId >= values.Length) {
lineId = 0;
}
print(columnValues[lineId++])
}
}
[CustomEditor(typeof(DataReader))]
public class DataReaderEditor : Editor {
private DataReader reader;
private TextAsset CSVInput;
private void OnEnable() {
reader = target as DataReader;
}
public void OnInspectedGUI() {
CSVInput = EditorGUILayout.ObjectField("CSV Input",
CSVInput,
typeof(TextAsset),
true) as TextAsset;
reader.lines = CSVInput.text.Split("\n");
string firstLine = reader.lines[0];
string[] headers = firstLine.Split(',');
reader.columnId = EditorGUILayout.Popup("Field",
reader.columeId,
headers);
}
Results
The parsing of the CSV file (simplified here since it's not the topic of the issue), the creation of the popup UI to show the name of the fields, passing the line data and selected field index to the reader for it to read all work great:
Problem
My issue with this example is that DataReaderEditor.CSVInput
does not persist when I use the Unity Editor: as soon as I select another object or play the application, the TextAsset field for CSVInput is empty in the inspector, even though DataReader.lineArray
is properly serialized and set and DataReader then reads the proper values.
I know I should probably use SerializedObjects instead in general, but even with that, my goal is not to serialize more properties for DataReader
which works well, but for DataReaderEditor
to maintain the TextAsset
input and not reset it to nil all the time. I guess the issue comes from the fact that I store a property in the Editor that is not in the MonoBehavior, but that instead the Editor pre-processes the that TextAsset
property into a string array that is enough for the DataReader
, and I'd like to keep it that way if possible (not keep track of the TextAsset
in DataReader
since we don't need it).
Thank you!
Your answer
Follow this Question
Related Questions
EditorGUILayout.BeginVertical("Box") not filling the inspector 0 Answers
Unity Custom Editor: 1 True Bool Only Within A Group 1 Answer
Custom inspector - How to serialize scene objects in ScriptableObject? 0 Answers
No type in ObjectField created in uxml (UIElements) 3 Answers
Why does Unity fail to read CSV exported from Excel 2010? 2 Answers