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

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

RyotaMurohoshi
November 25, 2017

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

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

RyotaMurohoshi

November 25, 2017
Tweet

More Decks by RyotaMurohoshi

Other Decks in Technology

Transcript

  1. 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(); }
  2. 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(); }
  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. 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(); }
  5. // 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); }); }
  6. // ץྋғ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); }); }
  7. 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)
  8. // 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); }); }
  9. 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Ψᶋᤒᐏ΁ͯΡ㳌ቘΘ஠ᥝ
  10. class Team { public List<Person> Members { get; private set;

    } /* Ӿኼ */ } --- Team team = LoadTeam(); // Κ͹ͼΑͭͥ΀͚඙֢ // क़ҁTeam΄ڥአᘏ)͡Ο΅̵͚ͩ͜͜୵ͽTeam΄Ӿ᫝Ψ඙֢ͫ΢͵ͥ΀͚ team.Members.Clear();
  11. //ᴠகጱπϡЄ;͚͜䌏ᒽ 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();
  12. // 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();
  13. 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); }
  14. // IReadOnlyList<T>΄ڥአ class Team { private List<Person> members; public IReadOnlyList<Person>

    Members { get { return members; } } /* Ӿኼ */ } --- Team team = LoadTeam(); // IReadOnlyList<T>΅ClearϮϊϐϖ޷Ώ΀͚ // team.Members.Clear();
  15. Enemy enemy = new Enemy(); Boss boss = new Boss();

    Enemy bossInEnemy = new Boss(); // ͩ΢΅πЀϞαϸε϶Є // Boss enemyInBoss = new Enemy();
  16. 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());
  17. List<Enemy> enemies = new List<Enemy>(); IList<Enemy> enemyList = enemies; IReadOnlyList<Enemy>

    enemyReadOnlyList = enemies; IEnumerable<Enemy> enemyEnumerable = enemies;
  18. 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);
  19. 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);
  20. Action<Enemy> toShow = (Enemy e) => Debug.Log(e.ToString()); toShow(new Enemy()); toShow(new

    Boss()); // ͺͤ΅πЀϞαϸε϶Є // toShow("Hello") // // toShow(3);
  21. Action<Boss>ࣳ΄䄜හ΁Action<Enemy>΄αЀφόЀφΨդفͽͣ΀͚Ѻ Action<Enemy> actionEnemy = (Enemy e) => { /* ኼ

    */ }; // .NET 3.5ͽ΅↓΅πЀϞαϸε϶Є // Action<Boss> actionBoss = actionEnemy;
  22. ͩ΢΅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());
  23. ͩ΢΅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());
  24. դفͽͣͼΘ͚͚ͮΙΩ Action<Enemy> actionEnemy = (Enemy e) => { /* ኼ

    */ }; // .NET 3.5ͽ΅↓΅πЀϞαϸε϶Є // Action<Boss> actionBoss = actionEnemy;
  25. Ac#on<in T> ݍ䄜௔ΨΘ͹ͼ͚Ρ΄ͽ̵դفͽͣΡ Action<Enemy> actionEnemy = (Enemy e) => {

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

    /* ኼ */ }; // ͩ΢΅πЀϞαϸε϶Є // Action<Enemy> actionEnemy = actionBoss;
  27. و䄜௔͘͢Ρ;৵ֺ͚ͭ IReadOnlyList<Boss> LoadBosses() { /* ኼ */ } void ShowEnemies(IReadOnlyList<Enemy>

    enemies) {/* ኼ */} ---------------------------------------- // ΘͭC#΁و䄜௔͢΀͡͹͵Ο̵ེ΅πЀϞαϸε϶Є ShowEnemies(LoadBosses());
  28. و䄜௔Ψ೮ͺঁΟ΄ֺ 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;
  29. IDragHandlerΨ́กᐏጱ΁͂䋚ᤰͯΡֺ public class DragSample : MonoBehaviour, IDragHandler { // public͢΀ͥ΀͹ͼϮϊϐϖݷ΁

    // αЀόЄϢδЄφݷ̿IDragHandler.̀͢ͺ͚͵ void IDragHandler.OnDrag(PointerEventData pointerData) { Debug.Log ("OnDrag"); } }
  30. ӥ懿΄OnDrag΅̵Unity΄UIαϦЀϕτφϓϭ͢޷Ωͽͥ΢Ρ public class DragSample : MonoBehaviour, IDragHandler { void IDragHandler.OnDrag(PointerEventData

    pointerData) { Debug.Log ("OnDrag"); } } ϪαЀϕ΅̵OnDragϮϊϐϖΨαϦЀϕτφϓϭզक़͡Ο ޷Άͱ͵͚΄͡Ϳ͜͡Ͷ;௏͜
  31. 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; } }
  32. ེ΄πЄϖ΅ͯΏͼπЀϞαϸε϶Є string str = FindObjectOfType<string> (); // MyClass΅ᛔ֢μ϶φ̵UnityEngine.ObjectΨ姅ಥͭͼ͚΀͚ MyClass myClass

    = FindObjectOfType<MyClass> (); // ISample΅ᛔ֢αЀόЄϢδЄφ ISample sample = FindObjectOfType<ISample> ();
  33. ӥ懿΅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 (); } }
  34. ֖ᗝᒵΨ͘ΟΥͯ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;
  35. ࣳ䄜䟵ᄍᓒৼ;͚͞Ά 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;
  36. Vector3䯤᭜֛͡ΟVector2䯤᭜֛Ύ΄ิ焥ጱࣳ䄜䟵 // zଷ䰤΅僻憙ͫ΢Ρ Vector2 vector2d = new Vector3 (1.0F, 1.0F,

    0.0F); // zଷ䰤΅0.0F͢䞜فͫ΢Ρ Vector3 vector3d = new Vector2 (1.0F, 1.0F);