Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Wulfara · Jul 29, 2020 at 09:27 AM · editorinspectorjson

Is there any way to hide properties to JsonUtility.ToJson but show them in inspector?

I'm trying to store in Json strings only the properties needed to save and restore some objects status, but JsonUtility.ToJson(this) captures all the properties that are public or marked as [SerializeField]. This way I cannot hide a property from Json but show it in the Unity Inspector.

I could store the save data into another object and parse that one to Json but I was wondering if the former way is doable.

Thank you in advance.

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
2
Best Answer

Answer by Bunny83 · Jul 29, 2020 at 09:52 AM

No, that's not possible at the moment. If something should be visible in the inspector, it has to be serializable. If it's serializable it will be serialized by the JsonUtility. What you can do is simply seperate all the things you want to save as json into a simple serializable data class and just use that in your original script. Imagine this

 public class SomeScript : MonoBehaviour
 {
     public float maxHealth;
     public float walkSpeed;
     public float currentHealth;
     public string playerName;
     // [ ... ]
 }

Now assuming we only want currentHealth and playerName in our json but not maxHealth or walkSpeed, we can do this:

 [System.Serializable]
 public class RuntimeData
 {
     public float currentHealth;
     public string playerName;
 }
 
 public class SomeScript : MonoBehaviour
 {
     public float maxHealth;
     public float walkSpeed;
     public RuntimeData runtimeData;
     // [ ... ]
 }

Now we still have all the information in the inspector, but we can simply serialize / deserialize the "runtimeData" instance. Of course when you want to access the playerName or the currentHealth in your code, you have to use runtimeData.currentHealth instead of just currentHealth. That way you can seperate variables into those which change during runtime / need to be serialized and those which are just "settings".


Of course you might be more creative with the choosen class name depending on the information you want to store. Keep in mind that this way you can simply composite a larger class that gathers those "runtime sets" from several classes into one save class and serialize only that one.


edit

An alternative might be to not use the JsonUtilty but use my SimpleJSON framework and provide your own "Serialize" and "Deserialize" methods that gathers the information you want to save / restore

 public class SomeScript : MonoBehaviour
 {
     public float maxHealth;
     public float walkSpeed;
     public float currentHealth;
     public string playerName;
     public JSONNode Seriaiize()
     {
         var root = new JSONObject();
         root["currentHealth"].AsFloat = currentHealth;
         root["playerName"].Value = playerName;
     }
     public void Deseriaiize(JSONNode aNode)
     {
         if (aNode.IsObject)
         {
             currentHealth = root["currentHealth"].AsFloat;
             playerName = root["playerName"].Value;
         }
     }

}

Now when you call Serialize you get back a JSONNode which you can either directly convert to json by using ToString or embed it in some larger json structure. This way you have full control over what is serialized and deserialized. You can also directly apply some sanity checks (like the currentHealth probably shouldn't be larger than maxHealth, etc.)


Note you only need the SimpleJSON.cs file. However having the SimpleJSONUnity.cs file as well adds some useful simplifications for some Unity types like Vector2/3/4 and Quaternion.

Comment
Add comment · Show 1 · 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 Bunny83 · Jul 29, 2020 at 10:05 AM 0
Share

I've edited my answer and added an alternative if you're interested.

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

181 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 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 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

Inspector support for .json TextAsset 0 Answers

Is there an event being fired off when the Inspector is being resized? 1 Answer

How to get notification of moving a GameObject in the hierachy when editing the scene? 1 Answer

How to display a message while editor is blocked? 1 Answer

Hierarchy not showing anything, getting KeyNotFoundException 1 Answer


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