Files
XCEngine/docs/api/resources/resources-iresource.md

80 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# IResource
**命名空间**: `XCEngine::Resources`
**类型**: `class` (abstract)
**描述**: 资源基类接口所有具体资源类型Texture、Mesh、Material 等)都必须继承自此类。
## 概述
`IResource` 是 XCEngine 资源管理系统的核心抽象基类。它定义了所有资源共有的基本属性和行为包括资源类型、名称、路径、GUID、内存大小和有效性状态。
## 公共方法
### 基础属性
| 方法 | 描述 |
|------|------|
| `ResourceType GetType() const` | 获取资源类型 |
| `const Containers::String& GetName() const` | 获取资源名称 |
| `const Containers::String& GetPath() const` | 获取资源路径 |
| `ResourceGUID GetGUID() const` | 获取全局唯一标识符 |
| `bool IsValid() const` | 检查资源是否有效 |
| `size_t GetMemorySize() const` | 获取资源占用的内存大小(字节) |
### 生命周期
| 方法 | 描述 |
|------|------|
| `void Release()` | 释放资源引用 |
### 构造参数
```cpp
struct ConstructParams {
Containers::String name; // 资源名称
Containers::String path; // 资源路径
ResourceGUID guid; // 全局唯一标识符
size_t memorySize = 0; // 内存大小
};
```
### 初始化方法
| 方法 | 描述 |
|------|------|
| `void Initialize(const ConstructParams& params)` | 使用构造参数初始化资源 |
| `void SetInvalid()` | 将资源标记为无效 |
## 公共成员
| 成员 | 类型 | 描述 |
|------|------|------|
| `m_name` | `Containers::String` | 资源名称 |
| `m_path` | `Containers::String` | 资源路径 |
| `m_guid` | `ResourceGUID` | 全局唯一标识符 |
| `m_isValid` | `bool` | 资源是否有效 |
| `m_memorySize` | `size_t` | 内存占用大小 |
## 使用示例
```cpp
class MyResource : public IResource {
public:
ResourceType GetType() const override { return ResourceType::Custom; }
const Containers::String& GetName() const override { return m_name; }
const Containers::String& GetPath() const override { return m_path; }
ResourceGUID GetGUID() const override { return m_guid; }
bool IsValid() const override { return m_isValid; }
size_t GetMemorySize() const override { return m_memorySize; }
void Release() override { /* 释放逻辑 */ }
};
```
## 相关文档
- [ResourceHandle](./resources-resourcehandle.md) - 资源句柄
- [ResourceManager](./resources-resourcemanager.md) - 资源管理器
- [ResourceTypes](./resources-resourcetypes.md) - 资源类型定义