上传个加载3D资源场景的Laya例子,方便以后测试不用重复写

上传个加载3D资源场景的Laya例子,方便以后测试不用重复写

首先创建个空的laya工程,创建个空场景
然后创建个 GameApp.ts脚本,挂到场景上去
运行就行了

export default class GameApp extends Laya.Script {
    public btn1: Laya.Button;
    public btn2: Laya.Button;
    public btn3: Laya.Button;
    private mainScene: Laya.Scene3D;
    private player: Laya.Sprite3D;
    private skinMeshArr: Laya.SkinnedMeshSprite3D[] = [];
    onAwake() {
        this.btn1 = this.owner.getChildByName("btn1") as Laya.Button;
        this.btn2 = this.owner.getChildByName("btn2") as Laya.Button;
        this.btn3 = this.owner.getChildByName("btn3") as Laya.Button;
        SceneManager.LoadScene("res/unityAssets/LayaScene_mainScene/Conventional/mainScene.ls", this, (scene) => {
            this.mainScene = scene;
            Laya.stage.addChildAt(this.mainScene, 0);
            this.player = this.mainScene.getChildByName("player") as Laya.Sprite3D;
            let Geometry: Laya.Sprite3D = this.player.getChildByName("Geometry") as Laya.Sprite3D;
            for (let index = 0; index < Geometry.numChildren; index++) {
                let sms: Laya.SkinnedMeshSprite3D = Geometry.getChildAt(index) as Laya.SkinnedMeshSprite3D;
                this.skinMeshArr.push(sms);
            }
            this.SwitchModelIndex(0);
        });
    }

    onClick(e: Laya.Event) {
        switch (e.target) {
            case this.btn1:
                this.SwitchModelIndex(0);
                break;
            case this.btn2:
                this.SwitchModelIndex(1);
                break;
            case this.btn3:
                this.SwitchModelIndex(2);
                break;
        }
    }
    
    public SwitchModelIndex(i: number) {
        for (let index = 0; index < this.skinMeshArr.length; index++) {
            if (index == i) {
                this.skinMeshArr[index].active = true;
            } else {
                this.skinMeshArr[index].active = false;
            }
        }
    }
}

场景加载脚本

export default class SceneManager {

  public static mainScene: Laya.Scene3D=null;
  public static gameScene: Laya.Scene3D = null;

  /**通過url加載場景 */
  public static LoadScene(url: string, callder, callbackFunc: Function) {
    Laya.Scene3D.load(url, Laya.Handler.create(SceneManager, SceneManager.OnLoadLevelok, [url, callder, callbackFunc]));
  }

  /**通過場景名稱加載場景
   * 需要配置路径
   */
  public static LoadSceneByName(name: string, callder, callbackFunc: Function) {
    let url = "" + name + ".ls";
    Laya.Scene3D.load(url, Laya.Handler.create(SceneManager, SceneManager.OnLoadLevelok, [url, callder, callbackFunc]));
  }

  static OnLoadLevelok(url, callder, callback: Function, scene: Laya.Scene3D) {
    callback.call(callder, scene);
  }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XyX许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值