20日目 Unity 連想配列(Dictionary)

//配列の宣言
        Dictionary<string, int> a = new Dictionary<string, int>()
        {
            {"item1",8},
            {"item2",4},
            {"item3",12},
        };

        //上書き
        a["item1"] = 3;

        //要素の削除
        a.Remove("item1");

        //要素の初期化
        a.Clear();

 

例文

//Dictionary生成

 

Dictionary<string, int> scores = new Dictionary<string, int>()
    {
        { "国木田",99},
        { "津島", 0}
    };


    private void Update()
    {        
        if (true == Input.GetKeyDown(KeyCode.A))
        {
            scores["国木田"]++; //国木田の中身を++する
        }
        if (true == Input.GetKeyDown(KeyCode.A))
        {
            scores["津島"]++; //津島の中身を++する
        }       

  foreach(KeyValuePair<string,int> s in scores)
        {
            Debug.Log(s.Key + "" + s.Value); //string形+int形でデバッグする
        }
    }

 

2Dの勉強をしていましたが、なかなかうまくいかない箇所があり、気分転換で3Dの授業を受けてみました(泣

2Dの方では、publicでの取得からテキストの変更やボタンの動作など、完成系があればほとんどできるようになりました!ですが、、、

スクリプトごとに取得しているためつながりが分かりにくくなっていること、引数の受け渡し方法、PrefabをInstantiateした後にDestroyできないことなど、課題が山ほどあります。

ただ、確実にスクリプトの中身が読めるようになってきたこと、できないながらも方法を変えてチャレンジするなど、少しずつ進歩はしていると思います。

勉強時間合計:33h