HarmonyOS鸿蒙Next中ArkGraphics3D库能加载沙箱里的文件吗?
HarmonyOS鸿蒙Next中ArkGraphics3D库能加载沙箱里的文件吗? Image可以加载沙箱里的文件,但是Scene.load似乎不可以 想问一下ArkGraphics3D库的Scene.load加载glb模型只能加载项目rawfile或者资源目录下的文件吗?
3 回复
【解决方案】
ArkGraphics 3D提供加载并解析GLB模型的能力,ArkGraphics 3D支持开发者将glb模型文件置于应用文件沙盒中,通过ArkGraphics 3D提供的Scene.load()完成模型文件的加载以及渲染。
除了使用$rawfile()加载还可以使用绝对路径的方式实现资源加载。
import { common } from '@kit.AbilityKit';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
aboutToAppear(): void {
let applicationContext = this.context.getApplicationContext();
// 获取应用缓存路径
let cacheDir = applicationContext.filesDir;
let fileUri= '/data/storage/el2' + cacheDir + '/aaa.glb'
Scene.load(fileUri)
}
build() {
// ...
}
}
【背景知识】
绝对路径获取可以应通过Context属性获取应用文件路径,详见获取应用文件路径。
更多关于HarmonyOS鸿蒙Next中ArkGraphics3D库能加载沙箱里的文件吗?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,ArkGraphics3D库可以加载沙箱内的文件。该库通过鸿蒙的安全沙箱机制访问应用私有目录,支持加载沙箱中的3D模型、纹理等资源文件。具体实现需使用鸿蒙提供的文件管理API,如通过ohos.file.fs模块获取沙箱路径,再调用ArkGraphics3D的相应接口加载资源。


