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

さらば💩コード!Clean Architecture をマスターして次世代の Unity エン...

さらば💩コード!Clean Architecture をマスターして次世代の Unity エンジニアになろう!/Let's master the Clean Architecture

2018/06/09 (Sat) に ABC 2018s (Unity Track 2) にて登壇した資料になります。

Tetsuya Mori

June 09, 2018
Tweet

More Decks by Tetsuya Mori

Other Decks in Programming

Transcript

  1. w ࢠͲ΋޲͚εϚϗΞϓϦ
 ։ൃͳͲ w ೥͋ͨΓ͔Β
 6OJUZΨοπϦ w ͬ͜͝ϥϯυ
 ྦྷܭສ%- w

    "QQ4UPSF(PPHMF1MBZ
 ʹͯࢠͲ΋޲͚ΧςΰϦ
 ϥϯΩϯά501 14 ͓͠͝ͱ
  2. 30

  3. 38

  4. 44

  5. wιϑτ΢ΣΞΞʔΩςΫνϟ ʹԙ͍ͯɺҰൠʹҎԼͷ
 Α͏ʹϨΠϠʔΛ෼͚Δ
 ʢίτ͕ଟ͍ʣ w1SFTFOUBUJPO ݟͨ໨  w%PNBJO ϩδοΫ 

    w%BUB σʔλ 53 ؔ৺ͷ෼཭ for ϨΠϠʔ Presentation Domain Data Clean Architecture ʹ͍ͭͯ
  6. • Controller w γʔϯ੍ޚ w 1SFTFOUFSॳظԽ • Gummi w ߱ͬͯ͘Δάϛ


    •Floor wϘτϧͷচ •Ceil wϘτϧͷఱҪ •Wall wϘτϧͷน 86 Presentation / View CAFU ʹ͍ͭͯ
  7. 107 Dependency Injections using UnityEngine; public class Player : MonoBehaviour

    { private void Update() { MovePosition(); } private void MovePosition() { var horizontal = Input.GetAxis("Horizontal"); var vertical = Input.GetAxis("Vertical"); transform.position = new Vector3( transform.position.x + horizontal, transform.position.y, transform.position.z + vertical ); } } ͓·͚ ྫ͑͹͜Μͳίʔυ
  8. 108 Dependency Injections var horizontal = Input.GetAxis("Horizontal"); var vertical =

    Input.GetAxis("Vertical"); transform.position = new Vector3( transform.position.x + horizontal, transform.position.y, transform.position.z + vertical ); ͓·͚ ֦େ
  9. 110 Dependency Injections using UnityEngine; public class Player : MonoBehaviour

    { private IInput Input { get; set; } public void SetInput(IInput input) { Input = input; } private void Update() { MovePosition(); } private void MovePosition() { var horizontal = Input.GetHorizontalAxis(); var vertical = Input.GetVerticalAxis(); transform.position = new Vector3( transform.position.x + horizontal, transform.position.y, transform.position.z + vertical ); } } public class Installer : MonoBehaviour { [SerializeField] private Player player; private void Start() { player.SetInput(new UnityInput()); } } public interface IInput { float GetHorizontalAxis(); float GetVerticalAxis(); } public class UnityInput : IInput { public float GetHorizontalAxis() { return Input.GetAxis("Horizontal"); } public float GetVerticalAxis() { return Input.GetAxis("Vertical"); } } public class TestInput : IInput { public float GetHorizontalAxis() { return 1.0f; } public float GetVerticalAxis() { return 1.0f; } } ͓·͚ ͜͏ͳΔ
  10. 111 Dependency Injections public class PlayerTest { [UnityTest] public IEnumerator

    MovePositionTest() { var go = new GameObject(); var player = go.AddComponent<Player>(); yield return new WaitForEndOfFrame(); // 初期化待ち player.transform.position = Vector3.zero; player.SetInput(new TestInput()); // Dependency Injection yield return new WaitForEndOfFrame(); Assert.AreEqual(new Vector3(1f, 0f, 1f), player.transform.position); } } ͓·͚ ςετίʔυ͸ͪ͜Β
  11. 113 Dependency Injections public class UnityInput : IInput { public

    float GetHorizontalAxis() { return Input.GetAxis("Horizontal"); } public float GetVerticalAxis() { return Input.GetAxis("Vertical"); } } ͓·͚ ֦େʢ۩৅Ϋϥεʣ
  12. 114 Dependency Injections private IInput Input { get; set; }

    ... var horizontal = Input.GetHorizontalAxis(); var vertical = Input.GetVerticalAxis(); ͓·͚ ֦େʢར༻Ϋϥεʣ
  13. 115 Dependency Injections public void SetInput(IInput input) { Input =

    input; } ... public class Installer : MonoBehaviour { [SerializeField] private Player player; private void Start() { player.SetInput(new UnityInput()); } } ͓·͚ ֦େʢར༻Ϋϥεʣ
  14. Dependency Injections ͓·͚ ֦େʢςετίʔυʣ 116 public class PlayerTest { [UnityTest]

    public IEnumerator MovePositionTest() { var go = new GameObject(); var player = go.AddComponent<Player>(); yield return new WaitForEndOfFrame(); // 初期化待ち player.transform.position = Vector3.zero; player.SetInput(new TestInput()); // Dependency Injection yield return new WaitForEndOfFrame(); Assert.AreEqual( new Vector3(1f, 0f, 1f), player.transform.position ); } }
  15. 118 Dependency Injections ʢΞϯνύλʔϯʣ using UnityEngine; public static class InputManager

    { private static IInput Input { get; set; } public static IInput GetInstance() { return Input ?? (Input = new UnityInput()); } public static void SetInstance(IInput input) { Input = input; } } public class Player : MonoBehaviour { private void Update() { MovePosition(); } private void MovePosition() { var horizontal = InputManager.GetInstance().GetHorizontalAxis(); var vertical = InputManager.GetInstance().GetVerticalAxis(); transform.position = new Vector3( transform.position.x + horizontal, transform.position.y, transform.position.z + vertical ); } } ͓·͚ Ұํɺ͜Μͳίʔυ
  16. 119 Dependency Injections ʢΞϯνύλʔϯʣ public static class InputManager { private

    static IInput Input { get; set; } public static IInput GetInstance() { return Input ?? (Input = new UnityInput()); } public static void SetInstance(IInput input) { Input = input; } } ͓·͚ ֦େʢ*OQVU.BOBHFSʣ