このセクションの目標:役割の移動、プロセスの統合
ラヤコマーシャル3Dゲーム開発
ロールクラスの
ゲームディレクトリに新しいPlayer.tsを作成します
// MonoBehaviourはフレームワーククラスであり、gameObjectとtransform属性が
デフォルトでエクスポートされます。デフォルトクラスPlayer extends MonoBehaviour {
localx = 0;
アニメーター:Laya.Animator;
onAwake(){ super.onAwake(); this.animator = this.gameObject.getChildByName( 'CatBase')。getComponent(Laya.Animator); this.animator.play( 'Cat_RunLong'); }
onUpdate(){ //左スワイプジェスチャif(Input.slideLeft){ this.localx + = 1.25; } //右スワイプジェスチャif(Input.slideRight){ this.localx- = 1.25; }
//插值计算,圆滑移动
this.transform.localPositionX = Mathf.Lerp(this.transform.localPositionX, this.localx, 0.2)
}
}
U3Dと協力して、役割クラスを説明します
Game目录下新建Game.ts
は「…/ UnityEngine / GameObject」からGameObjectをインポートします。
「./Player」からPlayerをインポートします。
「./ObstacleSpawn」から{ObstacleSpawn}をインポートします。
「./FishSpwan」から{FishSpwan}をインポートします。
「./BuildSpawn」からBuildSpawnをインポートします。
「…/ UnityEngine / SceneManager」からSceneManagerをインポートします。
デフォルトクラスのエクスポートGameはLaya.Scriptを拡張します{
static instance: Game;
scene: Laya.Scene3D;
cureentZ = 0;
speed = 0.3;
spwans: BuildSpawn[] = [];
playerPivot: Laya.Sprite3D;
player: Player;
obstacleSpawn: ObstacleSpawn;
onAwake() {
Game.instance = this;
this.scene = SceneManager.game;
}
onStart() {
let bulidjsonStr = '{"findRoot":"Resources/BuildItem","spwanItemDatas":[{"goName":"IndustrialWarehouse01","length":18.0},{"goName":"IndustrialWarehouse03","length":27.0}],"startCreateZ":0.0,"CreateLength":150.0,"recoverOffset":-10.0}';
let fishjsonStr = '{"findRoot":"Resources/items","spwanItemDatas":[{"goName":"Fish","length":1.0}],"startCreateZ":30.0,"CreateLength":150.0,"recoverOffset":-5.0}';
let obstaclejsonStr = '{"findRoot":"Resources/items","spwanItemDatas":[{"goName":"ObstacleRoadworksBarrier","length":1.0}],"startCreateZ":30.0,"CreateLength":150.0,"recoverOffset":-5.0}';
let buildSpwan = new BuildSpawn();
buildSpwan.spwanConfigObj = JSON.parse(bulidjsonStr);
buildSpwan.scene = this.scene;
this.scene.addComponentIntance(buildSpwan);
this.spwans.push(buildSpwan);
let fishSpwan = new FishSpwan();
fishSpwan.spwanConfigObj = JSON.parse(fishjsonStr);
fishSpwan.scene = this.scene;
this.scene.addComponentIntance(fishSpwan);
this.spwans.push(fishSpwan);
let p_ObstacleSpawn = new ObstacleSpawn();
p_ObstacleSpawn.spwanConfigObj = JSON.parse(obstaclejsonStr);
p_ObstacleSpawn.scene = this.scene;
this.scene.addComponentIntance(p_ObstacleSpawn);
this.spwans.push(p_ObstacleSpawn);
this.obstacleSpawn = p_ObstacleSpawn;
this.playerPivot = this.scene.getChildByName('PlayerPivot') as Laya.Sprite3D;
let charater = GameObject.Find(this.scene, 'PlayerPivot/charater')
this.player = charater.addComponent(Player);
}
onUpdate() {
this.cureentZ += this.speed;
for (let index = 0; index < this.spwans.length; index++) {
this.spwans[index].currentZ = this.cureentZ;
}
}
onLateUpdate() {
let pos = this.playerPivot.transform.position;
pos.z = this.cureentZ;
this.playerPivot.transform.position = pos;
}
}
スクリプトの下に追加
GameSample.ts
「./UnityEngine/SceneManager」からSceneManagerをインポートします。
「./Game/Game」からゲームをインポートします。
エクスポートクラスGameSample { public static StartGame(){ SceneManager.LoadSceneByName( 'Game'、this、this.OnGameSceneLoadOk); }
static OnGameSceneLoadOk(p_Scene3D: Laya.Scene3D) {
Laya.stage.addChildAt(p_Scene3D, 0);
SceneManager.game = p_Scene3D;
p_Scene3D.addComponent(Game);
}
}
Main.ts
onConfigLoaded():void { let node = new Laya.Node(); Laya.stage.addChild(node); node.addComponent(UnityEnagine); GameSample.StartGame(); }
F8 f5