メインコンテンツまでスキップ

Engine APIの呼び出し

PlayCanvasスクリプトを記述する際、PlayCanvas Engine APIを操作することになります。このページでは、スクリプトで最も頻繁に使用する重要なクラスとパターンについて説明します。

スクリプト開発者向けの主要クラス

スクリプトコンテキスト

すべてのスクリプトは、以下のコアオブジェクトにアクセスできます。

this.app        // メインアプリケーション (AppBase)
this.entity // このスクリプトがアタッチされているエンティティ
important

this.appthis.entityは、Scriptインスタンスで定義されたメソッド(initializeupdateなど)内でのみ有効です。JavaScriptのthisキーワードについて詳しくはこちらをご覧ください。

必須クラス

AppBase - アプリケーション

// 一般的なアプリケーション操作
this.app.fire('game:start');
const player = this.app.root.findByName('Player');
const texture = this.app.assets.find('logo', 'texture');

Entity - シーン内のオブジェクト

// 一般的なエンティティ操作
this.entity.setPosition(0, 5, 0);
this.entity.rotate(0, 90, 0);
const child = this.entity.findByName('Weapon');

Component - エンティティに機能を追加する

// コンポーネントへのアクセス
const camera = this.entity.camera;
const light = this.entity.light;
const rigidbody = this.entity.rigidbody;
const sound = this.entity.sound;

数学クラス

計算や変換のためにこれらをインポートします。

import { Vec3, Quat, Color } from 'playcanvas';

const position = new Vec3(0, 5, 0);
const rotation = new Quat();
const red = new Color(1, 0, 0);

一般的なスクリプトパターン

エンティティの検索

// 名前で検索(階層全体を検索)
const player = this.app.root.findByName('Player');

// タグで検索(配列を返す)
const enemies = this.app.root.findByTag('enemy');

// 現在のエンティティからの相対パス
const weapon = this.entity.findByPath('Arms/RightHand/Weapon');

アセットの操作

// アセットを検索してロード
const sound = this.app.assets.find('explosion', 'audio');
sound.ready(() => {
this.entity.sound.play('explosion');
});
this.app.assets.load(sound);

イベントと通信

// アプリケーションイベントの発火
this.app.fire('player:died', this.entity);

// イベントのリッスン
this.app.on('game:start', this.onGameStart, this);

さらに学ぶ

ヒント

IDEサポート: スクリプト記述中にオートコンプリートとインラインドキュメントを利用するには、VS Code Extensionを使用してください。