今、Unityプログラマに教えて上げたいC#のあれやこれや

F46c97bb74758d481531990654933050?s=47 RyotaMurohoshi
November 25, 2017

 今、Unityプログラマに教えて上げたいC#のあれやこれや

2017/11/26(日)に開催された「プログラマのためのUnity勉強会」の資料です

F46c97bb74758d481531990654933050?s=128

RyotaMurohoshi

November 25, 2017
Tweet

Transcript

  1. ̿Ք̵UnityϤϺν϶ϫ΁ර͞ͼӤͨ͵͚C#΄͘΢Κͩ΢Κ̀ @RyotaMurohoshi 2017/11/26(ᰂ) ϤϺν϶ϫ΄͵Η΄Unityۣ䔶տ

  2. 㺔氂ͽͯ

  3. var names = new List<string> { "ΊΞΊΞ", "ΥΩΥΩ", "΁ΙЄΩ" };

    var actions = new List<Action>(); foreach (string name in names) { actions.Add(() => { Debug.Log(name); }); } foreach(var action in actions) { action(); }
  4. ΀Ω;ᤒᐏͫ΢ΡͽͭΝ͜͡Ҙ

  5. ΊΞΊΞ ΥΩΥΩ ΁ΙЄΩ

  6. Ͷ;௏͜ͽͭΝ͜Ҙ

  7. ΁ΙЄΩ ΁ΙЄΩ ΁ΙЄΩ

  8. ͹ͼᤒᐏͫ΢Ρ䁰ݳΘ͘ΠΔͯ

  9. Unity 5.4զӥ΀Ο̵̿΁ΙЄΩ̵΁ΙЄΩ̵΁ΙЄῺ Unity 5.5զӤ΀Ο̵̿ΊΞΊΞ̵ΥΩΥΩ̵΁ΙЄῺ

  10. ͩ΄Ξ͜΁΀Ρቘኧ΅̵ C#΄᥺承՛䯭独ͳ΄Ꮘ䃪ጱ䄜ๅ΁樛㭚ͭΔͯ

  11. ͳ΄ڹ΁ᛔ૩奧Օ

  12. @RyotaMurohoshi

  13. Ք෭扖ͯͩ;

  14. Ք෭΅̿ϤϺν϶ϫ΄͵Η΄Unityۣ䔶տ̀

  15. ̿Ք̵UnityϤϺν϶ϫ΁ර͞ͼӤͨ͵͚C#΄͘΢Κͩ΢Κ̀ ;氂ͭͼ………

  16. Unityͽ.NET 4.6͢Experimental΀2017ଙ΄ଙ๛΄Ք̵ C#ΨιЀιЀֵ͹ͼUnityͽοЄϭΨ樄咲ͭͼ͚Ρ ϤϺν϶ϫ΄Ε΀ͫΩ΁ݻͧͼ………

  17. ̿ΎЄC#͹ͼͩΩ΀՛䯭΀ΩͶ̀ ̿ͩΩ΀᥺承䱛ᚆ͘ΡΩͶЄ̀ ̿ͩ΄̵ࣳᎣΟ΀͡͹͵뺶̀ ͹ͼ௏͞ΡΞ͜΀扖ΨͭΔͯ

  18. ᙧว

  19. Α;ΩͿ΄ϤϺν϶ϫ΁;͹ͼ ̿ϤϺν϶ϬЀν᥺承΅ٍ᭲̀ 㰜΅ͳ͜௏͹ͼ͚Δ̶ͯ

  20. ͠܅ᘏͫΩ͢܅咅࢏ٍ΄ֵ͚ො̵͘ΔΠᎣΟ΀͡͹͵Ο͚ΚͽͯΞ΃Ҙ य़ૡͫΩ͢ૡٍ΄ֵ͚ො̵樌晅͹ͼ͚͵Ο͚ΚͽͯΞ΃Ҙ ෆ㯪ॊͫΩٍ᭲͢΄ֵ͚ො̵晒୮΀汜ᤈ䱛΁㪥Π͵ͥ΀͚ͽͯΞ΃Ҙ

  21. ٍ̿᭲̀Ψ΀͚ͭ͢Σ΁ͯΡϤϺ מ毲ͽͣ΀͚ͽͯΞ΃Ҙ

  22. ̿ϤϺν϶ϬЀν᥺承΅ٍ᭲̀Ͷ͡Οͩͳ̵ ϤϺ;ͭͼ晒ڔ΁ٍ᭲ΨᎣΠ̵ֵ͚ͩ΀ͯΏͣͶ;௏͚Δͯ

  23. Ք෭͠扖ͭͯΡٖ਻΅̵䋚᪢ጱ΀Θ΄ͽ΅͘ΠΔͱΩ ͩ΢ΨᎣ͹ͼΕ΀ͫΩ΄οЄϭͯͦ͢䄜ΥΡΥͧͽ΅͘ΠΔͱΩ

  24. ͧ΢Ϳ̿ͩ΢Ꭳ͹ͼ͚͠ͼΞ͡͹͵̀;௏͹ͼΘΟ͞Ρ C#΄͠扖Ψͭ͵͚;௏͚Δͯ

  25. ͫ͹ͣ΄̿΁ΙЄΩ̵΁ΙЄΩ̵΁ΙЄῺ ;ᤒᐏͫ΢Ρቘኧ΅̵ C#΄᥺承՛䯭独ͳ΄Ꮘ䃪ጱ䄜ๅ΁樛㭚ͭΔͯ

  26. ͳ΄1 foreach΄՛䯭;ͳ΄Ꮘ䃪ጱ䄜ๅ΄扖

  27. var names = new List<string> { "ΊΞΊΞ", "ΥΩΥΩ", "΁ΙЄΩ" };

    var actions = new List<Action>(); foreach (string name in names) { actions.Add(() => { Debug.Log(name); }); } foreach(var action in actions) { action(); }
  28. C# 4.0 -> 5.0ͽ՛䯭͢䄜ΥΠΔͭ͵

  29. C# 4.0;ͳ΢զڹͽ΅̵ ΁ΙЄΩ ΁ΙЄΩ ΁ΙЄΩ

  30. C# 5.0;ͳ΢զᴳͽ΅ ΊΞΊΞ ΥΩΥΩ ΁ΙЄΩ

  31. C# 5.0΄奾ຎΨ๗இͭΔͯΞ΃Ҙ

  32. var names = new List<string> { "ΊΞΊΞ", "ΥΩΥΩ", "΁ΙЄΩ" };

    var actions = new List<Action>(); foreach (string name in names) { actions.Add(() => { Debug.Log(name); }); } foreach(var action in actions) { action(); }
  33. foreach෈΄ٖ᮱՛䯭͢ C# 4.0 -> 5.0ͽ͡ΥΠΔͭ͵

  34. Θ;Θ;ቘኧ独ϮϷϐϕ͘͢͹ͼ C# 4.0΄ٖ᮱՛䯭΁ͭͼ͚͵΄ͽ̵ͯ͢ ϔϷοЄϕ独϶ϭύୗ΄ڥአ͢䓈ΔΠC# 5.0ͽ՛䯭䄜ๅ

  35. Unity5.4 -> 5.5ͽ̵᥺承΅ίϐϤϔЄϕͫ΢ͼ͚΀͚ͧͿ πЀϞα϶ͶͧίϐϤϔЄϕ̶foreach΄ٖ᮱՛䯭Θ䄜ๅ

  36. var names = new List<string> { "ΊΞΊΞ", "ΥΩΥΩ", "΁ΙЄΩ" };

    var actions = new List<Action>(); foreach (string name in names) { actions.Add(() => { Debug.Log(name); }); } foreach(var action in actions) { action(); }
  37. Unity5.4/C# 4.0զӥͽ΅̵ ΁ΙЄΩ ΁ΙЄΩ ΁ΙЄΩ

  38. Unity5.5/C# 5.0զӤͽ΅̵ ΊΞΊΞ ΥΩΥΩ ΁ΙЄΩ

  39. ͩ΄Unity5.4/C# 4.0΄՛䯭 ͽϜϫ͹͵ՈΘग़͚΄ͽ΅Ҙ

  40. // Quest΄ϷφϕΨQuestCell΄Ϸφϕ΁ϝαЀϖ // Unity 5.4զӥͽ̵ࢯΠͳ͜΀πЄϖֺ // Ϳ΄ψϸ͢μϷϐμͫ΢͵䦒Θ̵๋஍΄quests΄ᥝᔰͽOnSelectQuest List<Quest> quests =

    LoadQuests() int index = 0; foreach (Quest quest in quests) { QuestCell questCell = questCells[index++]; questCell.Bind(quest); questCell.SetClickListener(() => { OnSelectQuest(quest); }); }
  41. // ץྋғQuest΄ϷφϕΨQuestCell΄Ϸφϕ΁ϝαЀϖ // Unity 5.4զӥͽ̵ࢯΠͳ͜΀πЄϖֺ΄ࢧ᭿ᒽ // ๗இ᭗Π΄㵕ͣѺ List<Quest> quests =

    LoadQuests() int index = 0; foreach (Quest quest in quests) { QuestCell questCell = questCells[index++]; Quest targetQuest = quest; // ͩͩ͢ϪαЀϕ questCell.Bind(targetQuest); questCell.SetClickListener(() => { OnSelectQuest(targetQuest); }); }
  42. ݘ͚UnityΨֵ͹ͼ͚ΡՈΚ̵ ݘ͚UnityΘ䌏᨝΄϶αϣ϶Ϸ樄咲ᘏ΄Ո ᥝဳ఺

  43. ϷЀμ h"p:/ /ufcpp.net/study/csharp/ap_ver5.html#foreach h"ps:/ /qiita.com/RyotaMurohoshi/items/b246ea0f87117ea7f93f h"ps:/ /qiita.com/RyotaMurohoshi/items/740151bd772889cf07de

  44. Scrip&ng Run&me Versionͽ.NET 4.6(Experimetal)Ψ晝ΏΆ̵ C#΄ϝЄυϴЀίϐϤͽ᭄ےͫ΢͵ ࣳΚϮϊϐϖ̵͢UnityͽΘֵ͞Δͯ

  45. ͳ΄2 ZipϮϊϐϖ/Tupleμ϶φ

  46. ZipϮϊϐϖ Zip΅LINQ΄Ϯϊϐϖ 2ͺ΄IEnumerable<T>΄αЀϔϐμφ͢ݶͮΘ΄ݶॊͽ㳌ቘ ᥝᔰහ͢晅͜䁰ݳ̵㪒ො΄䟦͹͵ᥝᔰΔͽͭ͡㳌ቘͫ͢΢΀͚

  47. Tupleμ϶φ Ϯϊϐϖͽ愢හ΄ηϣυδμϕΨΔ;Ηͼᬬͭ͵͚ ܇ݷࣳ΅ϮϊϐϖΨΔ͵ͨ΀̵͚μ϶φ֢Ρ΄΅ΗΩͿ͚ͥͫ ͳΩ΀䦒΁׎ڥ̵TupleѺѺѺ

  48. UniRx΄IObservable.ZipϮϊϐϖΚUniRx.Tuple䯤᭜֛ͽ̵ 泿ວΕ͘͢ΡՈΘग़͚ͽͯΞ΃Ҙ

  49. List<string> strings = new List<string> { "A", "B", "C" };

    List<int> ints = new List<int> { 3, 1, 4, 1, 5 }; IEnumerable<Tuple<string, int>> zipped = strings.Zip(ints, (s, i) => Tuple.Create(s, i)); foreach(Tuple<string, int> tuple in zipped) { Debug.Log(tuple); } // (A, 3) // (B, 1) // (C, 4)
  50. System.Tuple΅ͳͩΔͽ׎ڥͮΙ΀͚ Item1;͡Item2͹ͼݷڹ̵Υ͡ΠͻΟ͚ C# 7.0ͽ΄όϤϸ᩻׎ڥѺUnityͽ΄ጭ䁰΁๗இѺѺѺ

  51. ;ͩΣͽ̵

  52. // quests;questCells΄槱ͫ͢晅͜䦒̵㺔氂͘Π List<Quest> quests = LoadQuests() int index = 0;

    foreach (Quest quest in quests) { QuestCell questCell = questCells[index++]; questCell.Bind(quest); questCell.SetClickListener(() => { OnSelectQuest(quest); }); }
  53. questCells΄ᥝᔰහΞΠquests΄ᥝᔰහ͢ग़͡͹͵䁰ݳ̵ ArgumentOutOfRangeExcep1on

  54. ͩ΄πЄϖΨ̵Zip;Tupleͽ䨗ͣ䟵͞Δͯ

  55. List<Quest> quests = LoadQuests() foreach (var tuple in quests.Zip(questCells, Tuple.Create))

    { var quest = tuple.Item1; var cell = tuple.Item2; cell.Bind(quest); cell.SetClickListener(() => { OnSelectQuest(quest); }); } αЀϔϐμφΘ΀ͥ΀͹ͼφϐκϷ ๋ڡ΁قͼ΄QuestCellΨᶋᤒᐏ΁ͯΡ㳌ቘΘ஠ᥝ
  56. ͩΩ΀ఽͮͽ 愢හ΄ϠϲЄ;愢හ΄ϔЄόΨ 奾ΉͺͧΡ䦒΁׎ڥͽͯ

  57. ϷЀμ h"ps:/ /msdn.microso/.com/library/dd267698(v=vs.110).aspx h"ps:/ /msdn.microso/.com/library/system.tuple(v=vs.110).aspx

  58. ͳ΄3 IReadOnlyList<T>

  59. // ΕΩ΀ᜉͥ䨗ͥΚͺ [SerializeField] List<Person> members;

  60. Unity΁͚͠ͼ̵System.Collec/ons.Generic.List<T>΅ᇙ㳨ͽͯ΃

  61. ͽΘList<T>ΚIList<T>΅ ͚Σ͚ΣΚ΢ͯͤΡorz

  62. Add(ᥝᔰ᭄ے);͡ Clear(ᥝᔰقڷᴻ);͡ ͚ͺΘͭ͵͚Ҙ

  63. 䋚檭̵foreachͽࢧͱͼ̵αЀϔμςͽίμψφͽͣͼ 槱ͫ͢Υ͡͹ͼ̵LINQ͢޷Ώ΢Άᜉ͚䁰ᶎ̵ग़ͥ΀͚Ҙ

  64. List<T>͚͢Σ͚ΣΚ΢ͯͤͼࢯΡֺ

  65. class Team { public List<Person> Members { get; private set;

    } /* Ӿኼ */ } --- Team team = LoadTeam(); // Κ͹ͼΑͭͥ΀͚඙֢ // क़ҁTeam΄ڥአᘏ)͡Ο΅̵͚ͩ͜͜୵ͽTeam΄Ӿ᫝Ψ඙֢ͫ΢͵ͥ΀͚ team.Members.Clear();
  66. 䌏ᒽͭΔͭΝ͜

  67. //ᴠகጱπϡЄ;͚͜䌏ᒽ class Team { private List<Person> members; public List<Person> Members

    { get { return new List<Person>(members); } } /* Ӿኼ */ } --- Team team = LoadTeam(); // Clear΅޷ΏΡͧͿ̵πϡЄͭ͵ϷφϕΨClearͭͼΡ // Team΄ٖ᮱΄members΁΅୽段΀ͭ // ͽΘTeam΄ڥአᘏ΅πЄϖ / ϖκϲϮЀϕ抎Δ΀͚;̵ͳ΢ΨᎣ΢΀͚ team.Members.Clear();
  68. ReadOnlyCollec,on<T>΄ڥአ System.Collec,ons.ObjectModel.ReadOnlyCollec,on<T>

  69. ReadOnlyCollec,on<T> Clear;͡Add;͡΀͚πϹμτϴЀμ϶φ αЀϔμς̵Count̵LINQ޷ΏΡetc Scrip&ng Run&me Versionͽ.NET 4.6(Experimetal)΁ͭ΀ͥͼΘֵ͞Ρ

  70. // ReadOnlyCollection<T>΄ڥአ class Team { private List<Person> members; public ReadOnlyCollection<Person>

    Members { get { return new ReadOnlyCollection<Person>(members); } } /* Ӿኼ */ } --- Team team = LoadTeam(); // ReadOnlyCollection<T>΅ClearϮϊϐϖ޷Ώ΀͚ // team.Members.Clear();
  71. Κ͹͵΃ѺReadOnlyCollec,on

  72. ͽΘ̵ᴠகጱπϡЄ ΗΩͿ͚ͥͫorz

  73. //̴ز΄ϮϊϐϖΨ↓Ψ void ShowEnemies(List<Enemy> enemies) // Ϯϊϐϖٖͽ䨗ͣ䟵͞΀͚ΞЄ͹ͼͩ;Ψ㬃͞Ρ͵Η void ShowEnemies(ReadOnlyCollection<Enemy> enemies) ShowEnemies΄ڥአᘏ΅䋚檭ΗΩͿ͚ͥͫ

    List<Enemy>ΨፗളΥ͵ͱͰ̵ ReadOnlyCollec,on<Enemy>΁ͭ΀͚;͚ͧ΀͚
  74. ͳͩͽ̵IReadOnlyList<T>͢ၚ攘 Scrip&ng Run&me Versionͽ.NET 4.6(Experimetal)΁ͯΡ;ֵ͞Ρ System.Collec,ons.Generic.IReadOnlyList<T>

  75. IReadOnlyList<T>αЀόЄϢδЄφ List<T>ࣳ͢ෛ͵΁䋚ᤰͭͼ͚ΡαЀόЄϢδЄφ Add;͡Clear;͡Insert;͡΀͚Ѻ ֵ͞Ρ΄΅Count̵αЀϔμς̵GetEnumeratorѺ ͫ͹ͣ΄ReadOnlyCollec,on<T>;IReadOnlyList<T>΅㳨Θ΄

  76. List<int> list = LoadList(); IReadOnlyList<int> readOnlyList = list; // readOnlyList.Add(1);

    ޷Ώ΀͚ // readOnlyList.Clear(); ޷Ώ΀͚ var count = readOnlyList.Count; foreach(var item in readOnlyList) { Debug.Log(item); }
  77. // IReadOnlyList<T>΄ڥአ class Team { private List<Person> members; public IReadOnlyList<Person>

    Members { get { return members; } } /* Ӿኼ */ } --- Team team = LoadTeam(); // IReadOnlyList<T>΅ClearϮϊϐϖ޷Ώ΀͚ // team.Members.Clear();
  78. //̴ز΄ϮϊϐϖΨ↓΅ void ShowEnemies(List<Enemy> enemies) // Ϯϊϐϖٖͽ䨗ͣ䟵͞΀͚ΞЄ͹ͼͩ;Ψ㬃͞΢ͼGood void ShowEnemies(IReadOnlyList<Enemy> enemies) ShowEnemies΄ڥአᘏ΅List<Enemy>ΘჁͱΡ

  79. Read Only ≠ Immutable

  80. IReadOnlyList<int> readOnlyList = LoadList(); if(readOnlyList is List<int>) { List<int> list

    = readOnlyList as List<int>; list.Clear(); }
  81. IEnumerable<T>͹ͼ͘ΠΔͯΞ΃

  82. IReadOnlyList<T>;IEnumerable<T>΄晅͚ ͳ΢΅̵ᥝᔰ͢嘦ਧͭͼ͚Ρͭ͡ͼ͚΀͚͡ IEnumerable<T>΅ٖ᮱㳌ቘ͢ᥝᔰίμψφ΄͵Ή΁ᩳΡݢᚆ௔͘Π

  83. List<T>΅Ӿ᫝͢䨗ͣ䟵͞Ο΢ΡϮϊϐϖ͵ͥͫΩ ϤϺϞϓΰ̵ࣳϮϊϐϖ΄୚හΚᬬΠ㮔΄ࣳ΅̵ 䁰ᶎ΁䖕ͮͼIReadOnlyList<T>ΨၚአͭΔͭΝ͜Ѻ

  84. IReadOnlyCollec-on<T>Κ IReadOnlyDic-nary<TKey, TValue>Θ

  85. ϷЀμ h"ps:/ /msdn.microso/.com/library/ms132474(v=vs.110).aspx h"ps:/ /msdn.microso/.com/library/hh192385(v=vs.110).aspx

  86. ེ΄扖氂΁ᤈͥڹ΁

  87. 櫏΁嘦扯 : ࣳ;姅ಥ;πϹμτϴЀ;ϔϷοЄϕ

  88. class Enemy { /* ኼ */} sealed class Boss :

    Enemy { /* ኼ */}
  89. Enemy enemy = new Enemy(); Boss boss = new Boss();

    Enemy bossInEnemy = new Boss(); // ͩ΢΅πЀϞαϸε϶Є // Boss enemyInBoss = new Enemy();
  90. List<Enemy> enemies = new List<Enemy>(); enemies.Add(new Enemy()); enemies.Add(new Boss()); List<Boss>

    bosses = new List<Boss>(); bosses.Add(new Boss()); // ͩ΢΅πЀϞαϸε϶Є // bosses.Add(new Enemy());
  91. List<Enemy> enemies = new List<Enemy>(); IList<Enemy> enemyList = enemies; IReadOnlyList<Enemy>

    enemyReadOnlyList = enemies; IEnumerable<Enemy> enemyEnumerable = enemies;
  92. int Square(int num){ return num * num; } --- Func<int,

    int> squareFunc = this.Square; squareFunc(5); Func<int, int> triple = (int num) => num * 3; triple(7);
  93. ϔϷοЄϕ => ϮϊϐϖΨ݇ᆙͯΡΚͺ独ͳ΄ࣳ(櫏) Ac#on<T>ΚFunc<T, TResult>΅͵ͥͫΩ΄ϔϷοЄϕ΄ࣳ΄Ӟͺ̵䷍አጱ΀Θ΄ LINQΚRx΅ϔϷοЄϕΨ୚හ΁ݐΡ̶ͩ΢Ψ֢Ρ;̵ͣͳ΄͵Η΁ϮϊϐϖΨ֢Ρ΄΅儴櫏 ϶ϭύୗΨֵ͞Ά̵αЀ϶αЀ΁㳌ቘΨ䨗͚ͼϔϷοЄϕΨ֢౮ͽͣΡ

  94. int Square(int num){ return num * num; } --- Func<int,

    int> squareFunc = this.Square; squareFunc(5); Func<int, int> triple = (int num) => num * 3; triple(7);
  95. Action<Enemy> toShow = (Enemy e) => Debug.Log(e.ToString()); toShow(new Enemy()); toShow(new

    Boss()); // ͺͤ΅πЀϞαϸε϶Є // toShow("Hello") // // toShow(3);
  96. 櫏΁嘦扯 : ࣳ;姅ಥ;πϹμτϴЀ;ϔϷοЄϕ ͭ͵;ͩΣͽ

  97. ͳ΄4 䄜௔

  98. Action<Boss>ࣳ΄䄜හ΁Action<Enemy>΄αЀφόЀφΨդفͽͣ΀͚Ѻ Action<Enemy> actionEnemy = (Enemy e) => { /* ኼ

    */ }; // .NET 3.5ͽ΅↓΅πЀϞαϸε϶Є // Action<Boss> actionBoss = actionEnemy;
  99. ͷΝ͹;ᘍ͞ͼΕΔͭΝ͜

  100. Ac#on<Enemy>;Ac#on<Boss>΅晅ࣳ͜ դفͽ΀͚΄΅̿Δ̵͘ͳ͜͡΀̀;Θ௏ͧ͜Ϳ………

  101. ͩ΢΅OKͶ͹͵΃ Enemy enemy = new Boss(); List<Enemy> enemies = new

    List<Enemy>(); enemies.Add(new Boss()); Action<Enemy> toShow = (Enemy e) => Debug.Log(e.ToString()); toShow(new Boss());
  102. ͩ΢΅OKͶ͹͵΃ // Action<Enemy>ࣳ΄䄜හ΅̵Enemyࣳ;ͳ΢Ψ姅ಥͭ͵ࣳΨ䜷͞Ρ Action<Enemy> actionEnemy = (Enemy e) => Debug.Log(e.ToString());

    actionEnemy(new Enemy()); actionEnemy(new Boss()); //Action<Boss>ࣳ΄䄜හ΅̵BossࣳΨ䜷͞Ρ Action<Boss> actionBoss = (Boss b) => Debug.Log(b.ToString()); actionBoss(new Boss());
  103. Ac#on<Boss>΄䄜හ΅̵Bossͭͩ͡΀͚(ف͹ͼͩ΀͚) Ac#on<Enemy>΄αЀφόЀφ΅̵BossΘ䜷͞Ρ(ف͹ͼͩ΢Ρ) Ac#on<Boss>΁Ac#on<Enemy>դفͽͣͼ͚͚΄ͽ΅Ҙ => ͧ΢Ϳ̵䋚檭΅晅ࣳ͜Ͷ͡Οդفͽͣ΀͚

  104. դفͽͣͼΘ͚͚ͮΙΩ Action<Enemy> actionEnemy = (Enemy e) => { /* ኼ

    */ }; // .NET 3.5ͽ΅↓΅πЀϞαϸε϶Є // Action<Boss> actionBoss = actionEnemy;
  105. Scrip&ng Run&me Versionͽ.NET 4.6(Experimetal)Ψ晝Ό; 䋚΅ͽͣΡѺ

  106. 䄜௔

  107. C# 4.0͡Ο䄜௔᭄͢ے υδϚϷμ΀αЀόЄϢδЄφ/ϔϷοЄϕ΄ ࣳ୚හ΁و䄜௔独ݍ䄜௔Ψ೮͵ͱΟ΢ΡΞ͜΁

  108. ֜͢৵͚ͭ΄?

  109. Ac#on<in T> ݍ䄜௔ΨΘ͹ͼ͚Ρ΄ͽ̵դفͽͣΡ Action<Enemy> actionEnemy = (Enemy e) => {

    /* ኼ */ }; // UnityͶ;.NET 4.6΁ͯ΢ΆOK Action<Boss> actionBoss = actionEnemy;
  110. Ac#on<in T> ݍ䄜௔ΨΘ͹ͼ͚ͼΘύϮ΀ֺ Action<Boss> actionBoss = (Boss b) => {

    /* ኼ */ }; // ͩ΢΅πЀϞαϸε϶Є // Action<Enemy> actionEnemy = actionBoss;
  111. ͺͤ΅و䄜௔

  112. و䄜௔͘͢Ρ;৵ֺ͚ͭ IReadOnlyList<T>Ψֺ΁

  113. IReadOnlyList<Boss> bossReadOnlyList = LoadBosses(); // ΘͭC#΁و䄜௔͢΀͡͹͵Ο̵ེ΅πЀϞαϸε϶Є IReadOnlyList<Enemy> enemyReadOnlyList = bossReadOnlyList;

  114. و䄜௔͘͢Ρ;৵ֺ͚ͭ IReadOnlyList<Boss> LoadBosses() { /* ኼ */ } void ShowEnemies(IReadOnlyList<Enemy>

    enemies) {/* ኼ */} ---------------------------------------- // ΘͭC#΁و䄜௔͢΀͡͹͵Ο̵ེ΅πЀϞαϸε϶Є ShowEnemies(LoadBosses());
  115. و䄜௔͘͢Ρ;̵Ϳ͚͜͜䦒΁৵͚ͭ͡㬃ΥΠΔͭ͵

  116. و䄜௔Ψ೮ͺঁΟ΄ֺ IEnumerable<Boss> bossEnumerable = LoadBosses(); IEnumerable<Enemy> enemyEnumerable = bossEnumerable; IReadOnlyList<Boss>

    bossReadOnlyList = LoadBosses(); IReadOnlyList<Enemy> enemyReadOnlyList = bossReadOnlyList; IEnumerator<Boss> bossEnumerator = LoadBosses().GetEnumerator(); IEnumerator<Enemy> enemyEnumerator = bossEnumerator;
  117. List<Boss> LoadBosses() { /* ኼ */ } --- IReadOnlyList<Enemy> bossReadOnlyList

    = LoadBosses();
  118. ̿͘Є̵͘Ρ͡Θ΃̀ ֵ̿͜䦒͘Πͳ͜Ͷ΃̀ ͹ͼ௏͜ͽͭΝҘ

  119. ̿͘΢̵IList<T>΅Ϳ͜΀΄Ҙ̀

  120. IList<T>΅̵ӧ䄜ͽͯ و䄜ͽΘݍ䄜ͽΘ΀͚ͽͯ

  121. ΘͭIList<T>͢و䄜Ͷ͹͵Ο IList<Boss> bosses = new List<Boss>(); // ๜୮΅ͽͣ΀͚ͧ΢Ϳ̵ΘͭIList<T>΁و䄜௔͘͢͹͵Ο IList<Enemy> enemies

    = bosses; // enemies΄䋚䙪΅List<Boss>̵Enemy΅Addͽͣ΀͚ enemies.Add(new Enemy());
  122. IList<T>΅̵ӧ䄜ͽͯ و䄜ͽΘݍ䄜ͽΘ΀͚ͽͯ

  123. Scrip&ng Run&me Versionͽ .NET 4.6(Experimetal)Ψ晝Ό; 䄜௔ͽαЀόЄϢδφ独ϔϷοЄϕ΄͚͚ࣳ͢ఽͮ΁

  124. ͳ΄5 αЀόЄϢδЄφ΄กᐏጱ΀䋚ᤰΨ ᇫ丆΁䖕ͮͼֵ͜͠

  125. IDragHandler void OnDrag(PointerEventData);͚͜ϮϊϐϖΨΘͺ ϖ϶ϐν䭥Ꭳ΁樛昧ͯΡUnity΄αЀόЄϢδЄφ

  126. IDragHandlerΨ́ฦ᭗΁͂䋚ᤰͯΡֺ public class DragSample : MonoBehaviour, IDragHandler { public void

    OnDrag(PointerEventData pointerData { Debug.Log ("OnDrag"); } }
  127. IDragHandlerΨ́กᐏጱ΁͂䋚ᤰͯΡֺ public class DragSample : MonoBehaviour, IDragHandler { // public͢΀ͥ΀͹ͼϮϊϐϖݷ΁

    // αЀόЄϢδЄφݷ̿IDragHandler.̀͢ͺ͚͵ void IDragHandler.OnDrag(PointerEventData pointerData) { Debug.Log ("OnDrag"); } }
  128. 晅͚΅Ҙ

  129. αЀόЄϢδЄφΨฦ᭗΁䋚ᤰͭ͵䁰ݳ // DragSampleࣳ΄䄜හͽΘOnDragϮϊϐϖ͢޷ΏΡ // dragSample΅DragSampleࣳ dragSample.OnDrag(pointerData);

  130. αЀόЄϢδЄφΨกᐏጱ΁䋚ᤰͭ͵䁰ݳ // DragSampleࣳ΄䄜හͽ΅OnDragϮϊϐϖ͢޷Ώ΀͚ // dragSample.OnDrag(pointerData);̴ૢ΅πЀϞαϸε϶Є // IDragHandlerࣳ΄䄜හ΁դفͯ΢Ά޷ΏΡ IDragHandler handler =

    dragSample; handler.OnDrag(pointerData);
  131. αЀόЄϢδЄφ΄กᐏጱ΀䋚ᤰΨͭ͵Ϯϊϐϖ΅ ͳ΄ࣳ΄䄜හ奺ኧͽ́޷Ώ΀ͥ΀͹ͼͭΔ͂͜ ͽ΅΀ͥͼ̵ ́޷Άͱ͵ͥ΀͚䁰ݳ΁̵޷Ώ΀ͥͯΡͩ;͢ͽͣΡѺ͂

  132. ֺ ReadOnlyCollec,on<T>΅ICollec,on<T>Ψ䋚ᤰͭͼ͚Ρ ICollec'on<T>.ClearΚICollec'on<T>.Add΀Ϳกᐏጱ΀䋚ᤰ ReadOnly΀ΩͶ͡Ο̵ᥝᔰ඙֢΅ͫͱ͵ͥ΀̶͚޷Άͱ͵ͥ΀̶͚ ICollec'on<T>ࣳ΀Ϳ΄䄜හ΁ف΢ͼ޷ΩͽͭΔ͹͵䁰ݳ̵NotSupportedExcep'onΨಭͨΡ

  133. ͽ΅ͫ͹ͣ΄Unity΄ֺ΁䜟͹ͼ

  134. ӥ懿΄OnDrag΅̵Unity΄UIαϦЀϕτφϓϭ͢޷Ωͽͥ΢Ρ public class DragSample : MonoBehaviour, IDragHandler { void IDragHandler.OnDrag(PointerEventData

    pointerData) { Debug.Log ("OnDrag"); } } ϪαЀϕ΅̵OnDragϮϊϐϖΨαϦЀϕτφϓϭզक़͡Ο ޷Άͱ͵͚΄͡Ϳ͜͡Ͷ;௏͜
  135. Unity΄UIαϦЀϕτφϓϭ͢޷Ωͽͥ΢Ρ ͼ͚͜͡ΖͭΣ՜͡Ο޷Άͱ͵ͥ΀̵͚ᴴਧͭ͵͚ ΀Ο̵αЀόЄϢδЄφ΄กᐏጱ΀䋚ᤰΨͭ͵ො͢Good

  136. ΘͷΣΩOnDragϮϊϐϖΨτφϓϭզक़͡Ο̵ ՜΄μ϶φ΀Ϳ͡Ο޷Ήڊͯ஠ᥝ͘͢Ρ΀ΟΆ㳨

  137. ͳ΄6 Object;Object usingεαϷίφϔΰϹμϓΰϣ

  138. Objectμ϶φ

  139. Objectμ϶φ • System.Objectμ϶φ • .NET Framework ΄قμ϶φ΄च๜μ϶φͽ̵ࣳ檦䍅΄ϸЄϕ • κЄϼЄϖ̿object̵̀ͽμ϶φݷΨᤒͱΡ

  140. Objectμ϶φ

  141. Objectμ϶φ • UnityEngine.Objectμ϶φ • Unityͽ᯿ᥝ΀GameObjectΚComponent΄憭μ϶φ • ΀ΩͽͳΩ΀ݷڹͺͧͷΙ͹͵΄ΔͮͽѺҘ

  142. Debug.Log;͚͜ϺνΨݺͥϮϊϐϖ public static void Log(object message, Object context); ͫЄ̵Ϳ͹ͷ͢Ϳ͹ͷͶҘ

  143. Debug.Log;͚͜ϺνΨݺͥϮϊϐϖ public static void Log(object message, Object context); ᒫӞ୚හ͢System.Objectͽ̵ᒫԫ୚හ͢UnityEngine.Objectͽ 奎ΟΥ͚ͭ͹͵Ο͘ΠΙͭ΀͚

  144. ͩͩͽusingεαϷίφϔΰϹμϓΰϣΨֵ͹ͼΕΡ

  145. usingεαϷίφϔΰϹμϓΰϣ ݷڹᑮ樌Δ͵΅ࣳ΄εαϷίφΨ֢౮ͽͣΡ䱛ᚆ

  146. using UnityEngine; using System; using UnityObject = UnityEngine.Object; // <-

    usingεαϷίφϔΰϹμϓΰϣ public class Sample : MonoBehaviour{ void Start () { // ӥ懿΅UnityEngine.Object UnityObject unityEngineObject; // ӥ懿΅System.Objectࣳ object systemObject; // ྜஷ΀͢Ο̵Object΅䨀ซ΀ΔΔͽπЀϞαϸε϶Є // Object ambiguousObject; } }
  147. ͳ΄7 FindObjectOfType<T> ࣳϞ϶ϮЄό΄ګ夹

  148. UnityEngine.Object.FindObjectOfType<T>Ϯϊϐϖ ࣳΨ೰ਧͭͼ̵οЄϭτЄЀӾ΄ͳ΄ࣳ΄ηϣυδϕΨݐ஑ͽͣΡ // GameManager΅ᛔ֢΄μ϶φͽ̵MonoBehaviourΨ姅ಥͭͼ͚Ρ GameManager gameManager = FindObjectOfType<GameManager> (); gameManager.StartGameLoop

    (); GameManagerզक़΄μ϶φ͡ΟͽΘ̵GameManager΄ηϣυδμϕΨݐ஑ͽͣΡ
  149. ེ΄πЄϖ΅ͯΏͼπЀϞαϸε϶Є string str = FindObjectOfType<string> (); // MyClass΅ᛔ֢μ϶φ̵UnityEngine.ObjectΨ姅ಥͭͼ͚΀͚ MyClass myClass

    = FindObjectOfType<MyClass> (); // ISample΅ᛔ֢αЀόЄϢδЄφ ISample sample = FindObjectOfType<ISample> ();
  150. ΀Ͳ͡Ҙ

  151. FindObjectOfType<T>΅ UnityEngine.Objectࣳ΁ࣳϞ϶ϮЄό΄ګ夹͢͡͡͹ͼ͚Ρ͵Η T΅UnityEngine.Object͡ͳ΄ςϣμ϶φͽ΀͚;͚ͧ΀͚ ࣳϞ϶ϮЄό΄ګ夹Ψֵ͜;υδϚϷϐμ΀μ϶φΚϮϊϐϖ΁̵ ࣳ୚හ΁೰ਧ独ֵአͽͣΡࣳ΄圵气΁ګᴴΨے͞Ρͩ;͢ͽͣΡ

  152. ӥ懿΅πЀϞαϸε϶Є FindObjectsOfType<T>ΘUnityEngine.ObjectͽࣳϞ϶ϮЄό΄ګ夹ͫ͢΢ͼ͚Ρ ͩ΢Ͷ;T΁΅̵UnityEngine.Object独ͳ΄ςϣμ϶φզक़͢๶Ρݢᚆ௔͘͢Ρ public class Utility { // TࣳͽοЄϭηϣυδμϕ΄ݷڹ͢name΀Θ΄Ψݐ஑ public

    static T[] FindObjectsOfType<T> (string name) { return Object.FindObjectsOfType<T> () .Where (it => it.name == name) .ToArray (); } }
  153. ӥ懿΅OK UnityEngine.ObjectͽࣳϞ϶ϮЄό΄ګ夹ͫ͢΢ͼ͚Ρ΄ͽ T΁΅̵UnityEngine.Object͡ͳ΄ςϣμ϶φͭͩ͡΀͚ public class Utility { // TࣳͽοЄϭηϣυδμϕ΄ݷڹ͢name΀Θ΄Ψݐ஑ public

    static T[] FindObjectsOfType<T> (string name) where T : UnityEngine.Object { return Object.FindObjectsOfType<T> () .Where (it => it.name == name) .ToArray (); } }
  154. ͳ΄8 ᄍᓒৼ΄ηЄϝЄϺЄϖ ࣳ䄜䟵ᄍᓒৼ

  155. ᄍᓒৼ΄ηЄϝЄϺЄϖ ϳЄσЄਧ嬝ࣳͽΘ+Κ-΀Ϳ΄ᄍᓒৼֵ͢͞ΡΞ͜΁΀ΡΚͺ

  156. ᄍᓒৼ΄ηЄϝЄϺЄϖ;͚͞Ά DateTimeࣳ;TimeSpanࣳ΄+;-ηϧϹЄόͽͯ DateTime dateTime = DateTime.Now + TimeSpan.FromSeconds (30.0); TimeSpan

    duration = DateTime.Now - GetStartedTime ();
  157. UnityͽΘय़ၚ攘

  158. ֖ᗝᒵΨ͘ΟΥͯVector3Κࢧ敢ΨᤒͯQuaternionͽ ᄍᓒৼ΄ηЄϝЄϺЄϖ͢ਧ嬝ͫ΢ͼ͚Ρ // Quaternion΅ࢥزහ̶ࢧ敢独঵㵟ᒵͽֵ͜ Quaternion rotation = Quaternion.Euler (0, 0,

    90.0F); Vector3 vectorA = new Vector3 (1.0F, 1.0F, 0.0F); Vector3 vectorB = new Vector3 (1.0F, -1.0F, 0.0F); Vector3 addedVector = vectorA + vectorB; Vector3 scaledVector = 2.0F * vectorA; Vector3 rotatedVector = rotation * vectorB;
  159. ࣳ䄜䟵ᄍᓒৼ กᐏጱ΁ࣳΨ೰ਧͯ΢Άࣳ䄜䟵Ψᤈ͞Ρกᐏጱࣳ䄜䟵; ࣳΨ೰ਧͭ΀ͥͼΘ஠ᥝ΁΀͹͵Ο䄜䟵ͭͼͥ΢Ρิ焥ጱࣳ䄜䟵

  160. ࣳ䄜䟵ᄍᓒৼ;͚͞Ά System.DecimalࣳͽͯΞ΃Ҙ // ग़ͥ΄හ㮔ࣳ͡Οิ焥ጱࣳ䄜䟵 decimal num1 = 1; decimal num2

    = 2.0F; decimal num3 = 3.0; // ग़ͥ΄හ㮔ࣳΎ΄กᐏጱࣳ䄜䟵 int numA = (int)num1; float numB = (float)num2; double numC = (double)num3;
  161. UnityͽΘ奾䯤ֵΥ΢ͼ͚Ρ

  162. Vector3䯤᭜֛͡ΟVector2䯤᭜֛Ύ΄ิ焥ጱࣳ䄜䟵 // zଷ䰤΅僻憙ͫ΢Ρ Vector2 vector2d = new Vector3 (1.0F, 1.0F,

    0.0F); // zଷ䰤΅0.0F͢䞜فͫ΢Ρ Vector3 vector3d = new Vector2 (1.0F, 1.0F);
  163. 㰜΅̵ͩ΢২͚ͽ̶ͯ 奾䯤ԪඳΠΔͱΩ͡Ҙ

  164. UnityEngine.Object͡ΟboolΎ΄ࣳ䄜䟵 // Rigidbody΅姅ಥ檦䍅Ψ昀Ρ;UnityEngine.Object΁͵ͿΠ፳ͥ Rigidbody rigidbody = GetComponent<Rigidbody>(); // ӥ懿΅if (rigidbody

    != null) { ;䋚搡ݶͮΟ͚ͭ if (rigidbody) { rigidbody.AddForce(10.0F * Vector3.forward); }
  165. Α;ΩͿ΄ϤϺν϶ϫ΁;͹ͼ ̿ϤϺν϶ϬЀν᥺承΅ٍ᭲̀ 㰜΅ͳ͜௏͹ͼ͚Δ̶ͯ

  166. ͠܅ᘏͫΩ͢܅咅࢏ٍ΄ֵ͚ො̵͘ΔΠᎣΟ΀͡͹͵Ο͚ΚͽͯΞ΃Ҙ य़ૡͫΩ͢ૡٍ΄ֵ͚ො̵樌晅͹ͼ͚͵Ο͚ΚͽͯΞ΃Ҙ ෆ㯪ॊͫΩٍ᭲͢΄ֵ͚ො̵晒୮΀汜ᤈ䱛΁㪥Π͵ͥ΀͚ͽͯΞ΃Ҙ

  167. ٍ̿᭲̀Ψ΀͚ͭ͢Σ΁ͯΡϤϺ מ毲ͽͣ΀͚ͽͯΞ΃Ҙ

  168. ̿ϤϺν϶ϬЀν᥺承΅ٍ᭲̀Ͷ͡Οͩͳ̵ ϤϺ;ͭͼ晒ڔ΁ٍ᭲ΨᎣΠ̵ֵ͚ͩ΀ͯΏͣͶ;௏͚Δͯ

  169. Ք෭͠扖ͭͭ͵ٖ਻΅̵䋚᪢ጱ΀Θ΄ͽ΅͘ΠΔͱΩͽͭ͵̶ ͭͭ͡Ε΀ͫΩ΄C#΄ᎣΟ΀͡͹͵᥺承䱛ᚆΨ ͠㬃͞ͽͣ͵΄ͽ΅΀͚ͽͭΝ͜͡Ҙ

  170. ͭ͹͡ΠC#̵UnityΚ͹ͼ͚ͣΔͭΝ͜

  171. ̿Ք̵UnityϤϺν϶ϫ΁ර͞ͼӤͨ͵͚C#΄͘΢Κͩ΢Κ̀ @RyotaMurohoshi 2017/11/26(ᰂ) ϤϺν϶ϫ΄͵Η΄Unityۣ䔶տ