2026-03-26 21:32:43 +08:00
|
|
|
# MeshRendererComponent::GetMaterialHandle
|
|
|
|
|
|
|
|
|
|
获取指定槽位的材质句柄。
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
const Resources::ResourceHandle<Resources::Material>& GetMaterialHandle(size_t index) const;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 参数
|
|
|
|
|
|
|
|
|
|
- `index` - 材质槽索引。
|
|
|
|
|
|
|
|
|
|
## 返回值
|
|
|
|
|
|
|
|
|
|
- 若索引有效则返回对应句柄引用。
|
|
|
|
|
- 否则返回一个静态空句柄引用。
|
|
|
|
|
|
2026-04-03 11:50:13 +08:00
|
|
|
## 行为说明
|
|
|
|
|
|
|
|
|
|
和 [GetMaterial](GetMaterial.md) 一样,当前实现会在返回前先执行:
|
|
|
|
|
|
|
|
|
|
1. `EnsureDeferredAsyncMaterialLoadStarted(index)`
|
|
|
|
|
2. `ResolvePendingMaterials()`
|
|
|
|
|
|
|
|
|
|
因此它也可能在首次访问时启动一次异步加载,而不是单纯返回缓存引用。
|
|
|
|
|
|
2026-03-26 21:32:43 +08:00
|
|
|
## 注意事项
|
|
|
|
|
|
|
|
|
|
- 越界时返回的是共享的静态空句柄,而不是临时对象。
|
2026-04-03 11:50:13 +08:00
|
|
|
- 即使索引有效,返回的句柄也可能仍为空,因为异步结果尚未完成或加载失败。
|
|
|
|
|
- 如果调用方只想读取路径 / `AssetRef` 元数据,应优先使用 [GetMaterialPath](GetMaterialPath.md) 或 [GetMaterialAssetRefs](GetMaterialAssetRefs.md)。
|
2026-03-26 21:32:43 +08:00
|
|
|
|
|
|
|
|
## 相关文档
|
|
|
|
|
|
|
|
|
|
- [返回类型总览](MeshRendererComponent.md)
|
|
|
|
|
- [GetMaterial](GetMaterial.md)
|
2026-04-03 11:50:13 +08:00
|
|
|
- [GetMaterialAssetRefs](GetMaterialAssetRefs.md)
|