PrefabUtility.cs 3.9 KB

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