Save a prefab from editor code
Hello
I have a problem with saving a prefab I am editing via custom Editor Window. The object is meant to be a database, to which I am creating a simple GUI for editing. Everything is fine except I can't seem to really save anything (after I exit Unity, changes are gone). I tried saving the prefab by adding a new one and overwriting the older, but this made my dictionary lose data.
Here's the code (it only adds a few options, since I started with saving issues):
public class SkillsEditor : EditorWindow {
string newSkillId = "";
SkillType browseTypes;
string[] skillsList;
string currentSkill = "";
int currentSkillIndex;
SkillData data = null;
[MenuItem("TFG/Editors/SkillsEditor")]
public static void ShowWindow() {
GetWindow(typeof(SkillsEditor));
}
void OnGUI() {
SkillsManager manager = Resources.Load("Managers/SkillsManager", typeof(SkillsManager)) as SkillsManager;
if (manager.container == null)
manager.container = new System.Collections.Generic.Dictionary<string, SkillData>();
GUILayout.BeginHorizontal();
EditorGUILayout.LabelField("New skill index:", new GUILayoutOption[] { GUILayout.Width(100) });
newSkillId = EditorGUILayout.TextField(newSkillId, new GUILayoutOption[] { GUILayout.Width(150) });
if (newSkillId.Length > 0)
if (GUILayout.Button("Add skill", new GUILayoutOption[] { GUILayout.Width(75) })) {
if (!manager.container.ContainsKey(newSkillId))
manager.container[newSkillId] = new SkillData();
currentSkill = newSkillId;
newSkillId = "";
}
GUILayout.EndHorizontal();
if (manager.container != null && manager.container.Count > 0) {
EditorGUILayout.BeginHorizontal();
browseTypes = (SkillType)EditorGUILayout.Popup((int)browseTypes, new string[] { "Passives", "Auras", "Self targeted", "Projectiles", "Instants" });
skillsList = manager.container.Where(skill => skill.Value.type == browseTypes).Select(kvp => kvp.Key).ToArray();
if (skillsList.Length != 0) {
if (currentSkill.Length > 0) {
for (int i = 0; i < skillsList.Length; ++i)
if (skillsList[i] == currentSkill) {
currentSkillIndex = i;
break;
}
}
currentSkillIndex = EditorGUILayout.Popup(currentSkillIndex, skillsList);
currentSkill = skillsList[currentSkillIndex];
}
EditorGUILayout.EndHorizontal();
}
if (currentSkill.Length > 0) {
Debug.Log(manager.container.Count);
data = manager[currentSkill];
data.type = (SkillType)EditorGUILayout.Popup((int)data.type, new string[] { "Passive", "Auras", "Self targeted", "Projectile", "Instant" });
manager.container[currentSkill] = data;
}
GUILayout.Space(10);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Save", new GUILayoutOption[] { GUILayout.Width(150), GUILayout.Height(20) })) {
SkillsManager temp = Instantiate(manager);
Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/Resources/Managers/SkillsManager.prefab");
PrefabUtility.ReplacePrefab(temp.gameObject, prefab, ReplacePrefabOptions.ReplaceNameBased);
DestroyImmediate(temp.gameObject);
}
GUILayout.Space(20);
if (GUILayout.Button("Delete", new GUILayoutOption[] { GUILayout.Width(150), GUILayout.Height(20) })) {
}
GUILayout.EndHorizontal();
}
}
The thing is - after hitting "Save" button my dictionary is cleared of values - despite the fact, that it ought not to (by my expectations, at least). When I added a simple string field to my SkillsManager class and changed it with this code, it saved perfectly and the change persisted through turning Unity off and on again. But here? I get an error in this part:
if (currentSkill.Length > 0) {
Debug.Log(manager.container.Count);
data = manager[currentSkill];
data.type = (SkillType)EditorGUILayout.Popup((int)data.type, new string[] { "Passive", "Auras", "Self targeted", "Projectile", "Instant" });
manager.container[currentSkill] = data;
}
Is gives me the null pointer exception in line "data.type = ...", because, apparently, the managet.container dictionary is empty (despite it was not before saving).
Is it because I use my own class, the dictionary or the generic types? SkillsManager is derived from a generic class that holds a simple dictionary and overloads the [] operator for my general convinience. I tried removing the dictionary, but to no avail - i had two lists, one of string, second of "T" values. The second one was cleared, the strings persisted. Then I tried removing the generic types and made SkillsManager not inherit. Still no do.
Any ideas here?
If anybody asks, the SkillData class holds mainly floats and strings, but also one Dictionary, one enum, one sprite and two members of my own type (which holds some arrays, a dictionary, enum and a member of SkillData type).
Answer by Adam-Mechtley · Oct 27, 2016 at 07:36 AM
It doesn't necessarily answer your specific question, but it seems like you could probably bypass a lot of the headaches here by not having a custom EditorWindow that is loading and replacing a prefab in Resources, but instead just make a custom Editor for your SkillsManager class so you can just pick it in the project and directly make edits in its inspector.
Answer by TehFisharmahn · Oct 27, 2016 at 11:55 AM
Well, yes, I agree. But that was an idea.
Anyway, i changed it. Now I simply serialize everything to json and the manager only loads, holds and saves the data. This way I don't need the prefab.
So, If anyone this same specific idea - I didn't manage to do it. Just found a better solution.
Your answer
![](https://koobas.hobune.stream/wayback/20220612095456im_/https://answers.unity.com/themes/thub/images/avi.jpg)