Forked from sebtoun/JsonSerialisableScriptableObject.cs
Created
July 11, 2022 03:08
-
-
Save inkcomic/64fcdfe1404a52447f0e6f392ce0e4be to your computer and use it in GitHub Desktop.
Unity's ScriptableObjects that easily serialize to JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.IO; | |
| using System.Linq; | |
| using Newtonsoft.Json; | |
| using Newtonsoft.Json.Serialization; | |
| using UnityEngine; | |
| public class JsonSerialisableScriptableObject<T> : ScriptableObject where T : JsonSerialisableScriptableObject<T> | |
| { | |
| public void LoadJsonFromFile( string filename ) | |
| { | |
| Debug.Log( string.Format( "[{0}] Read file {1}", typeof( T ).Name, filename ) ); | |
| LoadJsonFromText( File.ReadAllText( filename ) ); | |
| } | |
| public void LoadJsonFromText( string json ) | |
| { | |
| var settings = new JsonSerializerSettings() | |
| { | |
| ObjectCreationHandling = ObjectCreationHandling.Replace | |
| }; | |
| JsonConvert.PopulateObject( json, this, settings ); | |
| OnPostJsonDeserialization(); | |
| } | |
| public string DumpJson() | |
| { | |
| var settings = new JsonSerializerSettings() | |
| { | |
| ContractResolver = new DerivedTypeOnlyContractResolver<T>(), | |
| Formatting = Formatting.Indented | |
| }; | |
| return JsonConvert.SerializeObject( this, settings ); | |
| } | |
| public void DumpJsonToFile( string filename ) | |
| { | |
| Debug.Log( string.Format( "[{0}] Write file {1}", typeof( T ).Name, filename ) ); | |
| File.WriteAllText( filename, DumpJson() ); | |
| } | |
| protected virtual void OnPostJsonDeserialization() | |
| { | |
| } | |
| private class DerivedTypeOnlyContractResolver<T> : DefaultContractResolver | |
| { | |
| protected override IList<JsonProperty> CreateProperties( Type type, MemberSerialization memberSerialization ) | |
| { | |
| var properties = base.CreateProperties( type, memberSerialization ); | |
| if ( !typeof( T ).IsAssignableFrom( type ) ) | |
| { | |
| return properties; | |
| } | |
| else | |
| { | |
| return properties.Where( property => property.DeclaringType == typeof( T ) ).ToList(); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.IO; | |
| using UnityEditor; | |
| using UnityEngine; | |
| public class JsonSerialisableScriptableObjectEditor<T> : Editor where T : JsonSerialisableScriptableObject<T> | |
| { | |
| public override void OnInspectorGUI() | |
| { | |
| var scriptableObject = (T) target; | |
| using ( new EditorGUILayout.HorizontalScope() ) | |
| { | |
| if ( GUILayout.Button( "Load File" ) ) | |
| { | |
| var filename = EditorUtility.OpenFilePanel( "Load Json File", Application.dataPath, "json" ); | |
| if ( !string.IsNullOrEmpty( filename ) && File.Exists( filename ) ) | |
| { | |
| scriptableObject.LoadJsonFromFile( filename ); | |
| } | |
| } | |
| if ( GUILayout.Button( "Save File" ) ) | |
| { | |
| var filename = EditorUtility.SaveFilePanel( "Save Json File", Application.dataPath, | |
| scriptableObject.name + ".json", "json" ); | |
| if ( !string.IsNullOrEmpty( filename ) ) | |
| { | |
| scriptableObject.DumpJsonToFile( filename ); | |
| } | |
| } | |
| } | |
| base.OnInspectorGUI(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment