あなたの世界を作りなさい
スクリプティング
ゲームオブジェクトとコンポーネント
13分
世界では、オブジェクトを作成したり、特定のコンポーネントをオブジェクトに変更または追加することが非常に一般的です。 zepetoscriptは、unityスクリプトで使用されるのと同じインターフェースをサポートしています。 gameobjectとcomponentの基本例 ゲームオブジェクトの作成と削除: gameobject // gameobject 作成 const tempobj = new gameobject(); const obj = object instantiate(tempobj); // gameobject 削除 object destroy(obj); gameobjectsのコンポーネントを取得したり、新しいものを追加するには、getcomponentまたはaddcomponentを使用します。 component // ジェネリックを使用したgetcomponent const mytransform = this getcomponent\<transform>(); // ジェネリックを使用したaddcomponent const animator = this gameobject addcomponent\<animator>(); gameobject componentの完全なコード例を確認してください。 こちらは、gameobjectの作成とプロパティの変更、コンポーネントの追加、破壊をカバーする例です。 gameobjectsample import { gameobject, object, transform, animator, vector3, waitforseconds } from 'unityengine'; import { zepetoscriptbehaviour } from 'zepeto script'; export default class gameobjectsample extends zepetoscriptbehaviour { private testobject gameobject; start() { // 空のgameobjectを作成 const tempobj = new gameobject(); this testobject = object instantiate(tempobj) as gameobject; // オブジェクトの名前を変更 this testobject name = "testobject"; // オブジェクトの変換情報を変更 this testobject transform position = new vector3(3,3,3); // オブジェクトにコンポーネントを追加 this testobject gameobject addcomponent\<animator>(); // getcomponentの使用例 const animator = this testobject getcomponent\<animator>(); if(animator != null) { console log("getcomponent 成功"); } // コルーチンを使用して5秒後にオブジェクトを削除 this startcoroutine(this destroyobject()); } destroyobject() { yield new waitforseconds(5); // gameobjectを削除 object destroy(this testobject); } } findの活用 find関連メソッドは、unityスクリプトと同様にzepetoscriptで同じインターフェーススタイルをサポートしています。 メソッド 説明 gameobject find() 現在のシーン内の名前に基づいてアクティブなgameobjectオブジェクトを見つけて返します。 \ 見つからない場合はnullを返します。 gameobject findgameobjectwithtag() 現在のシーン内のタグに基づいてアクティブなgameobjectオブジェクトを見つけて返します。 \ アクティブなオブジェクトの中から指定されたタグに一致する最初のオブジェクトを見つけて返し、見つからない場合はnullを返します。 gameobject findgameobjectswithtag() 現在のシーン内の特定のタグを持つすべてのアクティブなgameobjectオブジェクトを見つけて配列として返します。 \ 見つからない場合は空の配列を返します。 例として、シーンを次のように設定します: いくつかの3dオブジェクトを追加し、すべてのタグを3dとして指定します。 スクリプトをアタッチするための空のオブジェクトを作成し、それをfindsampleに名前変更します。 typescript import { zepetoscriptbehaviour } from 'zepeto script'; import { gameobject, transform } from 'unityengine'; export default class findsample extends zepetoscriptbehaviour { public tempobj1 gameobject; public tempobj2 gameobject; public tempobj3 gameobject\[]; start() { // 指定された名前のターゲットgameobjectを見つけます。 // 同じ名前のgameobjectが複数ある場合、最初に見つかったものが返されます。 this tempobj1 = gameobject find("cube"); if(this tempobj1 != null) { console log("gameobjectの検索成功"); } // 指定されたタグのターゲットgameobjectを見つけます。 // 同じタグのgameobjectが複数ある場合、最初に見つかったものが返されます。 this tempobj2 = gameobject findgameobjectwithtag("3d"); if(this tempobj2 != null) { console log("findgameobjectwithtagの成功"); } // 指定されたタグを持つgameobjectの配列を返します。 this tempobj3 = gameobject findgameobjectswithtag("3d"); if(this tempobj3 != null) { console log("findgameobjectswithtagの成功"); } } } findsampleオブジェクトにスクリプトを追加すると、インスペクタは空になります。 再生ボタンを押して実行し、インスペクタウィンドウで各オブジェクトが割り当てられていることを確認し、コンソールウィンドウを通じてオブジェクトの検索成功を確認できます。 プレハブの作成 ランタイム中にプレハブを作成する方法を探ってみましょう。 まず、3dオブジェクト > キューブをシーンに追加します。 キューブのインスペクターで「コンポーネントを追加」を押し、rigidbodyを追加します。 次に、キューブをプロジェクトエリアにドラッグしてプレハブにします。 プレハブを利用するため、シーン内のキューブを削除します。 以下のスクリプトを書いてください。 typescript import { gameobject, object, vector3, waitforseconds } from 'unityengine'; import { zepetoscriptbehaviour } from 'zepeto script'; export default class clonesample extends zepetoscriptbehaviour { public cloneprefab gameobject; start() { this startcoroutine(this doroutine()); } doroutine() { while(true) { yield null; // create a clone const clone = object instantiate(this cloneprefab) as gameobject; // set the height of the clone object clone transform position = new vector3(0, 10, 0); // destroy it after 5 seconds of creation gameobject destroy(clone, 5); yield new waitforseconds(1); } } } 次に、unityエディタに戻り、プレハブをスクリプトインスペクタにドラッグして追加します。 再生ボタンを押して、3dオブジェクトが作成され、毎秒上から落ちてくることを確認します。 以下のリンクをクリックして、zepetoscriptが提供するunityのgameobjectsとcomponentsについて詳しく学んでください。 📘 gameobjectsの作成 https //docs unity3d com/2020 3/documentation/scriptreference/object instantiate html https //docs unity3d com/2020 3/documentation/scriptreference/object instantiate html 📘 gameobjectsの破壊 https //docs unity3d com/2020 3/documentation/scriptreference/object destroy html https //docs unity3d com/2020 3/documentation/scriptreference/object destroy html 📘 コンポーネントの使用 https //docs unity3d com/kr/current/manual/usingcomponents html https //docs unity3d com/kr/current/manual/usingcomponents html