- Home /
how to save and load component values from unity editor?
Problem:
I have a component with several input fields., and want to save that as JSON and load it when i need it again..
Here is how the scripts are:
Products.cs:
using UnityEngine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
[Serializable]
public class ProductsYo {
public enum ProductCategory {
FabricSofas,
WoodenSofas,
Leather_LeatheretteSofas
}
public enum VariantName{
Blue,
Red,
Black,
Brown,
BlahBlahBlah
}
public string ProductName;
public ProductCategory SelectProductCategory;
public List<Seater> Sets = new List<Seater>();
}
[Serializable]
public class Seater{
public String SetName;
public List<PrefabInfo> Variants = new List<PrefabInfo>();
}
[Serializable]
public struct PrefabInfo {
public string VariantName;
public GameObject Prefab;
public Sprite VariantIcon;
}
ProductsDetails.cs
is attached to an object:
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEditor;
public class ProductsDetails : MonoBehaviour {
public List<ProductsYo> Products = new List<ProductsYo>();
}
[CustomEditor(typeof(ProductsDetails))]
public class ObjectBuilderEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
ProductsDetails myScript = (ProductsDetails)target;
if(GUILayout.Button("Load JSON"))
{
return;
}
if(GUILayout.Button("Save JSON"))
{
return;
}
}
}
Here is how it looks when attached:
When i click on save JSON, it has to save all the data to JSON in the editor itself, when i click on load JSON, it has to load and re-assign all the data in the editor..
Can anyone please help me do save and load JSONs?
Final JSON format to appear:
[
{
"ProductName": "Product Name Here",
"SelectProductCategory": "Fabric Sofas",
"Sets": [
{
"SetName": "Seater 1",
"Variants": [
{
"VariantName": "White",
"Prefab": "Assets\\Resources\\Prefabs\\white.prefab",
"VariantIcon": "Assets\\Resources\\Icons\\white.png"
},
{
"VariantName": "Black",
"Prefab": "Assets\\Resources\\Prefabs\\black.prefab",
"VariantIcon": "Assets\\Resources\\Icons\\black.png"
}
]
}
]
}
]
Answer by Multirezonator · Nov 28, 2017 at 01:13 PM
UnityEditor can to serialize objects to json, but links to other assets will be something like this:
"TargetObject":{"fileID":22459490,"guid":"a79390e1231faed4e883da331190a0cd","type":2}
if the readability of the file does not matter:
public override void OnInspectorGUI()
{
DrawDefaultInspector();
ProductsDetails myScript = (ProductsDetails)target;
if (GUILayout.Button("Load JSON"))
{
StreamReader sr = new StreamReader("Assets/Serialized.txt");
EditorJsonUtility.FromJsonOverwrite(sr.ReadToEnd(), myScript);
sr.Close();
return;
}
if (GUILayout.Button("Save JSON"))
{
StreamWriter sw = new StreamWriter("Assets/Serialized.txt");
sw.Write(EditorJsonUtility.ToJson(myScript));
sw.Close();
return;
}
}
I'm getting this error when i click on Save or Load JSON
IOException: Sharing violation on path D:\UrbanLadderProjects\AR\UL-LivingSpacesWithGit\Assets\Serialized.txt
System.IO.FileStream..ctor (System.String path, File$$anonymous$$ode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320)
System.IO.FileStream..ctor (System.String path, File$$anonymous$$ode mode, FileAccess access, FileShare share)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.File$$anonymous$$ode,System.IO.FileAccess,System.IO.FileShare)
System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamWriter.cs:124)
System.IO.StreamWriter..ctor (System.String path)
(wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string)
ObjectBuilderEditor.OnInspectorGUI () (at Assets/ProductsDetails.cs:30)
UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/Inspector/InspectorWindow.cs:1231)
UnityEditor.DockArea:OnGUI()
Am i missing something?
An IOException can be thrown when the file is already open. This might mean you are opening it in two places in code. It might also mean you have it open in another editor. I have tested this script, in my case all works fine.
actually, i first get this error
$$anonymous$$issing$$anonymous$$ethodException: Cannot find the requested method.
UnityEditor.EditorJsonUtility.ToJson (UnityEngine.Object obj) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorJsonUtilityBindings.gen.cs:17)
ObjectBuilderEditor.OnInspectorGUI () (at Assets/ProductsDetails.cs:31)
UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/Inspector/InspectorWindow.cs:1231)
UnityEditor.DockArea:OnGUI()
and then get the IO error.. how ever, i see that the Serialized.txt is been created but the file is empty..
Anyway Thank you so much.. you're great :D Its working perfectly fine in Unity 2017 but didn't work in Unity 5.3.4.
I get this when i click on save JSON
am i missing something?
IOException: Sharing violation on path D:\UrbanLadderProjects\AR\UL-LivingSpacesWithGit\Assets\Serialized.txt
System.IO.FileStream..ctor (System.String path, File$$anonymous$$ode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320)
System.IO.FileStream..ctor (System.String path, File$$anonymous$$ode mode, FileAccess access, FileShare share)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.File$$anonymous$$ode,System.IO.FileAccess,System.IO.FileShare)
System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamWriter.cs:124)
System.IO.StreamWriter..ctor (System.String path)
(wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string)
ObjectBuilderEditor.OnInspectorGUI () (at Assets/ProductsDetails.cs:30)
UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/Inspector/InspectorWindow.cs:1231)
UnityEditor.DockArea:OnGUI()
Am i missing something?