上传个加载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);
}
}