あなたの世界を作りなさい
スクリプティング
スクリプトインポート
3分
gameobjectにアタッチされている以外に、zepetoscriptは他のスクリプトから直接使用できます。 zepetoscriptをライブラリやモジュールのようにインポートして使用できます。typescriptの構文では、zepetoscriptファイルへの相対パスを使用する必要があります。 以下の状況を考慮すると、extracomponentをscriptimportにインポートしたい場合は、次の構文を使用する必要があります。 以下の例は、libフォルダーにあるextracomponent tsを呼び出すためのscriptimport tsでの宣言です。 typescript import extracomponent from ' /lib/extracomponent'; 👍 ヒント 別のtsファイルをインポートする際は、パスを正しく指定することが重要です。 相対パスの表記に慣れてください。 / は現在のディレクトリを示します。 / は親ディレクトリを示し、つまり現在のディレクトリの1つ上のレベルのディレクトリです。 インポートしたスクリプトで宣言されたエクスポートされた関数や変数には、サンプルのように直接参照することでアクセスできます。 this gameobject addcomponent\<extracomponent>(); const extracomponent = this gameobject getcomponent\<extracomponent>(); // メソッド呼び出しで値を取得 const count = extracomponent getcount(); // メソッド呼び出しで値を設定 extracomponent setcount(0); // 公開プロパティを取得 const resultstring = extracomponent stringproperty; 以下は、インポートされたtypescriptを示すサンプルコードです。 import { zepetoscriptbehaviour } from 'zepeto script'; import { text } from 'unityengine ui'; // パスからカスタムスクリプトをインポート import extracomponent from ' /lib/extracomponent'; export default class scriptimport extends zepetoscriptbehaviour { public resultui text; private extcomponent extracomponent; start() { // スクリプトコンポーネントを追加 this gameobject addcomponent\<extracomponent>(); this extcomponent = this gameobject getcomponent\<extracomponent>(); } update() { // メソッド呼び出しで値を取得 const count = this extcomponent getcount(); if (count > 10) { // メソッド呼び出しで値を設定 this extcomponent setcount(0); } // 公開プロパティを取得 const resultstring = this extcomponent stringproperty; // 結果を出力 console log(`result ${resultstring}`); this resultui text = resultstring; } } extracomponentsのサンプルコードです。 import { zepetoscriptbehaviour } from 'zepeto script'; export default class extracomponent extends zepetoscriptbehaviour { public stringproperty string; private message string; private count int; start() { this message = "こんにちは、ゼペト!"; this count = 0; } update() { this stringproperty = `${this message} ${this count++}`; } getcount() { return this count; } setcount(newcount int) { this count = newcount; } } 以下のテストコード出力画面を確認してください。