void ScriptSerializedSceneObject::internal_Restore(ScriptSerializedSceneObject* thisPtr)
	{
		HSceneObject sceneObj = thisPtr->mSO;

		if (sceneObj.isDestroyed())
			return;

		HSceneObject parent = sceneObj->getParent();

		UINT32 numChildren = sceneObj->getNumChildren();
		HSceneObject* children = nullptr;
		if (!thisPtr->mRecordHierarchy)
		{
			children = bs_stack_new<HSceneObject>(numChildren);
			for (UINT32 i = 0; i < numChildren; i++)
			{
				HSceneObject child = sceneObj->getChild(i);
				children[i] = child;

				child->setParent(HSceneObject());
			}
		}

		sceneObj->destroy(true);

		GameObjectManager::instance().setDeserializationMode(GODM_RestoreExternal | GODM_UseNewIds);

		MemorySerializer serializer;
		SPtr<SceneObject> restored = std::static_pointer_cast<SceneObject>(
			serializer.decode(thisPtr->mSerializedObject, thisPtr->mSerializedObjectSize));

		EditorUtility::restoreIds(restored->getHandle(), thisPtr->mSceneObjectProxy);
		restored->setParent(parent);

		if (!thisPtr->mRecordHierarchy)
		{
			for (UINT32 i = 0; i < numChildren; i++)
				children[i]->setParent(restored->getHandle());

			bs_stack_delete(children, numChildren);
		}

		restored->_instantiate();
	}
	ScriptSerializedSceneObject::ScriptSerializedSceneObject(MonoObject* instance, const HSceneObject& so, bool recordHierarchy)
		: ScriptObject(instance), mSO(so), mRecordHierarchy(recordHierarchy), mSerializedObject(nullptr), mSerializedObjectSize(0)
	{
		if (mSO.isDestroyed())
			return;

		UINT32 numChildren = mSO->getNumChildren();
		HSceneObject* children = nullptr;

		if (!mRecordHierarchy)
		{
			children = bs_stack_new<HSceneObject>(numChildren);
			for (UINT32 i = 0; i < numChildren; i++)
			{
				HSceneObject child = mSO->getChild(i);
				children[i] = child;

				child->setParent(HSceneObject());
			}
		}

		bool isInstantiated = !mSO->hasFlag(SOF_DontInstantiate);
		mSO->_setFlags(SOF_DontInstantiate);

		MemorySerializer serializer;
		mSerializedObject = serializer.encode(mSO.get(), mSerializedObjectSize);

		if (isInstantiated)
			mSO->_unsetFlags(SOF_DontInstantiate);

		mSceneObjectProxy = EditorUtility::createProxy(mSO);

		if (!mRecordHierarchy)
		{
			for (UINT32 i = 0; i < numChildren; i++)
				children[i]->setParent(mSO->getHandle());

			bs_stack_delete(children, numChildren);
		}
	}
Exemplo n.º 3
0
	void CmdRecordSO::recordSO(const HSceneObject& sceneObject)
	{
		UINT32 numChildren = mSceneObject->getNumChildren();
		HSceneObject* children = nullptr;

		if (!mRecordHierarchy)
		{
			children = bs_stack_new<HSceneObject>(numChildren);
			for (UINT32 i = 0; i < numChildren; i++)
			{
				HSceneObject child = mSceneObject->getChild(i);
				children[i] = child;

				child->setParent(HSceneObject());
			}
		}

		bool isInstantiated = !mSceneObject->hasFlag(SOF_DontInstantiate);
		mSceneObject->_setFlags(SOF_DontInstantiate);

		MemorySerializer serializer;
		mSerializedObject = serializer.encode(mSceneObject.get(), mSerializedObjectSize);

		if (isInstantiated)
			mSceneObject->_unsetFlags(SOF_DontInstantiate);

		mSceneObjectProxy = EditorUtility::createProxy(mSceneObject);

		if (!mRecordHierarchy)
		{
			for (UINT32 i = 0; i < numChildren; i++)
				children[i]->setParent(sceneObject->getHandle());

			bs_stack_delete(children, numChildren);
		}
	}