SerializedObject.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 object.
  25. /// </summary>
  26. /// <param name="obj">Object to serialize.</param>
  27. /// <returns>Object containing serialized data.</returns>
  28. public static SerializedObject Create(object obj)
  29. {
  30. if (obj == null)
  31. return null;
  32. return Internal_Create(obj);
  33. }
  34. /// <summary>
  35. /// Deserializes data stored in this object. Components and resources cannot be deserialized.
  36. /// </summary>
  37. /// <typeparam name="T">Type to cast the object to after deserialization.</typeparam>
  38. /// <returns>Deserialized object if successful, null otherwise.</returns>
  39. public T Get<T>()
  40. {
  41. return (T) Internal_Deserialize(mCachedPtr);
  42. }
  43. [MethodImpl(MethodImplOptions.InternalCall)]
  44. private static extern SerializedObject Internal_Create(object obj);
  45. [MethodImpl(MethodImplOptions.InternalCall)]
  46. private static extern object Internal_Deserialize(IntPtr instance);
  47. }
  48. /** @} */
  49. }