私は開発中のゲームを持っています。それはTBSであり、多くの生き物が関わっています。私は、実行時に持っているすべての生き物を配列に格納したいと思っていました。しかし、私はそれらの配列の項目からすべてのスクリプトを呼び出すことができる必要があります:Array内のGameObjects上で異なる名前のスクリプトを一度に呼び出すにはどうすればよいですか?イニシアティブin D & D.UnityのGameObject []からスクリプトを選択
0
A
答えて
2
継承またはインターフェイスを使用していると仮定すると、基本クラス/インターフェイスの配列を作成し、その配列にすべての生き物を追加できます。あなたはこれらのすべての生き物の配列を持っていると言います)。
単純に、配列内のすべての要素をループして、それぞれのメソッドを呼び出します。例えば
:
//not a cut-and-paste example, but gives you the general idea.
foreach(var _creature in Array)
{
_creature.Die();
}
あなたは、それぞれに異なるメソッドを呼び出そうとしている場合しかし、あなたはこれがあなたのために動作しません見つけることがあります。
あなたはさらにそれを打破したい場合は、あなたがしようとスイッチタイプにして、各タイプごとに特定のメソッドを呼び出しますが、それは本当に一般的なクラスではないことができます。
例えば:
//not a cut-and-paste example, but gives you the general idea.
foreach(var _creature in Array)
{
switch typeof(_creature):
case LowLevel:
_creature.Flee();
break;
case MidLevel:
_creature.Guard();
break;
case HighLevel:
_creature.Attack();
break;
default:
_creature.Idle();
break;
}
多型の実装例:あなたの配列は `配列
public abstract class Combatant : MonoBehaviour
{
//for arguments sake, we are only going to have speed property, attack method which
//all instances have to implement, and a defend which only some might implement their
//own method for (the rest default to the base class)
int speed {get;set;}
//Need to have this method in derived classes
//We don't define a body, because the derived class needs to define it's own
protected abstract void Attack();
//Can be overriden in derived class, but if not, falls back to this implementation
protected virtual void Defend()
{
speed *= 0.5; //half speed when defending
}
}
public class MonsterA : Combatant
{
protected override void Attack()
{
//attack code goes here for MonsterA
}
//to access speed, we simply need to use base.speed
public void SomethingElse()
{
base.speed *= 1.25;
}
//notice we don't have to provide an implementation for Defend, but we can still access it by calling base.Defend(); - this will then run the base classes implementation of it.
}
public class MonsterB : Combatant
{
protected override void Attack()
{
//attack code goes here for MonsterB
}
//Assume this is a 'heavier' class 'monster', speed will go down more than standard when defending
protected override void Defend()
{
base.speed *= 0.3;
}
}
関連する問題
- 1. UnityのGameObject更新命令
- 2. gameObjectにロゴを追加Unity Unity
- 3. Unity GameObjectをコピーする
- 4. Unity GameObject setActive playerprefsに保存
- 5. 他のオブジェクトを含むリストのUnity GameObject
- 6. Unityのゲームオブジェクトの選択と選択
- 7. Unity GameObjectの速度とアニメーションの問題
- 8. Unity Battleships - GameObjectの色を変更する
- 9. Unityのgameobjectの奇妙なXとY
- 10. Unityでは、私のgameObjectはUnity 5.5のphysics2D
- 11. Unityは他のGameObjectのスクリプトを見つけることができません
- 12. GameObjectを転送するUnity NetworkServer/NetworkClient
- 13. Unity Editorスクリプティング - MouseOverでgameObjectを取得
- 14. unity android gameobjectが動作しない
- 15. コンポーネントgameObjectからコンテナgameObjectにアクセス
- 16. GameObjectを非表示にしてすべて表示ソートGameObject Unityを隠すC#
- 17. Unityで異なるGameObjectのアニメーションを再生するには?
- 18. UnityのGameObjectの名前を比較する方法
- 19. スクリプトからGameObjectコンポーネントを有効/無効にする[Unity3D]
- 20. UnityスクリプトからUIコントロールを作成
- 21. 別のスクリプトから変数C#UNITY
- 22. UnityスクリプトからのJWT認証
- 23. Unityでデバイスフィルタを選択していますか?デバイスフィルタを選択する際に
- 24. 適切なコンテナを選択するUnity
- 25. Unity Vuforia Google VR - GameObjectへのonPointerEnterを作成できません。
- 26. JPA(選択)から選択
- 27. Unity:Remove random.rangeから選択したオブジェクト
- 28. Unity 5.6でCrashlytics GameObjectが見つかりませんでした
- 29. JQueryテーブルセル選択スクリプト
- 30. 他のスクリプトで使用するスクリプトからのUnityアクセスブール値
、あなたはn個ますこれらのメソッドをあなたが100%確実であると確信しているタイプにキャストしない限り、これらのメソッドを公開しないでください。 – DeeKayy90
これは新しい方法です。しかし、ここで私は '' Public GameObject [] Combatants''と '' Combatants = GameObject.FindGameObjectsWithTag( "tagname");を実行しようとしている例を示し、最後にそれらのアイテムすべてからスクリプトを得ることができます。それを具体的に行う方法はありますか? – Sora
「スクリプトを入手する」とはどういう意味ですか?自分の「戦闘員」のそれぞれからメソッドを呼び出すことを意味しますか?あるいは、それらの '戦闘員'ゲームオブジェクトにどのスクリプトが付いているのか知りたいですか?あなたのメソッドは最も確実に動作し、 'GameObject'の配列を返しますが、' GameObject'型であることを知っていなければなりません。つまり、型にキャストしたり、型付き変数に代入しなければ、 'GameObject'クラスには存在しないので、' Combatant'にあるメソッドにアクセスすることができます。 – DeeKayy90