2016-10-01 1 views
0

私のゲームでは、ダイアログ画面と画像パネルがあり、それぞれの文字に3-frames animationと表示したいと思います。私は、このスクリプトでそれを行う:アレイ内の特別なフレームにアクセスする必要があります

​​

私は、すべての文字のすべての画像に対して1つの配列を持っています。どのように3つのイメージを配列から取りたいのですか?このような何か:

void Update() 
{ 
    switch(currentCharacterName) 
    { 
     case "character1": 
      // take elements 0 1 2 from array 'frames' 
      image.texture = frames[index]; 
      break; 
     case "character2": 
      // take elements 3 4 5 from array 'frames' 
      image.texture = frames[index]; 
      break; 
    } 
} 

enter image description here

+0

画像を辞書に保存できますか?可能な名前はどこかのリストに保存されていますか? –

+0

@MaorVeitsmanはい、できます – dima

答えて

1

私は私が正しく正しくあなたの質問を理解したいと考えています。また

string[] names = new[] {"name1", "name2", "name3"}; // sample array 
void Update(){ 
    int index = names.indexOf(currentCharacterName); 
    var framesByName = names.Skip(index * 3).Take(3).ToArray(); 
} 

あなたはすべての名前がそれぞれのインデックスを持っている辞書に名前の配列を変更した場合、あなたはそれがより速く、よりエレガントにすることができます。

Dictionary<string, int> names = new {{"name1", 1}, {"name2", 2}, {"name3", 3}}; // Now you can directly access the index without using indexOf 

しかし、最もエレガントな解決策は、彼らは道あなたのフレームを保存変えるために、次のようになります。これにより

public Dictionary<string, Texture[]> frames; // Here you can have pairs of character name and an array of its three textures.. 

あなたは、単に直接必要な3つの要素にアクセスすることができます。

+0

ありがとう!私はそれをします – dima

+1

LINQはおそらくこの特定のアプリケーションのために過剰です。新しい配列を生成する代わりに、 'index * 3'、' index * 3 + 1'、 'index * 3 + 2'のインデックスを使って元の配列の画像を参照することができます。 – Abion47

関連する問題