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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值