BsPrefab.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "BsPrefab.h"
  2. #include "BsPrefabRTTI.h"
  3. #include "BsResources.h"
  4. #include "BsSceneObject.h"
  5. #include "BsPrefabUtility.h"
  6. namespace BansheeEngine
  7. {
  8. Prefab::Prefab()
  9. :Resource(false), mHash(0), mNextLinkId(0)
  10. {
  11. }
  12. Prefab::~Prefab()
  13. {
  14. if (mRoot != nullptr)
  15. mRoot->destroy(true);
  16. }
  17. HPrefab Prefab::create(const HSceneObject& sceneObject)
  18. {
  19. PrefabPtr newPrefab = createEmpty();
  20. newPrefab->initialize(sceneObject);
  21. HPrefab handle = static_resource_cast<Prefab>(gResources()._createResourceHandle(newPrefab));
  22. newPrefab->mUUID = handle.getUUID();
  23. sceneObject->mPrefabLinkUUID = newPrefab->mUUID;
  24. newPrefab->_getRoot()->mPrefabLinkUUID = newPrefab->mUUID;
  25. return handle;
  26. }
  27. PrefabPtr Prefab::createEmpty()
  28. {
  29. PrefabPtr newPrefab = bs_core_ptr<Prefab>(new (bs_alloc<Prefab>()) Prefab());
  30. newPrefab->_setThisPtr(newPrefab);
  31. return newPrefab;
  32. }
  33. void Prefab::initialize(const HSceneObject& sceneObject)
  34. {
  35. sceneObject->mPrefabDiff = nullptr;
  36. UINT32 newNextLinkId = PrefabUtility::generatePrefabIds(sceneObject, mNextLinkId);
  37. if (newNextLinkId < mNextLinkId)
  38. {
  39. BS_EXCEPT(InternalErrorException, "Prefab ran out of IDs to assign. " \
  40. "Consider increasing the size of the prefab ID data type.");
  41. }
  42. mNextLinkId = newNextLinkId;
  43. // If there are any child prefab instances, make sure to update their diffs so they are saved with this prefab
  44. Stack<HSceneObject> todo;
  45. todo.push(sceneObject);
  46. while (!todo.empty())
  47. {
  48. HSceneObject current = todo.top();
  49. todo.pop();
  50. UINT32 childCount = current->getNumChildren();
  51. for (UINT32 i = 0; i < childCount; i++)
  52. {
  53. HSceneObject child = current->getChild(i);
  54. if (!child->mPrefabLinkUUID.empty())
  55. PrefabUtility::recordPrefabDiff(child);
  56. else
  57. todo.push(child);
  58. }
  59. }
  60. // Clone the hierarchy for internal storage
  61. sceneObject->setFlags(SOF_DontInstantiate);
  62. mRoot = sceneObject->clone();
  63. sceneObject->unsetFlags(SOF_DontInstantiate);
  64. mRoot->mParent = nullptr;
  65. // Remove objects with "dont save" flag
  66. todo.push(mRoot);
  67. while (!todo.empty())
  68. {
  69. HSceneObject current = todo.top();
  70. todo.pop();
  71. if (current->hasFlag(SOF_DontSave))
  72. current->destroy();
  73. else
  74. {
  75. UINT32 numChildren = current->getNumChildren();
  76. for (UINT32 i = 0; i < numChildren; i++)
  77. todo.push(current->getChild(i));
  78. }
  79. }
  80. }
  81. void Prefab::update(const HSceneObject& sceneObject)
  82. {
  83. initialize(sceneObject);
  84. sceneObject->mPrefabLinkUUID = mUUID;
  85. mRoot->mPrefabLinkUUID = mUUID;
  86. mHash++;
  87. }
  88. HSceneObject Prefab::instantiate(bool onlyClone)
  89. {
  90. if (mRoot == nullptr)
  91. return HSceneObject();
  92. #if BS_EDITOR_BUILD
  93. // Update any child prefab instances in case their prefabs changed
  94. Stack<HSceneObject> todo;
  95. todo.push(mRoot);
  96. while (!todo.empty())
  97. {
  98. HSceneObject current = todo.top();
  99. todo.pop();
  100. UINT32 childCount = current->getNumChildren();
  101. for (UINT32 i = 0; i < childCount; i++)
  102. {
  103. HSceneObject child = current->getChild(i);
  104. if (!child->mPrefabLinkUUID.empty())
  105. PrefabUtility::updateFromPrefab(child);
  106. else
  107. todo.push(child);
  108. }
  109. }
  110. #endif
  111. mRoot->mPrefabHash = mHash;
  112. HSceneObject clone = mRoot->clone();
  113. if (!onlyClone)
  114. clone->instantiate();
  115. return clone;
  116. }
  117. RTTITypeBase* Prefab::getRTTIStatic()
  118. {
  119. return PrefabRTTI::instance();
  120. }
  121. RTTITypeBase* Prefab::getRTTI() const
  122. {
  123. return Prefab::getRTTIStatic();
  124. }
  125. }