Advertisement
Guest User

SerializedBigDataScriptableObject with runtime

a guest
Sep 24th, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. using UnityEngine;
  2. using Sirenix.OdinInspector;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System;
  6. #if UNITY_EDITOR
  7. using Sirenix.Utilities.Editor;
  8. using UnityEditor;
  9. #endif
  10. using Sirenix.Utilities;
  11. using Sirenix.Serialization;
  12. using Object = UnityEngine.Object;
  13.  
  14. public class SerializedBigDataScriptableObject<T> : ScriptableObject
  15.     where T : class, new()
  16. {
  17.     [ShowInInspector]
  18.     public static string binariesPathInResourcesFolder = "SerializedBigData";
  19.  
  20.     public string cachedFileName;
  21.  
  22.     static SerializedBigDataScriptableObject()
  23.     {
  24.         if (typeof(T).InheritsFrom<UnityEngine.Object>())
  25.         {
  26.             throw new Exception(typeof(T).GetNiceName());
  27.         }
  28.     }
  29.  
  30.     [NonSerialized]
  31.     private T data = new T();
  32.  
  33.     [NonSerialized]
  34.     private bool isLoadAttepted;
  35.  
  36.     [NonSerialized]
  37.     private bool isSuccesfullyLoaded;
  38.  
  39.     [SerializeField, HideInInspector]
  40.     private List<UnityEngine.Object> unityObjectReferences;
  41.  
  42.     [HideLabel]
  43.     [ShowInInspector, HideReferenceObjectPicker, OnInspectorGUI(PrependMethodName = "DrawBox")]
  44.     private T Data
  45.     {
  46.         get
  47.         {
  48.             #if UNITY_EDITOR
  49.             // Lets wait until trying to do anything until the object is created.
  50.             if (!AssetDatabase.Contains(this))
  51.             {
  52.                 return this.data;
  53.             }
  54.             #endif
  55.  
  56.             if (this.isLoadAttepted)
  57.             {
  58.                 if (this.data == null) this.data = new T();
  59.                 return this.data;
  60.             }
  61.  
  62.             this.data = this.LoadData();
  63.             this.isLoadAttepted = true;
  64.             return this.data;
  65.         }
  66.         set { }
  67.     }
  68.  
  69.     public T LazyLoadedData { get { return this.Data; } }
  70.  
  71. #if UNITY_EDITOR
  72.     private string GetBinaryFileDataPath()
  73.     {
  74.         // Lets wait until trying to do anything until the object is created.
  75.  
  76.         if (!AssetDatabase.Contains(this))
  77.         {
  78.             return null;
  79.         }
  80.  
  81.         var assetPath = AssetDatabase.GetAssetPath(this);
  82.         var folder = System.IO.Path.GetDirectoryName(assetPath);
  83.         folder = Path.Combine(folder, "Resources", binariesPathInResourcesFolder);
  84.         cachedFileName = System.IO.Path.GetFileNameWithoutExtension(assetPath);
  85.         Directory.CreateDirectory(folder);
  86.         var dataPath = folder.Replace('\\', '/').TrimEnd('/') + "/" + cachedFileName + ".bytes";
  87.  
  88.         return dataPath;
  89.     }
  90.  
  91.     public virtual void Save(T obj)
  92.     {
  93.         var filePath = this.GetBinaryFileDataPath();
  94.         byte[] bytes = SerializationUtility.SerializeValue(data, DataFormat.Binary);
  95.         using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write))
  96.         using (var writer = new BinaryWriter(fs))
  97.         {
  98.             writer.Write(bytes);
  99.         }
  100.         AssetDatabase.Refresh();
  101.     }
  102.  
  103.     private void DrawBox()
  104.     {
  105.         GUILayout.Space(10);
  106.         SirenixEditorGUI.Title(typeof(T).GetNiceName().SplitPascalCase(), null, TextAlignment.Left, true);
  107.         var rect = GUILayoutUtility.GetRect(0, 1);
  108.         rect.y -= 12;
  109.         rect.yMin -= 20;
  110.         rect.xMin = rect.xMax - 120;
  111.         rect.height += 4;
  112.  
  113.         if (GUI.Button(rect.Split(0, 2).Padding(0, 2), "Load"))
  114.         {
  115.             this.data = this.LoadData();
  116.         }
  117.  
  118.         if (GUI.Button(rect.Split(1, 2).Padding(0, 2), "Save"))
  119.         {
  120.             this.Save(this.data);
  121.         }
  122.  
  123.         GUILayout.Space(-5);
  124.     }
  125.  
  126. #endif
  127.  
  128.     public virtual T LoadData()
  129.     {
  130.         this.isLoadAttepted = false;
  131.         if (string.IsNullOrEmpty(cachedFileName))
  132.         {
  133.             return null;
  134.         }
  135.  
  136.         var dataPath = binariesPathInResourcesFolder + "/" + cachedFileName;
  137.  
  138.         var binary = Resources.Load(dataPath)  as TextAsset;
  139.         if (binary == null)
  140.         {
  141.             Debug.LogWarning($"{dataPath} does not exist");
  142.             return null;
  143.         }
  144.        
  145.         var s = new MemoryStream(binary.bytes);
  146.         using (var reader = new BinaryReader(s))
  147.         {
  148.             var obj = SerializationUtility.DeserializeValue<T>(reader.BaseStream, DataFormat.Binary, this.unityObjectReferences);
  149.             return obj;
  150.         }
  151.     }
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement