- Home /
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.
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.
I've edited my answer and added an alternative if you're interested.
Your answer

Follow this Question
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