動作確認
Unity 5.1.3-f1 on MacOS X 10.8.5
helloというコマンドに対して、異なる応答を返すようにしたい。
Listで実装しようとしたら(おそらく)listは1つの要素しか使えないので、工夫が必要。
try1
とりあえずArray型に逃げた、ひよコードは以下。
listTestScript.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // for List
public class listTestScript : MonoBehaviour {
void Test_list_duplicate_registration() {
// List<int> [] myList = new [2] List<int> ();
// List<int> [] myList = new List<int> ()[2];
string [,] strList = new string[3, 2];
strList [0, 0] = "hello";
strList [0, 1] = "hello, Mike";
strList [1, 0] = "hello";
strList [1, 1] = "tadano shikabane no youda";
strList [2, 0] = "hello";
strList [2, 1] = "This number is not currently registered";
int idx;
for (int loop=0; loop<10; loop++) {
idx = Random.Range(0,3);
Debug.Log(strList[idx,0] + " >> " + strList[idx,1]);
}
}
void Start () {
Test_list_duplicate_registration ();
}
}
try2 (List<List>使用)
Listの中のList。まだ使い方がよくわかってないが、以下はできた。
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // for List
public class listTest : MonoBehaviour {
void Test_list_duplicate_registration() {
var ls = new List<List<int>> ();
var inls1 = new List<int> ();
inls1.Add (3);
inls1.Add (1);
inls1.Add (4);
ls.Add (inls1);
var inls2 = new List<int> ();
inls2.Add (2);
inls2.Add (7);
inls2.Add (1);
ls.Add (inls2);
for (int oi=0; oi<2; oi++) {
for (int idx=0; idx<3; idx++) {
var x = ls [oi] [idx];
Debug.Log ("" + idx.ToString () + ":" + x.ToString ());
}
Debug.Log("---");
}
}
void Start () {
Test_list_duplicate_registration ();
}
}
結果
0:3
1:1
2:4
---
0:2
1:7
2:1
πとネイピア数の途中まで表示。
try3 (Dictionary<string, List<string>>)
教えていただいたList<List<int>>
をもとに、Dictionaryと組み合わせる方法をやってみた。
dicListTest.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // for List, Dictionary
public class dicListTest : MonoBehaviour {
void displayAllElementWithKey(ref Dictionary<string,
List<string>> myDic, string searchKey) {
foreach(KeyValuePair<string, List<string>> pair in myDic) {
if (pair.Key != searchKey) {
continue;
}
Debug.Log("cmd:" + pair.Key.ToString());
foreach(var element in pair.Value) {
Debug.Log("res:" + element);
}
}
}
void Test_diclist() {
Dictionary <string, List<string>> myDic
= new Dictionary<string, List<string>> ();
string keystr;
// command 1
keystr = "hello";
var res1ls = new List<string> ();
res1ls.Add ("hello, Mike");
res1ls.Add ("tadano shikabane no youda");
res1ls.Add ("This number is not currently registered");
myDic.Add (keystr, res1ls);
// command 2
keystr = "Ca va bien?";
var res2ls = new List<string> ();
res2ls.Add ("Merci, beaucoup");
res2ls.Add ("honjitsu wa sentenn nari");
res2ls.Add ("Je ne peut pas parler Francaise.");
myDic.Add (keystr, res2ls);
displayAllElementWithKey (ref myDic, /* searchKey=*/"hello");
}
void Start () {
Test_diclist ();
}
}
とりあえずhelloキーの全要素の出力まではできた。
try4 (指定コマンドの応答をランダムに返す)
以下の関数を用意した。 searchKeyでmyDicを検索して、その中のListからランダムに返す。
void findElementWithKey(
ref Dictionary<string, List<string>> myDic, string searchKey) {
List<string> resList;
resList = myDic [searchKey];
int pos = Random.Range (0, resList.Count);
Debug.Log ("res:" + resList [pos]);
}
以下は使用例
for (int loop=0; loop<10; loop++) {
findElementWithKey (ref myDic, /* searchKey=*/"hello");
}
結果
ランダムに応答を返せている。