PrefabUtility.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. public static void ApplyPrefab(SceneObject obj)
  34. {
  35. if (obj == null)
  36. return;
  37. IntPtr objPtr = obj.GetCachedPtr();
  38. Internal_ApplyPrefab(objPtr);
  39. }
  40. /// <summary>
  41. /// Remove any instance specific changes to the object or its hierarchy from the provided prefab instance and
  42. /// restore it to the exact copy of the linked prefab.
  43. /// </summary>
  44. /// <param name="obj">Prefab instance to revert to original state.</param>
  45. public static void RevertPrefab(SceneObject obj)
  46. {
  47. if (obj == null)
  48. return;
  49. IntPtr objPtr = obj.GetCachedPtr();
  50. Internal_RevertPrefab(objPtr);
  51. }
  52. /// <summary>
  53. /// Updates all of the objects belonging to the same prefab instance as the provided object (if any). The update
  54. /// will apply any changes from the linked prefab to the hierarchy(if any).
  55. /// </summary>
  56. /// <param name="obj"></param>
  57. public static void UpdateFromPrefab(SceneObject obj)
  58. {
  59. if (obj == null)
  60. return;
  61. IntPtr objPtr = obj.GetCachedPtr();
  62. Internal_UpdateFromPrefab(objPtr);
  63. }
  64. /// <summary>
  65. /// Checks if a scene object has a prefab link. Scene objects with a prefab link will be automatically updated
  66. /// when their prefab changes in order to reflect its changes.
  67. /// </summary>
  68. /// <param name="obj">Scene object to check if it has a prefab link.</param>
  69. /// <returns>True if the object is a prefab instance (has a prefab link), false otherwise.</returns>
  70. public static bool IsPrefabInstance(SceneObject obj)
  71. {
  72. if (obj == null)
  73. return false;
  74. IntPtr objPtr = obj.GetCachedPtr();
  75. return Internal_HasPrefabLink(objPtr);
  76. }
  77. /// <summary>
  78. /// Returns the root object of the prefab instance that this object belongs to, if any.
  79. /// </summary>
  80. /// <param name="obj">Scene object to retrieve the prefab parent for.</param>
  81. /// <returns>Prefab parent of the provided object, or null if the object is not part of a prefab instance.</returns>
  82. public static SceneObject GetPrefabParent(SceneObject obj)
  83. {
  84. if (obj == null)
  85. return null;
  86. IntPtr objPtr = obj.GetCachedPtr();
  87. return Internal_GetPrefabParent(objPtr);
  88. }
  89. /// <summary>
  90. /// Returns the UUID of the prefab attached to the provided scene object. Only works on root prefab objects.
  91. /// </summary>
  92. /// <param name="obj">Scene object to retrieve the prefab UUID for.</param>
  93. /// <returns>Prefab UUID if the object is part of a prefab, null otherwise. </returns>
  94. public static string GetPrefabUUID(SceneObject obj)
  95. {
  96. if (obj == null)
  97. return null;
  98. IntPtr objPtr = obj.GetCachedPtr();
  99. return Internal_GetPrefabUUID(objPtr);
  100. }
  101. [MethodImpl(MethodImplOptions.InternalCall)]
  102. private static extern void Internal_BreakPrefab(IntPtr nativeInstance);
  103. [MethodImpl(MethodImplOptions.InternalCall)]
  104. private static extern void Internal_ApplyPrefab(IntPtr nativeInstance);
  105. [MethodImpl(MethodImplOptions.InternalCall)]
  106. private static extern void Internal_RevertPrefab(IntPtr nativeInstance);
  107. [MethodImpl(MethodImplOptions.InternalCall)]
  108. private static extern bool Internal_HasPrefabLink(IntPtr nativeInstance);
  109. [MethodImpl(MethodImplOptions.InternalCall)]
  110. private static extern void Internal_UpdateFromPrefab(IntPtr nativeInstance);
  111. [MethodImpl(MethodImplOptions.InternalCall)]
  112. private static extern SceneObject Internal_GetPrefabParent(IntPtr nativeInstance);
  113. [MethodImpl(MethodImplOptions.InternalCall)]
  114. private static extern string Internal_GetPrefabUUID(IntPtr nativeInstance);
  115. }
  116. /** @} */
  117. }