SerializedObject.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup Utility-Editor
  9. * @{
  10. */
  11. /// <summary>
  12. /// Container and functionality to creating a serialized version of an object. The object must be of valid serializable
  13. /// type (<see cref="ManagedComponent"/>, <see cref="ManagedResource"/> or a class/struct marked with
  14. /// <see cref="SerializeObject"/> attribute).
  15. /// </summary>
  16. public class SerializedObject : ScriptObject
  17. {
  18. /// <summary>
  19. /// Constructs a new serialized object. Only for internal runtime use.
  20. /// </summary>
  21. private SerializedObject()
  22. { }
  23. /// <summary>
  24. /// Serializes all data within the provided component.
  25. /// </summary>
  26. /// <param name="obj">Component to serialize.</param>
  27. /// <returns>Object containing serialized data.</returns>
  28. public static SerializedObject Create(Component obj)
  29. {
  30. if (obj == null)
  31. return null;
  32. IntPtr componentPtr = obj.GetCachedPtr();
  33. return Internal_CreateComponent(componentPtr);
  34. }
  35. /// <summary>
  36. /// Serializes all data within the provided resources.
  37. /// </summary>
  38. /// <param name="obj">Resource to serialize.</param>
  39. /// <returns>Object containing serialized data.</returns>
  40. public static SerializedObject Create(ManagedResource obj)
  41. {
  42. if (obj == null)
  43. return null;
  44. IntPtr resourcePtr = obj.GetCachedPtr();
  45. return Internal_CreateResource(resourcePtr);
  46. }
  47. /// <summary>
  48. /// Serializes all data within the provided object.
  49. /// </summary>
  50. /// <param name="obj">Object to serialize.</param>
  51. /// <returns>Object containing serialized data.</returns>
  52. public static SerializedObject Create(object obj)
  53. {
  54. if (obj == null)
  55. return null;
  56. return Internal_CreateGeneric(obj);
  57. }
  58. /// <summary>
  59. /// Deserializes data stored in this object. Components and resources cannot be deserialized.
  60. /// </summary>
  61. /// <typeparam name="T">Type to cast the object to after deserialization.</typeparam>
  62. /// <returns>Deserialized object if successful, null otherwise.</returns>
  63. public T Get<T>()
  64. {
  65. return (T) Internal_Deserialize(mCachedPtr);
  66. }
  67. [MethodImpl(MethodImplOptions.InternalCall)]
  68. private static extern SerializedObject Internal_CreateComponent(IntPtr componentPtr);
  69. [MethodImpl(MethodImplOptions.InternalCall)]
  70. private static extern SerializedObject Internal_CreateResource(IntPtr resourcePtr);
  71. [MethodImpl(MethodImplOptions.InternalCall)]
  72. private static extern SerializedObject Internal_CreateGeneric(object obj);
  73. [MethodImpl(MethodImplOptions.InternalCall)]
  74. private static extern object Internal_Deserialize(IntPtr instance);
  75. }
  76. /** @} */
  77. }