ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

cocos creator 笔记-Asset Bundle

cocos creator 笔记-Asset Bundle cocos creator实现使用Asset Bundle加载远端模型点击模型切换操作编译器版本 3.5.2生成远端包https://docs.cocos.com/creator/3.5/manual/zh/asset/bundle.html新建一个子包工程在资源下新建文件夹同时放置模型和新建场景如果想直接在引用的工程里面直接渲染的话勾选配置为Bundle编译文件在 build\web-desktop\remote使用nginx映射文件目录。主工程使用第一人称相机自身操作cocos creator 笔记-模型转动_暮雪...的博客-CSDN博客_cocos 模型https://blog.csdn.net/qq_22071421/article/details/126447808?spm1001.2014.3001.5501loadModel(){ let thatthis assetManager.loadBundle(http://192.168.0.107/data/cocosbundle/myBundle/remote/fashi, (err, bundle) { bundle.load(xxx/xxx,Prefab,function (err, prefab) { that.loadNode instantiate(prefab); that.loadNode.setPosition(new Vec3(0,0,10)) that.loadNode.setRotationFromEuler(new Vec3(0, -180, 0)); that.loadNode.setScale(0.5,1,0.5) that.loadNode.addComponent(RigidBody); that.loadNode.getComponent(RigidBody).useGravityfalse that.loadNode.addComponent(BoxCollider); that.loadNode.getComponent(BoxCollider).enabledtrue that.loadNode.getComponent(BoxCollider).sizenew Vec3(3,12,3) director.getScene().addChild(that.loadNode); }); }); }直接加载场景loadModel(){ assetManager.loadBundle(http://192.168.0.107/data/cocosbundle/myBundle/remote/fashi, (err, bundle) { bundle.loadScene(fashi, function (err, scene) { director.runScene(scene); }); }); }可能是因为模型制作不规范的原因添加BoxCollider时要指定size不然不能完整点击模型点击切换模型与自身操作简单使用变量 actionState记录操作对象屏幕上模型点击监听//入口引入 input.on(Input.EventType.TOUCH_START, this.onMouseTouchStart, this); property(Camera) private fristCamera: Camera null!; onMouseTouchStart(touch) { let touchPos touch.getLocation(); let ray this.fristCamera.screenPointToRay(touchPos.x,touchPos.y); if (PhysicsSystem.instance.raycastClosest(ray)) { let res PhysicsSystem.instance.raycastClosestResult; let hitNode res.collider.node; commonUtil.actionState!commonUtil.actionState } }模型响应后使用键盘进行转动onKeyDown(event: EventKeyboard) { if(!commonUtil.actionState){ moveUtil.movePosition(this.node, this.position_dis, event.keyCode) }else{ if(!this.loadNode){ return } viewUtil.viewModeDefault(this.loadNode, event) } } /** * 转动角色 * param node node * param event 键盘事件 * param rotax x单元角度 * param rotay y单元角度 */ static viewMode(node: Node, event: EventKeyboard, rotax: number, rotay: number) { let rota_target new Vec3(); let rota_move new Quat(); let rota_cur new Quat(); node.getRotation(rota_cur); //四元数转欧拉角 Quat.toEuler(rota_move, rota_cur); switch (event.keyCode) { case KeyCode.KEY_W: if (rota_move.x 0 rota_move.x -180) { if (rota_move.x - rotax -180) { rota_target new Vec3(rota_move.x - rotax 360, rota_move.y, 0); } else { rota_target new Vec3(rota_move.x - rotax, rota_move.y, 0); } } else { rota_target new Vec3(rota_move.x - rotax, rota_move.y, 0); } break; case KeyCode.KEY_S: if (rota_move.x 0 rota_move.x 180) { if (rota_move.x rotax 180) { rota_target new Vec3(rotax rota_move.x - 360, rota_move.y, 0); } else { rota_target new Vec3(rota_move.x rotax, rota_move.y, 0); } } else { rota_target new Vec3(rota_move.x rotax, rota_move.y, 0); } break; case KeyCode.KEY_A: if (rota_move.y 0 rota_move.y -180) { if (rota_move.y - rotay -180) { rota_target new Vec3(rota_move.x, rota_move.y - rotay 360, 0); } else { rota_target new Vec3(rota_move.x, rota_move.y - rotay, 0); } } else { rota_target new Vec3(rota_move.x, rota_move.y - rotay, 0); } break; case KeyCode.KEY_D: if (rota_move.y 0 rota_move.y 180) { if (rota_move.y rotay 180) { rota_target new Vec3(rota_move.x, rota_move.y rotay - 360, 0); } else { rota_target new Vec3(rota_move.x, rota_move.y rotay, 0); } } else { rota_target new Vec3(rota_move.x, rota_move.y rotay, 0); } break; } node.setRotationFromEuler(rota_target); }单元角度可以随意指定cocos 2d下禁止重力需要碰撞效果设置
返回列表