You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在RealityKit 2.0中为Plane Entity应用自定义纹理并解决iOS15废弃API警告

解决方案

iOS 15 及以上系统中,SimpleMaterial 将原有的 baseColor 和 tintColor 两个属性合并为统一的 color 属性,可同时传入纹理和色调参数,直接替换对应代码即可消除废弃警告:

func createBoard() {
    let planeMesh = MeshResource.generatePlane(width: 1, 
                                              height: 1, 
                                        cornerRadius: 0.2)
    var myMaterial = SimpleMaterial()
    // 合并原baseColor和tintColor参数
    myMaterial.color = try! .init(tint: .blue, texture: .load(named: "texture.png"))

    myMaterial.metallic = MaterialScalarParameter(floatLiteral: 0.5)
    myMaterial.roughness = MaterialScalarParameter(floatLiteral: 0.5)

    let modelEntity = ModelEntity(mesh: planeMesh, materials: [myMaterial])
}

参数说明

  • 构造参数 tint 对应原废弃的 tintColor 属性,如果不需要调整原始纹理的色调,设置为 .white 即可完全保留纹理本身的色彩效果
  • 构造参数 texture 对应原废弃的 baseColor 纹理参数,不需要加载自定义纹理时传 nil 即可实现纯色材质

多版本兼容方案

如果需要适配 iOS 15 以下的系统,可以增加版本分支判断:

var myMaterial = SimpleMaterial()
if #available(iOS 15, *) {
    myMaterial.color = try! .init(tint: .blue, texture: .load(named: "texture.png"))
} else {
    myMaterial.baseColor = try! .texture(.load(named: "texture.png"))
    myMaterial.tintColor = .blue
}

内容的提问来源于stack exchange,提问作者Damiano Miazzi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 20:15:03