Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

㺔氂ͽͯ

Slide 3

Slide 3 text

var names = new List { "ΊΞΊΞ", "ΥΩΥΩ", "΁ΙЄΩ" }; var actions = new List(); foreach (string name in names) { actions.Add(() => { Debug.Log(name); }); } foreach(var action in actions) { action(); }

Slide 4

Slide 4 text

΀Ω;ᤒᐏͫ΢ΡͽͭΝ͜͡Ҙ

Slide 5

Slide 5 text

ΊΞΊΞ ΥΩΥΩ ΁ΙЄΩ

Slide 6

Slide 6 text

Ͷ;௏͜ͽͭΝ͜Ҙ

Slide 7

Slide 7 text

΁ΙЄΩ ΁ΙЄΩ ΁ΙЄΩ

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

ͳ΄ڹ΁ᛔ૩奧Օ

Slide 12

Slide 12 text

@RyotaMurohoshi

Slide 13

Slide 13 text

Ք෭扖ͯͩ;

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

ᙧว

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

var names = new List { "ΊΞΊΞ", "ΥΩΥΩ", "΁ΙЄΩ" }; var actions = new List(); foreach (string name in names) { actions.Add(() => { Debug.Log(name); }); } foreach(var action in actions) { action(); }

Slide 28

Slide 28 text

C# 4.0 -> 5.0ͽ՛䯭͢䄜ΥΠΔͭ͵

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

var names = new List { "ΊΞΊΞ", "ΥΩΥΩ", "΁ΙЄΩ" }; var actions = new List(); foreach (string name in names) { actions.Add(() => { Debug.Log(name); }); } foreach(var action in actions) { action(); }

Slide 33

Slide 33 text

foreach෈΄ٖ᮱՛䯭͢ C# 4.0 -> 5.0ͽ͡ΥΠΔͭ͵

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

var names = new List { "ΊΞΊΞ", "ΥΩΥΩ", "΁ΙЄΩ" }; var actions = new List(); foreach (string name in names) { actions.Add(() => { Debug.Log(name); }); } foreach(var action in actions) { action(); }

Slide 37

Slide 37 text

Unity5.4/C# 4.0զӥͽ΅̵ ΁ΙЄΩ ΁ΙЄΩ ΁ΙЄΩ

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

// Quest΄ϷφϕΨQuestCell΄Ϸφϕ΁ϝαЀϖ // Unity 5.4զӥͽ̵ࢯΠͳ͜΀πЄϖֺ // Ϳ΄ψϸ͢μϷϐμͫ΢͵䦒Θ̵๋஍΄quests΄ᥝᔰͽOnSelectQuest List quests = LoadQuests() int index = 0; foreach (Quest quest in quests) { QuestCell questCell = questCells[index++]; questCell.Bind(quest); questCell.SetClickListener(() => { OnSelectQuest(quest); }); }

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

ݘ͚UnityΨֵ͹ͼ͚ΡՈΚ̵ ݘ͚UnityΘ䌏᨝΄϶αϣ϶Ϸ樄咲ᘏ΄Ո ᥝဳ఺

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

List strings = new List { "A", "B", "C" }; List ints = new List { 3, 1, 4, 1, 5 }; IEnumerable> zipped = strings.Zip(ints, (s, i) => Tuple.Create(s, i)); foreach(Tuple tuple in zipped) { Debug.Log(tuple); } // (A, 3) // (B, 1) // (C, 4)

Slide 50

Slide 50 text

System.Tuple΅ͳͩΔͽ׎ڥͮΙ΀͚ Item1;͡Item2͹ͼݷڹ̵Υ͡ΠͻΟ͚ C# 7.0ͽ΄όϤϸ᩻׎ڥѺUnityͽ΄ጭ䁰΁๗இѺѺѺ

Slide 51

Slide 51 text

;ͩΣͽ̵

Slide 52

Slide 52 text

// quests;questCells΄槱ͫ͢晅͜䦒̵㺔氂͘Π List quests = LoadQuests() int index = 0; foreach (Quest quest in quests) { QuestCell questCell = questCells[index++]; questCell.Bind(quest); questCell.SetClickListener(() => { OnSelectQuest(quest); }); }

Slide 53

Slide 53 text

questCells΄ᥝᔰහΞΠquests΄ᥝᔰහ͢ग़͡͹͵䁰ݳ̵ ArgumentOutOfRangeExcep1on

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

List 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Ψᶋᤒᐏ΁ͯΡ㳌ቘΘ஠ᥝ

Slide 56

Slide 56 text

ͩΩ΀ఽͮͽ 愢හ΄ϠϲЄ;愢හ΄ϔЄόΨ 奾ΉͺͧΡ䦒΁׎ڥͽͯ

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

ͳ΄3 IReadOnlyList

Slide 59

Slide 59 text

// ΕΩ΀ᜉͥ䨗ͥΚͺ [SerializeField] List members;

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

ͽΘListΚIList΅ ͚Σ͚ΣΚ΢ͯͤΡorz

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

List͚͢Σ͚ΣΚ΢ͯͤͼࢯΡֺ

Slide 65

Slide 65 text

class Team { public List Members { get; private set; } /* Ӿኼ */ } --- Team team = LoadTeam(); // Κ͹ͼΑͭͥ΀͚඙֢ // क़ҁTeam΄ڥአᘏ)͡Ο΅̵͚ͩ͜͜୵ͽTeam΄Ӿ᫝Ψ඙֢ͫ΢͵ͥ΀͚ team.Members.Clear();

Slide 66

Slide 66 text

䌏ᒽͭΔͭΝ͜

Slide 67

Slide 67 text

//ᴠகጱπϡЄ;͚͜䌏ᒽ class Team { private List members; public List Members { get { return new List(members); } } /* Ӿኼ */ } --- Team team = LoadTeam(); // Clear΅޷ΏΡͧͿ̵πϡЄͭ͵ϷφϕΨClearͭͼΡ // Team΄ٖ᮱΄members΁΅୽段΀ͭ // ͽΘTeam΄ڥአᘏ΅πЄϖ / ϖκϲϮЀϕ抎Δ΀͚;̵ͳ΢ΨᎣ΢΀͚ team.Members.Clear();

Slide 68

Slide 68 text

ReadOnlyCollec,on΄ڥአ System.Collec,ons.ObjectModel.ReadOnlyCollec,on

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

// ReadOnlyCollection΄ڥአ class Team { private List members; public ReadOnlyCollection Members { get { return new ReadOnlyCollection(members); } } /* Ӿኼ */ } --- Team team = LoadTeam(); // ReadOnlyCollection΅ClearϮϊϐϖ޷Ώ΀͚ // team.Members.Clear();

Slide 71

Slide 71 text

Κ͹͵΃ѺReadOnlyCollec,on

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

//̴ز΄ϮϊϐϖΨ↓Ψ void ShowEnemies(List enemies) // Ϯϊϐϖٖͽ䨗ͣ䟵͞΀͚ΞЄ͹ͼͩ;Ψ㬃͞Ρ͵Η void ShowEnemies(ReadOnlyCollection enemies) ShowEnemies΄ڥአᘏ΅䋚檭ΗΩͿ͚ͥͫ ListΨፗളΥ͵ͱͰ̵ ReadOnlyCollec,on΁ͭ΀͚;͚ͧ΀͚

Slide 74

Slide 74 text

ͳͩͽ̵IReadOnlyList͢ၚ攘 Scrip&ng Run&me Versionͽ.NET 4.6(Experimetal)΁ͯΡ;ֵ͞Ρ System.Collec,ons.Generic.IReadOnlyList

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

List list = LoadList(); IReadOnlyList readOnlyList = list; // readOnlyList.Add(1); ޷Ώ΀͚ // readOnlyList.Clear(); ޷Ώ΀͚ var count = readOnlyList.Count; foreach(var item in readOnlyList) { Debug.Log(item); }

Slide 77

Slide 77 text

// IReadOnlyList΄ڥአ class Team { private List members; public IReadOnlyList Members { get { return members; } } /* Ӿኼ */ } --- Team team = LoadTeam(); // IReadOnlyList΅ClearϮϊϐϖ޷Ώ΀͚ // team.Members.Clear();

Slide 78

Slide 78 text

//̴ز΄ϮϊϐϖΨ↓΅ void ShowEnemies(List enemies) // Ϯϊϐϖٖͽ䨗ͣ䟵͞΀͚ΞЄ͹ͼͩ;Ψ㬃͞΢ͼGood void ShowEnemies(IReadOnlyList enemies) ShowEnemies΄ڥአᘏ΅ListΘჁͱΡ

Slide 79

Slide 79 text

Read Only ≠ Immutable

Slide 80

Slide 80 text

IReadOnlyList readOnlyList = LoadList(); if(readOnlyList is List) { List list = readOnlyList as List; list.Clear(); }

Slide 81

Slide 81 text

IEnumerable͹ͼ͘ΠΔͯΞ΃

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

IReadOnlyCollec-onΚ IReadOnlyDic-naryΘ

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

ེ΄扖氂΁ᤈͥڹ΁

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

Enemy enemy = new Enemy(); Boss boss = new Boss(); Enemy bossInEnemy = new Boss(); // ͩ΢΅πЀϞαϸε϶Є // Boss enemyInBoss = new Enemy();

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

List enemies = new List(); IList enemyList = enemies; IReadOnlyList enemyReadOnlyList = enemies; IEnumerable enemyEnumerable = enemies;

Slide 92

Slide 92 text

int Square(int num){ return num * num; } --- Func squareFunc = this.Square; squareFunc(5); Func triple = (int num) => num * 3; triple(7);

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

int Square(int num){ return num * num; } --- Func squareFunc = this.Square; squareFunc(5); Func triple = (int num) => num * 3; triple(7);

Slide 95

Slide 95 text

Action toShow = (Enemy e) => Debug.Log(e.ToString()); toShow(new Enemy()); toShow(new Boss()); // ͺͤ΅πЀϞαϸε϶Є // toShow("Hello") // // toShow(3);

Slide 96

Slide 96 text

櫏΁嘦扯 : ࣳ;姅ಥ;πϹμτϴЀ;ϔϷοЄϕ ͭ͵;ͩΣͽ

Slide 97

Slide 97 text

ͳ΄4 䄜௔

Slide 98

Slide 98 text

Actionࣳ΄䄜හ΁Action΄αЀφόЀφΨդفͽͣ΀͚Ѻ Action actionEnemy = (Enemy e) => { /* ኼ */ }; // .NET 3.5ͽ΅↓΅πЀϞαϸε϶Є // Action actionBoss = actionEnemy;

Slide 99

Slide 99 text

ͷΝ͹;ᘍ͞ͼΕΔͭΝ͜

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

ͩ΢΅OKͶ͹͵΃ Enemy enemy = new Boss(); List enemies = new List(); enemies.Add(new Boss()); Action toShow = (Enemy e) => Debug.Log(e.ToString()); toShow(new Boss());

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

Ac#on΄䄜හ΅̵Bossͭͩ͡΀͚(ف͹ͼͩ΀͚) Ac#on΄αЀφόЀφ΅̵BossΘ䜷͞Ρ(ف͹ͼͩ΢Ρ) Ac#on΁Ac#onդفͽͣͼ͚͚΄ͽ΅Ҙ => ͧ΢Ϳ̵䋚檭΅晅ࣳ͜Ͷ͡Οդفͽͣ΀͚

Slide 104

Slide 104 text

դفͽͣͼΘ͚͚ͮΙΩ Action actionEnemy = (Enemy e) => { /* ኼ */ }; // .NET 3.5ͽ΅↓΅πЀϞαϸε϶Є // Action actionBoss = actionEnemy;

Slide 105

Slide 105 text

Scrip&ng Run&me Versionͽ.NET 4.6(Experimetal)Ψ晝Ό; 䋚΅ͽͣΡѺ

Slide 106

Slide 106 text

䄜௔

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

֜͢৵͚ͭ΄?

Slide 109

Slide 109 text

Ac#on ݍ䄜௔ΨΘ͹ͼ͚Ρ΄ͽ̵դفͽͣΡ Action actionEnemy = (Enemy e) => { /* ኼ */ }; // UnityͶ;.NET 4.6΁ͯ΢ΆOK Action actionBoss = actionEnemy;

Slide 110

Slide 110 text

Ac#on ݍ䄜௔ΨΘ͹ͼ͚ͼΘύϮ΀ֺ Action actionBoss = (Boss b) => { /* ኼ */ }; // ͩ΢΅πЀϞαϸε϶Є // Action actionEnemy = actionBoss;

Slide 111

Slide 111 text

ͺͤ΅و䄜௔

Slide 112

Slide 112 text

و䄜௔͘͢Ρ;৵ֺ͚ͭ IReadOnlyListΨֺ΁

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

و䄜௔͘͢Ρ;৵ֺ͚ͭ IReadOnlyList LoadBosses() { /* ኼ */ } void ShowEnemies(IReadOnlyList enemies) {/* ኼ */} ---------------------------------------- // ΘͭC#΁و䄜௔͢΀͡͹͵Ο̵ེ΅πЀϞαϸε϶Є ShowEnemies(LoadBosses());

Slide 115

Slide 115 text

و䄜௔͘͢Ρ;̵Ϳ͚͜͜䦒΁৵͚ͭ͡㬃ΥΠΔͭ͵

Slide 116

Slide 116 text

و䄜௔Ψ೮ͺঁΟ΄ֺ IEnumerable bossEnumerable = LoadBosses(); IEnumerable enemyEnumerable = bossEnumerable; IReadOnlyList bossReadOnlyList = LoadBosses(); IReadOnlyList enemyReadOnlyList = bossReadOnlyList; IEnumerator bossEnumerator = LoadBosses().GetEnumerator(); IEnumerator enemyEnumerator = bossEnumerator;

Slide 117

Slide 117 text

List LoadBosses() { /* ኼ */ } --- IReadOnlyList bossReadOnlyList = LoadBosses();

Slide 118

Slide 118 text

̿͘Є̵͘Ρ͡Θ΃̀ ֵ̿͜䦒͘Πͳ͜Ͷ΃̀ ͹ͼ௏͜ͽͭΝҘ

Slide 119

Slide 119 text

̿͘΢̵IList΅Ϳ͜΀΄Ҙ̀

Slide 120

Slide 120 text

IList΅̵ӧ䄜ͽͯ و䄜ͽΘݍ䄜ͽΘ΀͚ͽͯ

Slide 121

Slide 121 text

ΘͭIList͢و䄜Ͷ͹͵Ο IList bosses = new List(); // ๜୮΅ͽͣ΀͚ͧ΢Ϳ̵ΘͭIList΁و䄜௔͘͢͹͵Ο IList enemies = bosses; // enemies΄䋚䙪΅List̵Enemy΅Addͽͣ΀͚ enemies.Add(new Enemy());

Slide 122

Slide 122 text

IList΅̵ӧ䄜ͽͯ و䄜ͽΘݍ䄜ͽΘ΀͚ͽͯ

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

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

Slide 125

Slide 125 text

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

Slide 126

Slide 126 text

IDragHandlerΨ́ฦ᭗΁͂䋚ᤰͯΡֺ public class DragSample : MonoBehaviour, IDragHandler { public void OnDrag(PointerEventData pointerData { Debug.Log ("OnDrag"); } }

Slide 127

Slide 127 text

IDragHandlerΨ́กᐏጱ΁͂䋚ᤰͯΡֺ public class DragSample : MonoBehaviour, IDragHandler { // public͢΀ͥ΀͹ͼϮϊϐϖݷ΁ // αЀόЄϢδЄφݷ̿IDragHandler.̀͢ͺ͚͵ void IDragHandler.OnDrag(PointerEventData pointerData) { Debug.Log ("OnDrag"); } }

Slide 128

Slide 128 text

晅͚΅Ҙ

Slide 129

Slide 129 text

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

Slide 130

Slide 130 text

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

Slide 131

Slide 131 text

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

Slide 132

Slide 132 text

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

Slide 133

Slide 133 text

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

Slide 134

Slide 134 text

ӥ懿΄OnDrag΅̵Unity΄UIαϦЀϕτφϓϭ͢޷Ωͽͥ΢Ρ public class DragSample : MonoBehaviour, IDragHandler { void IDragHandler.OnDrag(PointerEventData pointerData) { Debug.Log ("OnDrag"); } } ϪαЀϕ΅̵OnDragϮϊϐϖΨαϦЀϕτφϓϭզक़͡Ο ޷Άͱ͵͚΄͡Ϳ͜͡Ͷ;௏͜

Slide 135

Slide 135 text

Unity΄UIαϦЀϕτφϓϭ͢޷Ωͽͥ΢Ρ ͼ͚͜͡ΖͭΣ՜͡Ο޷Άͱ͵ͥ΀̵͚ᴴਧͭ͵͚ ΀Ο̵αЀόЄϢδЄφ΄กᐏጱ΀䋚ᤰΨͭ͵ො͢Good

Slide 136

Slide 136 text

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

Slide 137

Slide 137 text

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

Slide 138

Slide 138 text

Objectμ϶φ

Slide 139

Slide 139 text

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

Slide 140

Slide 140 text

Objectμ϶φ

Slide 141

Slide 141 text

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

Slide 142

Slide 142 text

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

Slide 143

Slide 143 text

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

Slide 144

Slide 144 text

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

Slide 145

Slide 145 text

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

Slide 146

Slide 146 text

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; } }

Slide 147

Slide 147 text

ͳ΄7 FindObjectOfType ࣳϞ϶ϮЄό΄ګ夹

Slide 148

Slide 148 text

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

Slide 149

Slide 149 text

ེ΄πЄϖ΅ͯΏͼπЀϞαϸε϶Є string str = FindObjectOfType (); // MyClass΅ᛔ֢μ϶φ̵UnityEngine.ObjectΨ姅ಥͭͼ͚΀͚ MyClass myClass = FindObjectOfType (); // ISample΅ᛔ֢αЀόЄϢδЄφ ISample sample = FindObjectOfType ();

Slide 150

Slide 150 text

΀Ͳ͡Ҙ

Slide 151

Slide 151 text

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

Slide 152

Slide 152 text

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

Slide 153

Slide 153 text

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

Slide 154

Slide 154 text

ͳ΄8 ᄍᓒৼ΄ηЄϝЄϺЄϖ ࣳ䄜䟵ᄍᓒৼ

Slide 155

Slide 155 text

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

Slide 156

Slide 156 text

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

Slide 157

Slide 157 text

UnityͽΘय़ၚ攘

Slide 158

Slide 158 text

֖ᗝᒵΨ͘ΟΥͯ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;

Slide 159

Slide 159 text

ࣳ䄜䟵ᄍᓒৼ กᐏጱ΁ࣳΨ೰ਧͯ΢Άࣳ䄜䟵Ψᤈ͞Ρกᐏጱࣳ䄜䟵; ࣳΨ೰ਧͭ΀ͥͼΘ஠ᥝ΁΀͹͵Ο䄜䟵ͭͼͥ΢Ρิ焥ጱࣳ䄜䟵

Slide 160

Slide 160 text

ࣳ䄜䟵ᄍᓒৼ;͚͞Ά 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;

Slide 161

Slide 161 text

UnityͽΘ奾䯤ֵΥ΢ͼ͚Ρ

Slide 162

Slide 162 text

Vector3䯤᭜֛͡ΟVector2䯤᭜֛Ύ΄ิ焥ጱࣳ䄜䟵 // zଷ䰤΅僻憙ͫ΢Ρ Vector2 vector2d = new Vector3 (1.0F, 1.0F, 0.0F); // zଷ䰤΅0.0F͢䞜فͫ΢Ρ Vector3 vector3d = new Vector2 (1.0F, 1.0F);

Slide 163

Slide 163 text

㰜΅̵ͩ΢২͚ͽ̶ͯ 奾䯤ԪඳΠΔͱΩ͡Ҙ

Slide 164

Slide 164 text

UnityEngine.Object͡ΟboolΎ΄ࣳ䄜䟵 // Rigidbody΅姅ಥ檦䍅Ψ昀Ρ;UnityEngine.Object΁͵ͿΠ፳ͥ Rigidbody rigidbody = GetComponent(); // ӥ懿΅if (rigidbody != null) { ;䋚搡ݶͮΟ͚ͭ if (rigidbody) { rigidbody.AddForce(10.0F * Vector3.forward); }

Slide 165

Slide 165 text

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

Slide 166

Slide 166 text

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

Slide 167

Slide 167 text

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

Slide 168

Slide 168 text

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

Slide 169

Slide 169 text

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

Slide 170

Slide 170 text

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

Slide 171

Slide 171 text

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