1. 程式人生 > >關於urho3d中載入資源用法在Lua中一些注意點 android

關於urho3d中載入資源用法在Lua中一些注意點 android

起因:

  1. 載入場景的做法是Lua函式匯出中使用的是File的形式,在android上無法取到正確的位置,但是cache:GetResource可以獲取到XMLFile所以需要進行修改

這裡修改的檔案

  1. Node.pkg, Scene.pkg
  2. Node.h Node.cpp Scene.h Scene.cpp

場景通過XML進行載入場景的修改

  1. 針對同步載入修改LoadByXMLFile
  2. 針對非同步載入修改LoadAsyncByXMLFile
  3. 針對場景直接例項化InstantiateByXML

// Scene.pkg
tolua_outside bool SceneLoadByXMLFile @ LoadByXMLFile(XMLFile *file);
tolua_outside Node* SceneInstantiateByXML @ InstantiateByXML(XMLFile *file, const Vector3& position, const Quaternion& rotation, CreateMode mode = REPLICATED);
tolua_outside bool SceneLoadAsyncByXMLFile @ LoadAsyncByXMLFile(XMLFile *file, LoadMode mode = LOAD_SCENE_AND_RESOURCES);

static bool SceneLoadByXMLFile(Scene* scene, XMLFile *file)
{
    return scene->LoadByXMLFile(file);
}

static Node* SceneInstantiateByXML(Scene* scene, XMLFile *file, const Vector3& position, const Quaternion& rotation, CreateMode mode)
{
    return file.IsOpen() ? scene->InstantiateByXML(file, position, rotation, mode) : 0;
}

static bool SceneLoadAsyncByXMLFile(Scene* scene, XMLFile *file, LoadMode mode)
{
    return scene->LoadAsyncByXMLFile(file, mode);
}


// Scene.h Scene.cpp
bool LoadByXMLFile(XMLFile *file);
bool LoadAsyncByXMLFile(XMLFile* file, LoadMode mode = LOAD_SCENE_AND_RESOURCES);
/// Instantiate scene content from XML data. Return root node if successful.
Node* InstantiateByXML(XMLFile *file, const Vector3& position, const Quaternion& rotation, CreateMode mode = REPLICATED);
    
    bool Scene::LoadByXMLFile(XMLFile *file)
{
	URHO3D_PROFILE(LoadSceneXMLFile);

	StopAsyncLoading();

	// Load the whole scene, then perform post-load if successfully loaded
	// Note: the scene filename and checksum can not be set, as we only used an XML element
	if (Node::LoadXMLFile(file))
	{
		FinishLoading(0);
		return true;
	}
	else
		return false;
}

bool Scene::Loa
dAsyncByXMLFile(XMLFile* file, LoadMode mode)
{
	if (!file)
	{
		URHO3D_LOGERROR("Null file for async loading");
		return false;
	}

	StopAsyncLoading();

	if (mode > LOAD_RESOURCES_ONLY)
	{
		URHO3D_LOGINFO("Loading scene from " + file->GetName());
		Clear();
	}

	asyncLoading_ = true;
	asyncProgress_.xmlFile_ = file;
	asyncProgress_.file_ = NULL;
	asyncProgress_.mode_ = mode;
	asyncProgress_.loadedNodes_ = asyncProgress_.totalNodes_ = asyncProgress_.loadedResources_ = asyncProgress_.totalResources_ = 0;
	asyncProgress_.resources_.Clear();

	if (mode > LOAD_RESOURCES_ONLY)
	{
		XMLElement rootElement = file->GetRoot();

		// Preload resources if appropriate
		if (mode != LOAD_SCENE)
		{
			URHO3D_PROFILE(FindResourcesToPreload);

			PreloadResourcesXML(rootElement);
		}

		// Store own old ID for resolving possible root node references
		unsigned nodeID = rootElement.GetUInt("id");
		resolver_.AddNode(nodeID, this);

		// Load the root level components first
		if (!Node::LoadXML(rootElement, resolver_, false))
			return false;

		// Then prepare for loading all root level child nodes in the async update
		XMLElement childNodeElement = rootElement.GetChild("node");
		asyncProgress_.xmlElement_ = childNodeElement;

		// Count the amount of child nodes
		while (childNodeElement)
		{
			++asyncProgress_.totalNodes_;
			childNodeElement = childNodeElement.GetNext("node");
		}
	}
	else
	{
		URHO3D_PROFILE(FindResourcesToPreload);

		URHO3D_LOGINFO("Preloading resources from " + file->GetName());
		PreloadResourcesXML(file->GetRoot());
	}

	return true;
}

Node* Scene::InstantiateByXML(XMLFile *file, const Vector3& position, const Quaternion& rotation, CreateMode mode)
{
	return InstantiateXML(file->GetRoot(), position, rotation, mode);
}


// Node.h Node.cpp
bool LoadXMLFile(XMLFile * file);
bool Node::LoadXMLFile(XMLFile * file)
{
	if (LoadXML(file->GetRoot()))
	{
		return true;
	}
	else
		return false;
}

大體的修改如上
基本上Lua中就實現的載入

// 載入方式
local file = cache:GetResource("XMLFile", scenefilename);
    
SubscribeToEvent(scene_, "AsyncLoadProgress", function(eventType, eventData) 
	local progress = eventData["Progress"]:GetFloat();
	print(progress);
end)

SubscribeToEvent(scene_, "AsyncLoadFinished", function(eventType, eventData) 
	print(" finish Load !!!!")
end)
scene_:LoadAsyncByXMLFile(file);