Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UnityでのLINQ活用例

 UnityでのLINQ活用例

2015/02/20(金)の勉強会の資料です。

※元々はSlideShareで公開していた資料です。

RyotaMurohoshi

February 20, 2015
Tweet

More Decks by RyotaMurohoshi

Other Decks in Technology

Transcript

  1. ͜ΜͳΫϥε͕͋ͬͯ public class Monster { public int Id { get;

    set; } public string Name { get; set; } public int Hp { get; set; } /* தུ */ }
  2. Α͋͘Δforeachɺifͷίʔυ List<Monster> monsterList = LoadMonsterList (); // LINQΛ࢖Θͳ͍ͱ int deadMonsterCount

    = 0; foreach (Monster monster in monsterList) { if(monster.Hp <= 0) { deadMonsterCount++; } }
  3. ͜ͷίʔυɺͲ͏ಡΈ·͢ʁ List<Monster> monsterList = LoadMonsterList (); // LINQΛ࢖Θͳ͍ͱ int deadMonsterCount

    = 0; foreach (Monster monster in monsterList) { if(monster.Hp <= 0) { deadMonsterCount++; } }
  4. ҰํLINQ͸ʁͲ͏ಡΈ·͢ʁ // LINQΛ࢖͏ͱ List<Monster> monsterList = LoadMonsterList (); // Hp͕0ҎԼͷཁૉͷ਺ʹ

    int deadMonsterCount = monsterList.Count ((Monster monster) => monster.Hp <= 0);
  5. ϑΟϧλϦϯάͱ͍͑͹Where ͜ΕΛisԋࢉࢠͱ͋Θͤͯ foreach (UnityEngine.Object target in Selection.objects.Where (o => o

    is Texture)){ // Ωϟετ͢Δ or asԋࢉࢠར༻͕ΊΜͲ͍ Texture texture = target as Texture; // ͜͜ͰTextureʹԿ͔͠Βͷॲཧ }
  6. CastϝιουͰΩϟετ΋Ͱ͖·͕͢ foreach (Texture texture in Selection.objects .Where (o => o

    is Texture) .Cast<Texture>()){ // ͜͜ͰTextureʹԿ͔͠Βͷॲཧ }
  7. DefaultIfEmpty γʔέϯε͕ۭͳΒ͹Ҿ਺Ͱ౉ͨ͠΋ͷΛ།Ұͷཁૉͱ͢Δγʔέϯεʹม׵ List<Monster> monsterList = LoadMonsterList (); Monster defaultMonster =

    GetDefaultMonster (); int targetId = GetTargetId (); // ࢦఆIDͷϞϯελʔΛ୳͢ɻͳ͔ͬͨΒdefaultMonsterʹɻ Monster targetMonster = monsterList .Where ((Monster monster) => monster.Id == targetId) .DefaultIfEmpty (defaultMonster) .First (); // ͜ͷΦʔόʔϩʔυ͸୯७ʹઌ಄ཁૉऔಘ
  8. ͜ΜͳΫϥεΛ࢖͍·͢ public class PlayLog { public int StageId { get;

    set; } public int Score { get; set; } public DateTime PlayedAt { get; set; } public TimeSpan PlayTime { get; set; } }
  9. OrderBy ֤ཁૉʹม׵Λࢪͦ͠ͷ݁ՌͰฒͼସ͑ List<PlayLog> playLogList = LoadPlayLogList (); // είΞͷ߱ॱͰฒͼସ͑ IEnumerable<PlayLog>

    orderedPlayLog = playLogList .OrderByDescending ((PlayLog playLog) => playLog.Score); OrderBy͸ঢॱͰฒͼସ͑ɺOrderByDescending͸߱ॱͰฒͼସ͑ ݩͷσʔλΛฒͼม͑ΔͷͰͳ͘ɺ৽ͨʹฒͼସ͑ͨγʔέϯεΛͭ͘Δ
  10. ThenBy OrderByͰฒͼସ͑ͨ΋ͷͰɺಉධՁͩͬͨ΋ͷΛฒͼସ͑ List<PlayLog> playLogList = LoadPlayLogList (); // είΞͷ߱ॱͰฒͼସ͑ͯɺಉ఺ͳΒ͕࣌ؒ୹͍ํΛઌʹ IEnumerable<PlayLog>

    orderedPlayLog =ɹplayLogList .OrderByDescending ((PlayLog playLog) => playLog.Score) .ThenBy ((PlayLog playLog) => playLog.PlayTime);
  11. Skip+Take σʔλΛը໘ʹ10ݸͣͭදࣔɺࠓճ͸3ϖʔδ໨(ΠϯσοΫε͸2) List<PlayLog> playLogList = LoadPlayLogList (); int numPerPage =

    10; int pageIndex = 2; int skipNum = numPerPage * pageIndex; // 3ϖʔδ໨(ΠϯσοΫε͸2)ɺ20൪͔Β10ݸͷཁૉΛऔಘ IEnumerable<PlayLog> selectedPlayLogs =ɹplayLogList .Skip (skipNum) .Take (numPerPage);
  12. SkipWhile)+)TakeWhile List<PlayLog> playLogList = LoadPlayLogList (); DateTime start = GetStartTime

    (); DateTime end = GetEndTime (); // PlayedAt͕startΑΓޙorಉ࣌ࠁͰɺendΑΓલͳཁૉΛऔಘ IEnumerable<PlayLog> selectedPlayLogs =ɹplayLogList .SkipWhile ((PlayLog playLog) => playLog.PlayedAt < start) .TakeWhile ((PlayLog playLog) => playLog.PlayedAt < end);
  13. ͜ΜͳΫϥεΛ࢖͍·͢ public class Item { public int Id { get;

    set; } public string Name { get; set; } public ItemType ItemType { get; set; } } public enum ItemType { Weapon, ConsumptionItem, ValuableItem }
  14. ToDic&onary γʔέϯε͔ΒࣙॻΛ࡞Δ!(ͦͷ1) List<Item> itemList = LoadItemList (); // IdϓϩύςΟΛΩʔͱ͢ΔσΟΫγϣφϦͷੜ੒ //

    ΋͠Ωʔͷॏෳ͕͋ͬͨΒྫ֎͕ൃੜ Dictionary<int, Item> itemDict = itemList .ToDictionary((Item item) => item.Id);
  15. ToDic&onary γʔέϯε͔ΒࣙॻΛ࡞Δ!(ͦͷ2) List<Item> itemList = LoadItemList (); // IdϓϩύςΟΛΩʔɺNameϓϩύςΟόϦϡʔͱ͢Δ //

    σΟΫγϣφϦͷੜ੒ // ΋͠Ωʔͷॏෳ͕͋ͬͨΒྫ֎͕ൃੜ Dictionary<int, string> itemDict = itemList.ToDictionary( (Item item) => item.Id, (Item item) => item.Name);
  16. ToLookup γʔέϯε͔ΒILookupΛ࡞Δ(άϧʔϓԽͨ͠ίϨΫγϣϯΛੜ੒) List<Item> itemList = LoadItemList (); // ILookup<ItemType, Items>͸

    // Dictionary<ItemType,List<Item>>ʹࣅ͍ͯΔ ILookup<ItemType, Item> itemLookup = itemList. ToLookup((Item item) => item.ItemType); // σΟΫγϣφϦͷΑ͏ʹ[]ͱΩʔͰͦͷάϧʔϓʹΞΫηεͰ͖Δ IEnumerable<Item> weapons = itemLookup[ItemType.Weapon];
  17. SelectMany ϦετͷϦετͷฏ׈ԽΛߦ͏(ͦͷ1) // ϞϯελʔͷϦετͷϦετ List<List<Monster>> listOfMonsterList = LoadListOfMonsterList (); //

    Ϟϯελʔͷγʔέϯεʹ IEnumerable<Monster> monsters = listOfMonsterList .SelectMany ((List<Monster> list) => list);
  18. ͜ΜͳΫϥε͕͋ͬͯ public class MonsterParty { public string Name { get;

    set; } // MonsterͷϦετɺ͜ͷϓϩύςΟ͕ϛι public List<Monster> MonsterList { get; set; } }
  19. SelectMany ϦετͷϦετͷฏ׈ԽΛߦ͏(ͦͷ2) // ύʔςΟʔͷϦετ List<MonsterParty> monsterPartyList = LoadMonsterPartyList (); //

    Ϟϯελʔͷγʔέϯεʹ IEnumerable<Monster> monsters = monsterPartyList .SelectMany ((MonsterParty party) => party.MonsterList);
  20. Dis$nct ॏෳΛআ͘ // γϯϓϧͳྫ int[] ids = new int[]{0, 1,

    1, 2, 2, 2, 3, 3, 4}; // ॏෳ͕আ͔Ε͍ͯΔ IEnumerable<int> distinctedIds = ids.Distinct ();
  21. Dis$nct ॏෳΛআ͘ // ύʔςΟʔʹಉ͡Ϟϯελʔ͸͍ͳ͍͕ɺଞͷύʔςΟʔʹ͸ಉ͡Ϟϯελʔ͕͍Δ List<MonsterParty> monsterPartyList = LoadMonsterPartyList (); //

    ॏෳͷແ͠ϞϯελʔͷIdͷγʔέϯεΛ࡞Δ IEnumerable<int> monsterIds = monsterPartyList // Ϟϯελʔͷγʔέϯεʹͯ͠ .SelectMany (party => party.MonsterList) // Idͷγʔέϯεʹͯ͠ .Select (monster => monster.Id) // ॏෳΛഉআ͢Δ .Distinct ();
  22. PlayLogΫϥεͱStageDataΫϥε͔ΒɺPlayDataΛ࡞Γ͍ͨ public class PlayLog { // StageͷIdͷΈͰStageͷ৘ใ͸ͳ͍ public int StageId

    { get; set; } public int Score { get; set; } /* தུ */ } public class StageData { public int Id { get; set; } public string Name { get; set; } /* தུ */ }
  23. PlayLogΫϥεͱStageDataΫϥε͔ΒɺPlayDataΛ࡞Γ͍ͨ public class PlayData { // StageDataΫϥεͷ໊લ͔Β public string StageName

    { get; set; } // PlayLogΫϥεͷScore͔Β public int Score { get; set; } /* தུ */ }
  24. foreachͳྫ // foreachͳྫɺ௕͍ List<PlayData> playDataList = new List<PlayData> (); foreach(PlayLog

    playLog in playLogList) { StageData targetStageData = null; foreach(StageData stageData in stageDataList) { if (playLog.StageId == stageData.Id) { targetStageData = stageData; break; // nullͳ΍ͭ͸ͳ͍ΑͶͱ͍͏ίʔυ } } playDataList.Add (new PlayData { StageName = targetStageData.Name, Score = playLog.Score, }); }
  25. LINQར༻ͳྫ // LINQར༻ͳྫɺ·ͩ௕͍ List<PlayData> playDataList = new List<PlayData> (); foreach(PlayLog

    playLog in playLogList) { StageData stageData = stageDataList.First (stage => stage.Id == playLog.StageId); playDataList.Add (new PlayData { StageName = stageData.Name, Score = playLog.Score, }); }
  26. Joinͷྫ IEnumerable<PlayData> playDatas = playLogList.Join ( stageDataList, (PlayLog playLog) =>

    playLog.StageId, // PlayLogͷΩʔ (StageData stageData) => stageData.Id, // StageDataͷΩʔ // ೋͭͷΩʔ͕ἧͬͨPlayLogͱStageData͕དྷΔ (PlayLog playLog, StageData stageData) => new PlayData { StageName = stageData.Name, Score = playLog.Score, });