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