Files
XCEngine/docs/api/resources/resource-file-system/find-resource.md

61 lines
1.4 KiB
Markdown
Raw Normal View History

2026-03-20 02:35:35 +08:00
# ResourceFileSystem::FindResource
## 方法签名
```cpp
bool FindResource(const Containers::String& relativePath, Containers::String& outAbsolutePath) const;
```
## 所属类
`XCEngine::Resources::ResourceFileSystem`
## 描述
查找指定相对路径的资源,并返回其绝对路径。搜索顺序为:目录(根路径 + 已注册目录)→ 档案。
此方法线程安全。
## 参数
| 参数名 | 类型 | 描述 |
|--------|------|------|
| `relativePath` | `const Containers::String&` | 资源的相对路径 |
| `outAbsolutePath` | `Containers::String&` | 输出参数,接收资源的绝对路径 |
## 返回值
- `bool` - 找到资源返回 `true`,未找到返回 `false`
## 示例
```cpp
#include "Resources/ResourceFileSystem.h"
using namespace XCEngine::Resources;
ResourceFileSystem& fs = ResourceFileSystem::Get();
fs.Initialize("C:/Game/Resources");
fs.AddDirectory("C:/Game/Assets");
Containers::String absolutePath;
// 查找纹理资源
if (fs.FindResource("textures/player/diffuse.png", absolutePath)) {
printf("Found at: %s\n", absolutePath.CStr());
// 使用绝对路径加载纹理
} else {
printf("Resource not found\n");
}
// 查找着色器资源
if (fs.FindResource("shaders/forward.glsl", absolutePath)) {
printf("Shader at: %s\n", absolutePath.CStr());
}
```
## 注意事项
- 搜索路径格式应使用正斜杠 `/`
- 档案内资源返回的绝对路径格式为 `archive://relative/path`