- Home /
Custom Assets do not save data that is not primitive
I'm trying to make a simple quest system with a custom editor. All primitive data types are saved, such as ints, strings, etc. My quest class has a struct however that is called a stage, and a dictionary with a key of an int and value of a stage. When restarting unity/playing the game in unity the data for the quest will not stay saved as the dictionary is lost.
I've looked into this in many places however all solutions I've found do not work, i've tried setting the file as dirty, and registering an undo however that does not work.
This is how I first initialize the quest, and then save it in my custom editor.
[MenuItem("Window/Quest Editor")]
public static void ShowWindow() {
GetWindow(typeof(QuestEditor), true, "Quest Editor");
quest = CreateInstance<Quest>();
stageIDs = new List<int>();
stages = new List<Quest.Stage>();
Quest.Stage st = new Quest.Stage();
st.QuestObject = new List<IQuestObject>();
stages.Add(st);
stageIDs.Add(0);
nname = "";
desc = "";
stageAmmount = 1;
openedQuest = false;
}
///////////////////////////////////////////////
private void SaveQuest() {
Dictionary<int, Quest.Stage> st = new Dictionary<int, Quest.Stage>();
for(int i = 0; i < stageAmmount; i++) {
st.Add(stageIDs[i], stages[i]);
}
EditorUtility.SetDirty(quest);
quest.stages = st;
quest.Name = nname;
quest.DescriptionSummary = desc;
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (!openedQuest) {
if (path == "") {
path = "Assets";
} else if (Path.GetExtension(path) != "") {
path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
}
AssetDatabase.CreateAsset(quest, path + "/" + nname + ".asset");
} else {
path = lastPathLoc;
Quest tosave = AssetDatabase.LoadMainAssetAtPath(path) as Quest;
EditorUtility.CopySerialized(quest, tosave);
}
EditorUtility.SetDirty(AssetDatabase.LoadMainAssetAtPath(path));
AssetDatabase.SaveAssets();
//EditorUtility.FocusProjectWindow();
}
This does not seem to work however, and I am unsure how to get it to work. The only thing I can think of is to write the entire instance to a binary file, and load it when needed, which would be a lot more work than I think is actually necessary.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why does AssetDatabase.SaveAssets() overwrite QualitySettings? 0 Answers
Making a bubble level (not a game but work tool) 1 Answer
Custom Editor value change 0 Answers