C#System.Collections.Generic
C#System.Collections.Generic
C#のSystem.Collections.Generic関連の覚書です
- 何度か確認したがunity_web_playerのコンパイル、ビルドではDictionaryBaseクラスを利用すること自体コンパイルエラーが出る
Dictionaryプロパティ自体がまともに利用できない状態で原因は不明。PCビルドでは問題ないので固有の問題?(2014/04/25現在)
sets/CatDictionary.cs(8,34): error CS0103: The name `Dictionary' does not exist in the current context
こんな感じのエラー。見つからない事は無い筈なのだが・・・
<原因っぽいのが判明>
端的に要約して言うとUnityでは「System.Collections 名前空間」のクラスの動作保証はなく
「System.Collections.Generic 名前空間」では動作が確実っぽい。つまり旧式のコレクションクラスに動作保証は無く
比較的、新しめなジェネリックなコレクションクラスの方が安定して動作するのかな?と
あまり関係ないかもしれませんが一応・・・
LinkedList関連
<サンプル>
値に変更を加える
LinkedListからはコレクションを加工できないのでLinkedListNodeを利用している点に注目
forループの使い方が非常に興味深い
void moveVertices (LinkedList<Vertices> vertices, float deltaTime) { for ( LinkedListNode<Vertices> node = vertices.Last ; node != null ; node = node.Previous) { //この行は重要なテクニック Vertices v = new Vertices(); v = node.Value; v.position += v.direction * v.speed * deltaTime; v.lifeTimes = v.lifeTimes - deltaTime; if(v.lifeTimes<0){ v.deathFlag = true; } node.Value = v; } }
DictionaryBaseとDictionaryEntry関連
(追加情報)このクラスはunityでは使わない方が良いです
■DictionaryBaseサンプル
using UnityEngine;
using System.Collections;
/// <summary> /// ベクターメッシュを管理するためのカスタム化された専用辞書クラス /// 適切なタイミングでVectorMeshクラスのOn~機能などを呼んでいる点に特徴がある /// </summary>
public class VectorMeshDictionary : DictionaryBase {
//ここでDictionaryを使えるようにしている public VectorMesh this[ string key ]{ get{return (VectorMesh) Dictionary[key];} set{Dictionary[key] = value;} } public ICollection Keys{ get { return Dictionary.Keys; } } public ICollection Values{ get { return Dictionary.Values; } } public void Add(string key,VectorMesh value){ if(Dictionary.Contains(key)){ Dictionary.Remove(key); } Dictionary.Add(key,value); } public bool Contains(string key){ return Dictionary.Contains(key); } public void Remove(string key ){ Dictionary.Remove(key); } protected override void OnInsertComplete (object key, object value) { VectorMesh vm = (VectorMesh) value; vm.OnInsertComplete(); //VectorMeshクラスの関数を呼び出す base.OnInsertComplete (key, value); }
protected override void OnRemoveComplete (object key, object value) { VectorMesh vm = (VectorMesh) value; vm.OnRemoveComplete(); //VectorMeshクラスの関数を呼び出す base.OnRemoveComplete (key, value); }
}
■DictionaryEntryサンプル
DictionaryBase内の辞書データーの名前やキーを取り出す際はDictionaryEntryクラスを利用する
public void Clear(){ string[] removeList = new string[with.Count]; int i=0;
//DictionaryBase内の辞書データーの名前やキーを取り出す際はDictionaryEntryクラスを利用する foreach (DictionaryEntry item in with) { removeList[i] = (string)item.Key; i++; } //foreachは基本的に非破壊操作なのでこうする必要がある for (int j = 0; j < removeList.Length; j++) { with.Remove(removeList[j]); } }
メニュー
- 最終更新:2014-06-13 12:20:01