PrefabUtility.cs 4.0 KB

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