- Home /
Problem is not reproducible or outdated
Undo.Record: Recording variables within array elements
I created an custom editor button that takes an TextField and creates a new Element in an array.
The array elements are the following class:
[System.Serializable]
public class NPCDialogueSettings {
public string name;
public int order;
[Header("Flags")]
public FlagNode[] flagsToCheck; //If these conditions are met the dialogue can be executed
public FlagNode[] flagsToSet; //After dialogue these flags are set true or false
[Header ("Execution Conditions")]
public bool playOnce = false;
}
I can easily create a new element in the array and save it using Undo.RecordObject. But changing a variable in one of the elements is not saved.
Is there a way to save variables in array elements (Which is a class) using Undo.RecordObject or some other method?
More complete code of the actual function: (Note that I am trying to save: Name and Order)
//==================Create new DialogueSettings in NPC Dialogue Controller==================
NPCDialogueSettings[] dialogueSettingsArray = dialogueControllerRef.dialogueSettings;
NPCDialogueSettings[] tempDialogueSettingsArray = new NPCDialogueSettings[dialogueSettingsArray.Length + 1];
for (int i = 0; i < dialogueSettingsArray.Length; i++) {
tempDialogueSettingsArray[i] = dialogueSettingsArray[i];
tempDialogueSettingsArray[i].order = i;
}
int lastElement = tempDialogueSettingsArray.Length -1;
//Create new NPCDialogue on last element
tempDialogueSettingsArray[lastElement] = new NPCDialogueSettings();
//Set the vars in new dialogue
tempDialogueSettingsArray[lastElement].name = newDialogueName;
tempDialogueSettingsArray[lastElement].order = dialogueSettingsArray.Length;
//Record
Undo.RecordObject(dialogueControllerRef, "Record NPC Dialogue Controller");
//Set the new array
dialogueControllerRef.dialogueSettings = tempDialogueSettingsArray;
Hmm, I think we may need some clarification, not quite clear what this means: "But changing a variable in one of the elements is not saved."
Okay, so I have a array in a gameobject. That array consists out of the NPCDialogueSettings (The first code above).
The editor button Adds an array element to the list (By copying the existing array and just making it one bigger (Dind't want a list)
If I do for example: tempDialogueSettingsArray[AnyElement].name = "Some string"
Than replace the existing string (dialogueControllerRef.dialogueSettings = tempDialogueSettingsArray;) The elements amount is saved (Thus the array is now 1 element bigger). The name also changed but is reset if pressing play and going back to editor again. (So the record doesn't work).
Now I am looking a lot as this: https://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html
And I know I probably have to use SerializedObject or the likes but can't seem to wrap my head around it and can't find proper documentation.
Do those array's exist in the EDITOR's class? Those are NOT automatically saved like say.. a scene or pre-fab object's data would be. Rather they are reinitialized on load or play.
I have a similar problem - I want to replace a group of objects that a user has defined via an array of game objects.
User populates an array via an editor window
User drags the object they want to use as a replacement
User hits 'replace' and the array of objects is replaced by copies of the replacement
This works great, but when they undo the array is left with $$anonymous$$issing References. (even though the object replacements are properly undone.
Follow this Question
Related Questions
Custom Editor + Array = A Long Nightmare ! 1 Answer
Custom Editor for array of custom class 1 Answer
Custom editor array without serialized object 2 Answers
Undo.RecordObject doesn't work 2 Answers
Custom Editor serializing data in prefab instance. 0 Answers