あなたの世界を作りなさい
スクリプティング
UnityEvent
4分
unityは、ユーザーがc#イベントとデリゲートを使用しやすくするためにunityeventsを提供します。 unityeventsは、異なるオブジェクトから引数を渡したり、特定の要件が満たされたときにメッセージを送信する方法です。 zepetoscriptでunityeventを使用するには、以下の形式で宣言する必要があります。 まず、unityeventクラスを使用するために、以下のようにインポートコマンドを追加します。 typescript import { unityevent } from 'unityengine events'; unityeventのサンプル結合コードを参照してください。 event import { zepetoscriptbehaviour } from 'zepeto script'; import { unityevent } from 'unityengine events'; import { button } from 'unityengine ui'; export default class event extends zepetoscriptbehaviour { public btn button; private mevent unityevent; start() { // unityeventの新しいインスタンスを作成し、meventに割り当てます。 this mevent = new unityevent(); // meventに新しいリスナーを追加します。このリスナーは、meventが呼び出されたときにpingメソッドを実行します。 this mevent addlistener(() => this ping()); // ボタンがクリックされ、meventがnullでないかを確認します。 this btn onclick addlistener(() => { if (this mevent != null) { // 上記の条件が真であれば、meventを呼び出します。 this mevent invoke(); } }); } ping() { console log('ping'); } } スクリプトの説明 上記の例では、ボタンが押されるたびに‘mevent’という名前のunityeventが呼び出され、‘mevent’が呼び出されたときに‘ping’メソッドが実行されます。 したがって、ボタンが押されるたびに、コンソールに'ping'というメッセージが表示されます。 接続しようとしているイベントにパラメータがある場合は、以下のようにunityeventインポートコマンドを追加する必要があります。 typescript import { unityevent$1 } from 'unityengine events'; 👍 ヒント unityevent$1はunityeventのジェネリックバージョンです。 例えば、整数値を受け取るイベントを作成したい場合は、 unityevent$1\<int> unityevent$1のパラメータタイプはc#に従うことに注意してください。 パラメータを使用したunityevent$1のサンプルコードは以下の通りです。 event import { zepetoscriptbehaviour } from 'zepeto script'; import { unityevent$1 } from 'unityengine events'; import { input } from 'unityengine'; export default class event extends zepetoscriptbehaviour { private meventint unityevent$1\<int>; private countnum number; start() { this meventint = new unityevent$1\<int>(); this meventint addlistener(num => this count(num)); this countnum = 0; } update() { if ((input anykeydown) && (this meventint != null)) { this meventint invoke(this countnum); ++this countnum; if (this countnum > 100) { this countnum = 0; } } } count(num) { console log(`count ${num}`); } } unityeventsについて詳しく知りたい場合は、以下のリンクをクリックしてください。 📘 unityevents https //docs unity3d com/kr/530/scriptreference/events unityevent html https //docs unity3d com/kr/530/scriptreference/events unityevent html