- Home /
Instancing ScriptableObject at Runtime, loading and saving via JsonUtility
Can I create an Instance of a ScriptableObject at runtime, feed it Data from JsonUtility and stored that Data again via JsonUtility? Would be nice if I could use this hybrid apprach
Comment
Best Answer
Answer by dCalle · Jan 20, 2018 at 11:04 AM
Alrighty, works ;-)
public void TryScriptableSave()
{
SpacerData data = ScriptableObject.CreateInstance<SpacerData>();
data.testName = "ufisudif";
string jsonData = JsonUtility.ToJson(data);
Debug.Log(Application.persistentDataPath);
File.WriteAllText(Application.persistentDataPath + "\\MyFile.json", jsonData);
}
public void TryScriptableLoad()
{
string jsonData = File.ReadAllText(Application.persistentDataPath + "\\MyFile.json");
Debug.Log(jsonData);
SpacerData data = ScriptableObject.CreateInstance<SpacerData>();
JsonUtility.FromJsonOverwrite(jsonData, data);
Debug.Log(data.testName);
}
Your answer
Follow this Question
Related Questions
How to save and load mobs? 0 Answers
When would you use a ScriptableObject? 1 Answer
JSON file not written/read on application exit on android 1 Answer
Saving items and reading their data 1 Answer
Saving and Loading 0 Answers