Vector2Intの互換ライブラリが作りたかった話!

 Vector2Intの互換ライブラリが作りたかった話!

2017/12/09 (土) 【年末だよ】Unity お・と・な のLT大会 2017【ポロりしてもいいのよ】

の @RyotaMurohoshi の資料です。

F46c97bb74758d481531990654933050?s=128

RyotaMurohoshi

December 09, 2017
Tweet

Transcript

  1. Vector2Int΄԰䟵϶αϣ϶Ϸ֢͢Π͵͡͹͵扖Ѻ @RyotaMurohoshi 2017/12/09 (ࢿ) ̓ଙ๛ͶΞ̈́Unity ͠独;独΀ ΄LTय़տ 2017̓ϪϺΠͭͼΘ͚͚΄Ξ̈́

  2. @RyotaMurohoshi

  3. Unity 2017.2ͽ Vector2Intࣳ΀Ϳ ݱ圵Intᔮ䯤᭜᭄֛͢ےѺ 托ͭͥ΅ͩͩΨ憎ͼ΃ : h$ps:/ /qiita.com/RyotaMurohoshi/items/4341f51548d8c53559f5

  4. Vector2Int : Vecotr2΄intᇇ Vector2Int positon = new Vector2Int(3, 2); int

    x = position.x; int y = position.y;
  5. ΘͷΣΩSerializeΘͽͣΡ public class Example : MonoBehaviour { [SerializeField] Vector2Int position;

    }
  6. ΘͷΣΩSerializeΘͽͣΡ

  7. ͩ΢̵Θ͹;෱ͥΑͭ͡͹͵

  8. ͳ͜Ͷ

  9. Vector2Int;͡΄Intᔮ䯤᭜̵֛͢ Unity 2017.2΄ڹͽΘֵ͞ͼ԰䟵௔͘͢͹ͼ̵ ͳ΄ΔΔྋ憒΄Κͺ΁ᑏᤈͽͣΡ϶αϣ϶ϷΨ֢Σ͜Ѻ

  10. Vector2Int΄԰䟵϶αϣ϶Ϸ֢͢Π͵͡͹͵扖Ѻ @RyotaMurohoshi 2017/12/09 (ࢿ) ̓ଙ๛ͶΞ̈́Unity ͠独;独΀ ΄LTय़տ 2017̓ϪϺΠͭͼΘ͚͚΄Ξ̈́

  11. ֢̿Π͵͡͹͵扖̀ ֢̿͹͵扖̀ͽ΅΀͚

  12. ͩ΢΅Vector2Int΄԰䟵϶αϣ϶ϷΨ֢Σ͜; ᝒ櫞΁ፗᶎ̵ͭૡॢͭ㪥Π̵᩼͞ͳͭͼগ㶧ͭ͵̵ͳΩ΀扖

  13. Vector2Int΄԰䟵϶αϣ϶Ϸ ԰䟵௔͹ͼ͚͹ͼΘ͚Σ͚Σ͘ΡΞ΃

  14. ϊЄφπЄϖAPI԰䟵௔ (㵛ಋ΁޸ݷ) • UnityEngine.Vector2Int͹ͼྋ憒;ݶͮݷڹ独APIΨ೮ͺ • UNITY_2017_2_OR_NEWERΨֵ͚Unity 2017.2ڹ΄Ε΁䌏䖕 • Unity 2017.2΁ίϐϤϔЄϕͭͼΘ̵԰䟵䯤᭜֛Ψֵ͜πЄϖ

    ΅֜Θ䄜͞΀ͥͼΘ͚͚Ξ͜΁ͭ͵͚
  15. τϷί϶αχ԰䟵௔ (㵛ಋ΁޸ݷ) Unity 2017.2ڹͽ̵԰䟵϶αϣ϶ϷΨτϷί϶αχ Unity 2017.2΁ϤϺυδμϕΨίϐϤϔЄϕͽͭ͵䦒̵ ڹ΁τϷί϶αχͭ͵㮔Ψͳ΄ΔΔֵ͞ΡΞ͜΁ͭ͵͚

  16. ϊЄφπЄϖAPI԰䟵௔ τϷί϶αχ԰䟵௔ ͩ΄2ͺΨ΀Ω;ͯ͡Ρ΄΁ᝒ㴼ͭ͵

  17. ͘ΩΔΠ஀΁ᒈ͵΀͚ Unity΁ͧ͠ΡτϷί϶αχTIPS

  18. [Serializable] public class Player { [SerializeField] int hp; [SerializeField] string

    name; }
  19. class CharacterContainer : MonoBehaviour { [SerializeField] Player character; }

  20. None
  21. τЄЀϢήαϸ(*.unity独YAML୵ୗ)ͽ̵ ͩΩ΀ఽͮͽτϷί϶αχͫ΢Ρ Playerࣳͽ͘Ρఘ䁭΅Ϳͩ΁ΘτϷί϶αχͫ΢΀͚ character: hp: 300 name: "Yusha"

  22. [Serializable] public class Player { [SerializeField] int hp; [SerializeField] string

    name; } [Serializable] public class Enemy { [SerializeField] int hp; [SerializeField] string name; }
  23. Player => Enemy ΁䨗ͣ䟵͞ class CharacterContainer : MonoBehaviour { //

    䨗ͣ䟵͞ : [SerializeField] Player character; [SerializeField] Enemy character; }
  24. None
  25. τЄЀϢήαϸ(*.unity独YAML୵ୗ)ͽ̵ ͩΩ΀ఽͮͽτϷί϶αχͫ΢͵ΔΔѺѺѺ character: hp: 300 name: "Yusha"

  26. τЄЀϢήαϸ(*.unity独YAML୵ୗ)ͽ΅̵ τϷί϶αχͫ΢ΡϢΰЄϸϖ(΄ࣳ;ݷڹ)͢ݶͮ΀Ο ࣳᛔ֛͢䄜Υ͹ͼΘ԰䟵௔͘͢Ρ

  27. ͽ̵Vector2Int

  28. Vector2Int΅τЄЀϢήαϸ(*.unity独YAML୵ୗ)ͽ̵ ͩΩ΀ఽͮͽτϷί϶αχͫ΢Ρ position: {x: 1, y: 3}

  29. 嘦扯Ѻ

  30. [Serializable] public struct Dummy { [SerializeField] int x; [SerializeField] int

    y; }
  31. class Example : MonoBehaviour { [SerializeField] Dummy position; }

  32. None
  33. τЄЀϢήαϸ(*.unity独YAML୵ୗ)ͽ̵ ͩΩ΀ఽͮͽτϷί϶αχͫ΢Ρ position: x: 1 y: 3

  34. Dummy => Vector2Int ΁䨗ͣ䟵͞ class Example : MonoBehaviour { //[SerializeField]

    Dummy position; [SerializeField] Vector2Int position; }
  35. None
  36. τЄЀϢήαϸ(*.unity独YAML୵ୗ)ͽ̵ ͩΩ΀ఽͮͽτϷί϶αχͫ΢Ρ position { x: 1, y: 3 }

  37. ԰䟵϶αϣ϶ϷΘྋ憒΄Vector2Int;ݶ䯭̵ int΄x;yΨτϷί϶αχͽͣ΢Ά͚͚Ѻ

  38. #if !UNITY_2017_2_OR_NEWER namespace UnityEngine { [System.Serializable] public struct Vector2Int {

    [SerializeField] int x; [SerializeField] int y; // ኼ } } #endif
  39. SerializeField͢ͺ͚ͼΡ ϢΰЄϸϖ x独yΨਧ嬝 ͩΩ΀ఽͮͽ͚ͧΡѺ

  40. ͚ͧ΀͚ѺѺѺѺ

  41. ͩ΢ͶΗѺѺѺ #if !UNITY_2017_2_OR_NEWER namespace UnityEngine { [System.Serializable] public struct Vector2Int

    { // ϢΰЄϸϖ [SerializeField] int x; [SerializeField] int y; // ኼ } } #endif
  42. API԰䟵௔ጱ΁΅ͩͭ͜͵͚ Vector2Int positon = new Vector2Int(3, 2); // ϤϺϞϓΰ int

    x = position.x; int y = position.y;
  43. ΀΁͚ͧ͢΀͚͡ 䶲͢՞ͣΔͭ͵Ҙ

  44. ϊЄφπЄϖAPI԰䟵ጱ΁΅̵̿x̀͹ͼ̿ϤϺϞϓΰ̀͢஠ᥝ τϷί϶αχ԰䟵௔΁΅̵̿x̀͹ͼ̿ϢΰЄϸϖ̀͢஠ᥝ

  45. C#ጱ΁΅ݶݷ΄ϢΰЄϸϖ;ϤϺϞϓΰΨϯϓ΀͚

  46. ͺΩͶorz

  47. x独y;͚͜ϢΰЄϸϖݷͽ΀͚;τϷί϶αχ΄԰䟵௔͢orz [System.Serializable] public struct Vector2Int { [SerializeField] int _x; public

    int x { get { return _x; } set { _x = value; }} [SerializeField] int _y; public int y { get { return _y; } set { _y = value; }} }
  48. x独y;͚͜ϤϺϞϓΰݷͽ΀͚;API΄԰䟵௔͢orz [System.Serializable] public struct Vector2Int { [SerializeField] int x; public

    int X { get { return x; } set { x = value; }} [SerializeField] int y; public int Y { get { return y; } set { y = value; }} }
  49. C#ͽ΅̵ݶӞ΄ݷڹ΄ϢΰЄϸϖ;ϤϺϞϓΰΨਧ嬝ͽͣ΀͚ [System.Serializable] public struct Vector2Int { [SerializeField] int x; public

    int x { get { return x; } set { x = value; }} [SerializeField] int y; public int y { get { return y; } set { y = value; }} }
  50. ͺΩͶorz

  51. τϷί϶αχݷ΅̵ϢΰЄϸϖݷͽͭ͡䷥ΗΟ΢΀͚ORZ FormerlySerializedAs΅አ᭔͢晅͜orz τϷί϶αχͯΡݷڹΨ೰ਧͯΡં௔͘͢΢Άorz

  52. ͩΩ΀䱛ᚆ΅΀͚ͧ΢Ϳ̵͘͹͵ΟΞ͡͹͵ [System.Serializable] public struct Vector2Iny { [SerializedAs("x")] // ͩΩ΀ఽͮͽݷڹΨ೰ਧͭ͵͡͹͵ [SerializeField]

    int _x; public int x { get { return _x; } set { _x = value; }} [SerializedAs("y")] // ͩΩ΀ఽͮͽݷڹΨ೰ਧͭ͵͡͹͵ [SerializeField] int _y; public int y { get { return _y; } set { _y = value; }} }
  53. ͺΩͶorz

  54. 橀(?)΄ṹ悬Ψֵ͞΀͚͡手ͭͼΕΡ

  55. Mono.Cecil ILΨͪ΁Νͪ΁Ν䨗ͣ䟵̵͞ෛ͵΀DLLΨ֢Ρͩ;͢ͽͣΡ

  56. Mono.CecilΨͺ͡͹ͼ x͹ͼϢΰЄϸϖ;ϤϺϞϓΰΨݶ䦒΁೮ͼΡΞ͜΁ ϢΰЄϸϖݷΨ䨗ͣ䟵͞ෛ͵΀DLLΨ֢ΠڊͯѺ

  57. ϤϺϞϓΰ΅πЀϞαϸͯΡ;ILጱ΁΅ get_x;͚͜Ϯϊϐϖ;set_x;͚͜Ϯϊϐϖ֢͢Ο΢Ρ x;͚͜ݷڹ΄ϮЀϝ͢ͽͣΡΥͧͽ΅΀̶͚ [System.Serializable] public class PropertyExample { [SerializeField ]

    int _x; public int x { get { return _x; } set { _x = value; } } }
  58. ̿C#̀ጱ΁΅NGͽΘ̿.NET̀ጱ΁΅ ݶݷ΄privateϢΰЄϸϖ;publicϤϺϞϓΰΨ֢Ρͩ;΅ݢᚆ

  59. // Mono.CecilͽDLL΄Ӿ΄ࣳ΄قϢΰЄϸϖ΄ض毣΄`_`Ψ;͹͵ݷᑍ΁䄜ๅͯΡπЄϖ var targetAssembly = AssemblyDefinition.ReadAssembly(sourceAssemblyPath); foreach (var type in

    targetAssembly.Modules.SelectMany(x => x.Types)){ foreach (var field in type.Fields) { var fieldReference = field.Module.Import(field); fieldReference.Name = fieldReference.Name.TrimStart('_'); } } sourceAssembly.Write(resultAssemblyPath);
  60. C#ͽ΅ᔰፗ΁΅䨗ͧ΀͚ͧ΢Ϳ x;͚͜private ϢΰЄϸϖ& public ϤϺϞϓΰ y;͚͜private ϢΰЄϸϖ& public ϤϺϞϓΰ ݶ䦒΁ΘͺDLLΨMono.Cecilͽ֢΢ΡѺ

  61. 㵛͹͵Ѻ

  62. ͧ΢Ϳͩ΢ΨͯΡ; UNITY_2017_2_OR_NEWERֵ͢͞΀͚

  63. ͺΩͶorz

  64. ϢΰЄϸϖ;ϤϺϞϓΰ΅㳨Ͷͧ΢Ϳ Θͩ͜΢ͽ͚͚͡ORZ [System.Serializable] public struct Vector2Int { public int x;

    public int y; } ϤϺϞϓΰ;ϢΰЄϸϖ΅㳨Ͷ͡Ο̵͚ΚͶͧͿ
  65. Δ;Η(;͚͜ݷ΄ݍ፜)

  66. Vector2Int΄԰䟵϶αϣ϶Ϸ֢͢Π͵͡͹͵扖Ѻ

  67. Vector2Int;͡΄԰䟵ࣳ ཿ͚ͭΞ΃

  68. • ϢΰЄϸϖ;ͳ΄ࣳ͢ݶͮ΀ΟτϷί϶αχ԰䟵௔͘Π • SerialzieField͢τϷί϶αχݷΨ೰ਧͽͣ΢Ά͚͚΄΁ORZ • Θͭͥ΅SerialzieAs͘͢΢Ά͚͚΄΁ORZ • Mono.CecilΨֵ͞Άݶݷ΄ϢΰЄϸϖ;ϤϺϞϓΰΨͺͥ΢Ρ

  69. Θͩ͜΢ͽ͚͚͡ORZ [System.Serializable] public struct Vector2Int { public int x; public

    int y; }
  70. ਠҁͥΚ͚ͭѺ҂