BsPrefabUtility.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsGameObject.h"
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup Scene
  7. * @{
  8. */
  9. /** Performs various prefab specific operations. */
  10. class BS_CORE_EXPORT PrefabUtility
  11. {
  12. private:
  13. /** Contains saved Component instance data. */
  14. struct ComponentProxy
  15. {
  16. GameObjectInstanceDataPtr instanceData;
  17. UINT32 linkId;
  18. };
  19. /** Contains saved SceneObject instance data, as well as saved instance data for all its children and components. */
  20. struct SceneObjectProxy
  21. {
  22. GameObjectInstanceDataPtr instanceData;
  23. UINT32 linkId;
  24. Vector<ComponentProxy> components;
  25. Vector<SceneObjectProxy> children;
  26. };
  27. public:
  28. /**
  29. * Remove any instance specific changes to the object or its hierarchy from the provided prefab instance and
  30. * restore it to the exact copy of the linked prefab.
  31. *
  32. * @param[in] so Object to revert.
  33. */
  34. static void revertToPrefab(const HSceneObject& so);
  35. /**
  36. * Updates all of the objects belonging to the same prefab instance as the provided object (if any). The update
  37. * will apply any changes from the linked prefab to the hierarchy (if any).
  38. *
  39. * @param[in] so Object to update.
  40. */
  41. static void updateFromPrefab(const HSceneObject& so);
  42. /**
  43. * Generates prefab "link" ID that can be used for tracking which game object in a prefab instance corresponds to
  44. * an object in the prefab.
  45. *
  46. * @note If any children of the provided object belong to another prefab they will not have IDs generated.
  47. */
  48. static UINT32 generatePrefabIds(const HSceneObject& sceneObject, UINT32 startingId);
  49. /**
  50. * Clears all prefab "link" IDs in the provided object and its children.
  51. *
  52. * @note If any of its children belong to another prefab they will not be cleared.
  53. */
  54. static void clearPrefabIds(const HSceneObject& sceneObject, bool recursive = true);
  55. /**
  56. * Updates the internal prefab diff data by recording the difference between the current values in the provided
  57. * prefab instance and its prefab.
  58. *
  59. * @note
  60. * If the provided object contains any child prefab instances, this will be done recursively for them as well.
  61. */
  62. static void recordPrefabDiff(const HSceneObject& sceneObject);
  63. private:
  64. /**
  65. * Traverses the object hierarchy, finds all child objects and components and records their instance data, as well
  66. * as their original place in the hierarchy. Instance data essentially holds the object's "identity" and by
  67. * restoring it we ensure any handles pointing to the object earlier will still point to the new version.
  68. *
  69. * @param[in] so Object to traverse and record.
  70. * @param[out] output Contains the output hierarchy of instance data.
  71. * @param[out] linkedInstanceData A map of link IDs to instance data. Objects without link IDs will not be
  72. * included here.
  73. *
  74. * @note Does not recurse into child prefab instances.
  75. */
  76. static void recordInstanceData(const HSceneObject& so, SceneObjectProxy& output,
  77. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData);
  78. /**
  79. * Restores instance data in the provided hierarchy, using link ids to determine what data maps to which objects.
  80. *
  81. * @param[in] so Object to traverse and restore the instance data.
  82. * @param[in] proxy Hierarchy containing instance data for all objects and components, returned by
  83. * recordInstanceData() method.
  84. * @param[in] linkedInstanceData A map of link IDs to instance data, returned by recordInstanceData() method.
  85. *
  86. * @note Does not recurse into child prefab instances.
  87. */
  88. static void restoreLinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy,
  89. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData);
  90. /**
  91. * Restores instance data in the provided hierarchy, but only for objects without a link id. Since the objects do
  92. * not have a link ID we rely on their sequential order to find out which instance data belongs to which object.
  93. *
  94. * @param[in] so Object to traverse and restore the instance data.
  95. * @param[in] proxy Hierarchy containing instance data for all objects and components, returned by
  96. * recordInstanceData() method.
  97. *
  98. * @note Does not recurse into child prefab instances.
  99. */
  100. static void restoreUnlinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy);
  101. };
  102. /** @} */
  103. }