Files
XCEngine/docs/api/XCEngine/Components/MeshRendererComponent/GetMaterial.md

49 lines
1.3 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.
# MeshRendererComponent::GetMaterial
获取指定槽位的材质指针。
```cpp
Resources::Material* GetMaterial(size_t index) const;
```
## 参数
- `index` - 材质槽索引。
## 返回值
- 若当前槽位已经有可用材质,返回对应 `Material*`
- 否则返回 `nullptr`
## 行为说明
按当前实现,这个访问器会先做两件事:
1. `EnsureDeferredAsyncMaterialLoadStarted(index)`
2. `ResolvePendingMaterials()`
因此它不是纯只读 getter而是一个“读访问 + 尝试推进材质兑现”的接口。
## `nullptr` 的含义
返回 `nullptr` 并不只代表“索引越界”,还可能表示:
- 槽位存在,但路径为空。
- 槽位存在,材质还没完成异步加载。
- 同步或异步加载失败。
- 槽位越界。
如果调用方需要区分这些情况,应结合 [GetMaterialPath](GetMaterialPath.md) 和 [GetMaterialAssetRefs](GetMaterialAssetRefs.md) 一起判断。
## 使用建议
- 渲染提取或真正需要访问 `Material` 对象时使用它。
- 如果只是想读取序列化主数据、调试路径或避免副作用,不要把它当作元数据 getter 使用。
## 相关文档
- [返回类型总览](MeshRendererComponent.md)
- [GetMaterialHandle](GetMaterialHandle.md)
- [GetMaterialPath](GetMaterialPath.md)
- [GetMaterialAssetRefs](GetMaterialAssetRefs.md)