BsPrefabUtility.h 3.7 KB

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