当前位置: 首页 > 工具软件 > Urho3D > 使用案例 >

关于urho3d中加载资源用法在Lua中一些注意点 android

公孙和怡
2023-12-01

起因:

  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);

 类似资料: