2016-12-05 11 views
0

私は開発中のゲームを持っています。それはTBSであり、多くの生き物が関わっています。私は、実行時に持っているすべての生き物を配列に格納したいと思っていました。しかし、私はそれらの配列の項目からすべてのスクリプトを呼び出すことができる必要があります:Array内のGameObjects上で異なる名前のスクリプトを一度に呼び出すにはどうすればよいですか?イニシアティブin D & D.UnityのGameObject []からスクリプトを選択

答えて

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; 
    } 
} 
+0

、あなたはn個ますこれらのメソッドをあなたが100%確実であると確信しているタイプにキャストしない限り、これらのメソッドを公開しないでください。 – DeeKayy90

+0

これは新しい方法です。しかし、ここで私は '' Public GameObject [] Combatants''と '' Combatants = GameObject.FindGameObjectsWithTag( "tagname");を実行しようとしている例を示し、最後にそれらのアイテムすべてからスクリプトを得ることができます。それを具体的に行う方法はありますか? – Sora

+0

「スクリプトを入手する」とはどういう意味ですか?自分の「戦闘員」のそれぞれからメソッドを呼び出すことを意味しますか?あるいは、それらの '戦闘員'ゲームオブジェクトにどのスクリプトが付いているのか知りたいですか?あなたのメソッドは最も確実に動作し、 'GameObject'の配列を返しますが、' GameObject'型であることを知っていなければなりません。つまり、型にキャストしたり、型付き変数に代入しなければ、 'GameObject'クラスには存在しないので、' Combatant'にあるメソッドにアクセスすることができます。 – DeeKayy90

関連する問題