PrefabUtility.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /** @addtogroup Utility-Editor
  9. * @{
  10. */
  11. /// <summary>
  12. /// Performs various prefab specific operations.
  13. /// </summary>
  14. public static class PrefabUtility
  15. {
  16. /// <summary>
  17. /// Breaks the link between a prefab instance and its prefab. Object will retain all current values but will
  18. /// no longer be influenced by modifications to its parent prefab.
  19. /// </summary>
  20. /// <param name="obj">Prefab instance whose link to break.</param>
  21. public static void BreakPrefab(SceneObject obj)
  22. {
  23. if (obj == null)
  24. return;
  25. IntPtr objPtr = obj.GetCachedPtr();
  26. Internal_BreakPrefab(objPtr);
  27. }
  28. /// <summary>
  29. /// Updates the contents of the prefab with the contents of the provided prefab instance. If the provided object
  30. /// is not a prefab instance nothing happens.
  31. /// </summary>
  32. /// <param name="obj">Prefab instance whose prefab to update.</param>
  33. /// <param name="refreshScene">If true, all prefab instances in the current scene will be updated so they consistent
  34. /// with the newly saved data.</param>
  35. public static void ApplyPrefab(SceneObject obj, bool refreshScene = true)
  36. {
  37. if (obj == null)
  38. return;
  39. if (refreshScene)
  40. {
  41. SceneObject root = Scene.Root;
  42. if (root != null)
  43. Internal_RecordPrefabDiff(root.GetCachedPtr());
  44. }
  45. IntPtr objPtr = obj.GetCachedPtr();
  46. Internal_ApplyPrefab(objPtr);
  47. if (refreshScene)
  48. {
  49. SceneObject root = Scene.Root;
  50. if (root != null)
  51. Internal_UpdateFromPrefab(root.GetCachedPtr());
  52. }
  53. }
  54. /// <summary>
  55. /// Remove any instance specific changes to the object or its hierarchy from the provided prefab instance and
  56. /// restore it to the exact copy of the linked prefab.
  57. /// </summary>
  58. /// <param name="obj">Prefab instance to revert to original state.</param>
  59. public static void RevertPrefab(SceneObject obj)
  60. {
  61. if (obj == null)
  62. return;
  63. IntPtr objPtr = obj.GetCachedPtr();
  64. Internal_RevertPrefab(objPtr);
  65. }
  66. /// <summary>
  67. /// Updates all of the objects belonging to the same prefab instance as the provided object (if any). The update
  68. /// will apply any changes from the linked prefab to the hierarchy(if any).
  69. /// </summary>
  70. /// <param name="obj"></param>
  71. public static void UpdateFromPrefab(SceneObject obj)
  72. {
  73. if (obj == null)
  74. return;
  75. IntPtr objPtr = obj.GetCachedPtr();
  76. Internal_UpdateFromPrefab(objPtr);
  77. }
  78. /// <summary>
  79. /// Checks if a scene object has a prefab link. Scene objects with a prefab link will be automatically updated
  80. /// when their prefab changes in order to reflect its changes.
  81. /// </summary>
  82. /// <param name="obj">Scene object to check if it has a prefab link.</param>
  83. /// <returns>True if the object is a prefab instance (has a prefab link), false otherwise.</returns>
  84. public static bool IsPrefabInstance(SceneObject obj)
  85. {
  86. if (obj == null)
  87. return false;
  88. IntPtr objPtr = obj.GetCachedPtr();
  89. return Internal_HasPrefabLink(objPtr);
  90. }
  91. /// <summary>
  92. /// Returns the root object of the prefab instance that this object belongs to, if any.
  93. /// </summary>
  94. /// <param name="obj">Scene object to retrieve the prefab parent for.</param>
  95. /// <returns>Prefab parent of the provided object, or null if the object is not part of a prefab instance.</returns>
  96. public static SceneObject GetPrefabParent(SceneObject obj)
  97. {
  98. if (obj == null)
  99. return null;
  100. IntPtr objPtr = obj.GetCachedPtr();
  101. return Internal_GetPrefabParent(objPtr);
  102. }
  103. /// <summary>
  104. /// Returns the UUID of the prefab attached to the provided scene object. Only works on root prefab objects.
  105. /// </summary>
  106. /// <param name="obj">Scene object to retrieve the prefab UUID for.</param>
  107. /// <returns>Prefab UUID if the object is part of a prefab, null otherwise. </returns>
  108. public static string GetPrefabUUID(SceneObject obj)
  109. {
  110. if (obj == null)
  111. return null;
  112. IntPtr objPtr = obj.GetCachedPtr();
  113. return Internal_GetPrefabUUID(objPtr);
  114. }
  115. [MethodImpl(MethodImplOptions.InternalCall)]
  116. private static extern void Internal_BreakPrefab(IntPtr nativeInstance);
  117. [MethodImpl(MethodImplOptions.InternalCall)]
  118. private static extern void Internal_ApplyPrefab(IntPtr nativeInstance);
  119. [MethodImpl(MethodImplOptions.InternalCall)]
  120. private static extern void Internal_RecordPrefabDiff(IntPtr nativeInstance);
  121. [MethodImpl(MethodImplOptions.InternalCall)]
  122. private static extern void Internal_RevertPrefab(IntPtr nativeInstance);
  123. [MethodImpl(MethodImplOptions.InternalCall)]
  124. private static extern bool Internal_HasPrefabLink(IntPtr nativeInstance);
  125. [MethodImpl(MethodImplOptions.InternalCall)]
  126. private static extern void Internal_UpdateFromPrefab(IntPtr nativeInstance);
  127. [MethodImpl(MethodImplOptions.InternalCall)]
  128. private static extern SceneObject Internal_GetPrefabParent(IntPtr nativeInstance);
  129. [MethodImpl(MethodImplOptions.InternalCall)]
  130. private static extern string Internal_GetPrefabUUID(IntPtr nativeInstance);
  131. }
  132. /** @} */
  133. }