Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SunilUL · Nov 28, 2017 at 12:05 PM · jsonsaveloadcustomeditorsave-to-file

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:

alt text


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"
           }
         ]
       }
     ]
   }
 ]




productdetails.png (54.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
         }
     }



Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SunilUL · Nov 29, 2017 at 10:51 AM 0
Share

alt text

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?

avatar image Multirezonator SunilUL · Nov 29, 2017 at 11:33 AM 0
Share

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.

avatar image SunilUL Multirezonator · Nov 29, 2017 at 11:48 AM 0
Share

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.

Show more comments
avatar image SunilUL · Nov 29, 2017 at 04:17 PM 0
Share

I get this when i click on save JSON

alt text

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?

error.png (196.1 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

73 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JsonUtility : When I'm saving/loading to/from .Json, do I need a C# object for every child object of my .json file? 1 Answer

saving array to bin file 0 Answers

JSON save/load system 2 Answers

Saving JSON didnt work 1 Answer

exporting a 3d object i drew though the iOS platform 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges