如何在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
相关产品推荐
相关产品推荐

