SerrializableObject.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using UnityEngine;
  3. using System.Globalization;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. namespace GraphProcessor
  8. {
  9. // Warning: this class only support the serialization of UnityObject and primitive
  10. [System.Serializable]
  11. public class SerializableObject
  12. {
  13. [System.Serializable]
  14. class ObjectWrapper
  15. {
  16. public UnityEngine.Object value;
  17. }
  18. public string serializedType;
  19. public string serializedName;
  20. public string serializedValue;
  21. public object value;
  22. public SerializableObject(object value, Type type, string name = null)
  23. {
  24. this.value = value;
  25. this.serializedName = name;
  26. this.serializedType = type.AssemblyQualifiedName;
  27. }
  28. public void Deserialize()
  29. {
  30. if (String.IsNullOrEmpty(serializedType))
  31. {
  32. Debug.LogError("Can't deserialize the object from null type");
  33. return;
  34. }
  35. Type type = Type.GetType(serializedType);
  36. if (type.IsPrimitive)
  37. {
  38. if (string.IsNullOrEmpty(serializedValue))
  39. value = Activator.CreateInstance(type);
  40. else
  41. value = Convert.ChangeType(serializedValue, type, CultureInfo.InvariantCulture);
  42. }
  43. else if (typeof(UnityEngine.Object).IsAssignableFrom(type))
  44. {
  45. ObjectWrapper obj = new ObjectWrapper();
  46. JsonUtility.FromJsonOverwrite(serializedValue, obj);
  47. value = obj.value;
  48. }
  49. else if (type == typeof(string))
  50. value = serializedValue.Length > 1 ? serializedValue.Substring(1, serializedValue.Length - 2).Replace("\\\"", "\"") : "";
  51. else
  52. {
  53. try {
  54. value = Activator.CreateInstance(type);
  55. JsonUtility.FromJsonOverwrite(serializedValue, value);
  56. } catch (Exception e){
  57. Debug.LogError(e);
  58. Debug.LogError("Can't serialize type " + serializedType);
  59. }
  60. }
  61. }
  62. public void Serialize()
  63. {
  64. if (value == null)
  65. return ;
  66. serializedType = value.GetType().AssemblyQualifiedName;
  67. if (value.GetType().IsPrimitive)
  68. serializedValue = Convert.ToString(value, CultureInfo.InvariantCulture);
  69. else if (value is UnityEngine.Object) //type is a unity object
  70. {
  71. if ((value as UnityEngine.Object) == null)
  72. return ;
  73. ObjectWrapper wrapper = new ObjectWrapper { value = value as UnityEngine.Object };
  74. serializedValue = JsonUtility.ToJson(wrapper);
  75. }
  76. else if (value is string)
  77. serializedValue = "\"" + ((string)value).Replace("\"", "\\\"") + "\"";
  78. else
  79. {
  80. try {
  81. serializedValue = JsonUtility.ToJson(value);
  82. if (String.IsNullOrEmpty(serializedValue))
  83. throw new Exception();
  84. } catch {
  85. Debug.LogError("Can't serialize type " + serializedType);
  86. }
  87. }
  88. }
  89. }
  90. }