スクリプトを作成する
Vircadiaの堅牢なJavaScriptAPIは、VRで優れたコンテンツとユーザーエクスペリエンスを構築するためのツールを提供します。
このセクションでは、Vircadiaで一般的なタスクを実行するための簡単なコードサンプルを見つけることができます。これらのコードサンプルの動作を確認するには、コードをコンピューターのどこかに保存されているtestScripts.js
ファイルにコピーします。
Note
Entity scripts, unlike interface scripts, are in containing functions. The example scripts here cannot be attached to an entity (and be used as an entity script) unless they are in a containing function ``function() {}``.
コンテンツ
デバッグウィンドウへの出力
これはインターフェイススクリプトの例であり、エンティティにアタッチすることはできない。これは、何かをデバッグウィンドウ に出力する方法を示しています。この例では、まず単純な「Hello, World」スクリプトから始めます。
print("Hello, World");
これをコピーして
testScript.js
ファイルに貼り付け、コンピューターに保存します。このスクリプトを ロードして実行 すると、Vircadia の [デバッグウィンドウ] に 「Hello, World」 という文字が出力されます。
エンティティの作成
Createアプリを使用してエンティティを追加するの代わりに、interface scriptを使用して作成できます。
// Get your position in the domain, so that the cube is spawned in front of you
var position = Vec3.sum(MyAvatar.position, Quat.getFront(MyAvatar.orientation));
var properties = {
type: "Box",
name: "ScriptBox",
position: position,
color: { red: 255, green: 0, blue: 0 }
};
var entityID = Entities.addEntity(properties);
print("Entity added");
これをコピーして
testScript.js
ファイルに貼り付け、コンピューターに保存します。このスクリプトを読み込んで実行すると、ドメイン内のアバターが検索され、カスタマイズされたプロパティに基づいて新しいエンティティが作成されます。設定してから、[デバッグウィンドウ]に1行出力します。この場合、エンティティは赤いボックスになります。
エンティティの編集
エンティティのプロパティを操作するには、interface scriptでEntities.editEntity
を使用できます。
var entityID = Entities.addEntity({
type: "Box",
position: Vec3.sum(MyAvatar.position, Quat.getFront(MyAvatar.orientation)),
});
var properties = Entities.getEntityProperties(entityID, ["color"]);
print("Entity color: " + JSON.stringify(properties.color));
Entities.editEntity(entityID, {
color: { red: 255, green: 0, blue: 0 }
});
properties = Entities.getEntityProperties(entityID, ["color"]);
print("Entity color: " + JSON.stringify(properties.color));
これをコピーして
testScript.js
ファイルに貼り付け、コンピューターに保存します。このスクリプトを読み込んで実行すると、ドメイン内のアバターが検索され、カスタマイズされたプロパティに基づいて新しいエンティティが作成されます。設定してから、そのエンティティの色が[デバッグウィンドウ]に出力されます。次に、スクリプトはエンティティの色を赤に変更し、[デバッグウィンドウ]に新しい色が出力されます。
関連項目