PrefabUtility.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /// <summary>
  9. /// Performs various prefab specific operations.
  10. /// </summary>
  11. public static class PrefabUtility
  12. {
  13. /// <summary>
  14. /// Breaks the link between a prefab instance and its prefab. Object will retain all current values but will
  15. /// no longer be influenced by modifications to its parent prefab.
  16. /// </summary>
  17. /// <param name="obj">Prefab instance whose link to break.</param>
  18. public static void BreakPrefab(SceneObject obj)
  19. {
  20. if (obj == null)
  21. return;
  22. IntPtr objPtr = obj.GetCachedPtr();
  23. Internal_BreakPrefab(objPtr);
  24. }
  25. /// <summary>
  26. /// Updates the contents of the prefab with the contents of the provided prefab instance. If the provided object
  27. /// is not a prefab instance nothing happens.
  28. /// </summary>
  29. /// <param name="obj">Prefab instance whose prefab to update.</param>
  30. public static void ApplyPrefab(SceneObject obj)
  31. {
  32. if (obj == null)
  33. return;
  34. IntPtr objPtr = obj.GetCachedPtr();
  35. Internal_ApplyPrefab(objPtr);
  36. }
  37. /// <summary>
  38. /// Remove any instance specific changes to the object or its hierarchy from the provided prefab instance and
  39. /// restore it to the exact copy of the linked prefab.
  40. /// </summary>
  41. /// <param name="obj">Prefab instance to revert to original state.</param>
  42. public static void RevertPrefab(SceneObject obj)
  43. {
  44. if (obj == null)
  45. return;
  46. IntPtr objPtr = obj.GetCachedPtr();
  47. Internal_RevertPrefab(objPtr);
  48. }
  49. /// <summary>
  50. /// Checks if a scene object has a prefab link. Scene objects with a prefab link will be automatically updated
  51. /// when their prefab changes in order to reflect its changes.
  52. /// </summary>
  53. /// <param name="obj">Scene object to check if it has a prefab link.</param>
  54. /// <returns>True if the object is a prefab instance (has a prefab link), false otherwise.</returns>
  55. public static bool IsPrefabInstance(SceneObject obj)
  56. {
  57. if (obj == null)
  58. return false;
  59. IntPtr objPtr = obj.GetCachedPtr();
  60. return Internal_HasPrefabLink(objPtr);
  61. }
  62. /// <summary>
  63. /// Returns the root object of the prefab instance that this object belongs to, if any.
  64. /// </summary>
  65. /// <param name="obj">Scene object to retrieve the prefab parent for.</param>
  66. /// <returns>Prefab parent of the provided object, or null if the object is not part of a prefab instance.</returns>
  67. public static SceneObject GetPrefabParent(SceneObject obj)
  68. {
  69. if (obj == null)
  70. return null;
  71. IntPtr objPtr = obj.GetCachedPtr();
  72. return Internal_GetPrefabParent(objPtr);
  73. }
  74. [MethodImpl(MethodImplOptions.InternalCall)]
  75. private static extern void Internal_BreakPrefab(IntPtr nativeInstance);
  76. [MethodImpl(MethodImplOptions.InternalCall)]
  77. private static extern void Internal_ApplyPrefab(IntPtr nativeInstance);
  78. [MethodImpl(MethodImplOptions.InternalCall)]
  79. private static extern void Internal_RevertPrefab(IntPtr nativeInstance);
  80. [MethodImpl(MethodImplOptions.InternalCall)]
  81. private static extern bool Internal_HasPrefabLink(IntPtr nativeInstance);
  82. [MethodImpl(MethodImplOptions.InternalCall)]
  83. private static extern SceneObject Internal_GetPrefabParent(IntPtr nativeInstance);
  84. }
  85. }