|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Reflection; |
| 5 | +using System.Runtime.Serialization; |
| 6 | +using System.Runtime.Serialization.Formatters.Binary; |
| 7 | +using UnityEngine; |
| 8 | + |
| 9 | +public class BinarySerializableScriptableObject : ScriptableObject |
| 10 | +{ |
| 11 | +#if UNITY_EDITOR |
| 12 | + private static string _scriptableObjectsDataDirectory = "ScriptableObjects_Dev"; |
| 13 | +#else |
| 14 | + private static string _scriptableObjectsDataDirectory = "ScriptableObjects"; |
| 15 | +#endif |
| 16 | + |
| 17 | + public void SaveToFile() |
| 18 | + { |
| 19 | + string dirPath = Path.Combine( |
| 20 | + Application.persistentDataPath, |
| 21 | + _scriptableObjectsDataDirectory |
| 22 | + ); |
| 23 | + string filePath = Path.Combine(dirPath, $"{name}.data"); |
| 24 | + |
| 25 | + if (!Directory.Exists(dirPath)) |
| 26 | + Directory.CreateDirectory(dirPath); |
| 27 | + |
| 28 | + BinaryFormatter formatter = new BinaryFormatter(); |
| 29 | + FileStream stream = new FileStream(filePath, FileMode.Create); |
| 30 | + |
| 31 | + BinarySerializableData data = new BinarySerializableData(this); |
| 32 | + try |
| 33 | + { |
| 34 | + formatter.Serialize(stream, data.properties); |
| 35 | + } |
| 36 | + catch (SerializationException e) |
| 37 | + { |
| 38 | + Debug.LogError($"Failed to serialize '{name}'. Reason: " + e.Message); |
| 39 | + } |
| 40 | + finally |
| 41 | + { |
| 42 | + stream.Close(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public void LoadFromFile() |
| 47 | + { |
| 48 | + string filePath = Path.Combine( |
| 49 | + Application.persistentDataPath, |
| 50 | + _scriptableObjectsDataDirectory, |
| 51 | + $"{name}.data" |
| 52 | + ); |
| 53 | + |
| 54 | + if (!File.Exists(filePath)) |
| 55 | + { |
| 56 | + Debug.LogWarning($"File \"{filePath}\" not found! Getting default values.", this); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + BinaryFormatter formatter = new BinaryFormatter(); |
| 61 | + FileStream stream = new FileStream(filePath, FileMode.Open); |
| 62 | + |
| 63 | + Dictionary<string, object> properties = null; |
| 64 | + try |
| 65 | + { |
| 66 | + properties = formatter.Deserialize(stream) as Dictionary<string, object>; |
| 67 | + } |
| 68 | + catch (SerializationException e) |
| 69 | + { |
| 70 | + Debug.LogWarning($"Failed to deserialize '{name}' - getting default values. Reason: " + e.Message); |
| 71 | + } |
| 72 | + finally |
| 73 | + { |
| 74 | + stream.Close(); |
| 75 | + } |
| 76 | + |
| 77 | + if (properties == null) |
| 78 | + return; |
| 79 | + |
| 80 | + Type T = GetType(); |
| 81 | + FieldInfo field; |
| 82 | + foreach (KeyValuePair<string, object> pair in properties) |
| 83 | + { |
| 84 | + field = T.GetField(pair.Key); |
| 85 | + object deserializedValue = null; |
| 86 | + if (BinarySerializableData.Deserialize(field, pair.Value, out deserializedValue)) |
| 87 | + field.SetValue(this, deserializedValue); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments