どこから始めるUnity Test

1d1580fb0945b0ffadff18e28bead3c5?s=47 いも
January 23, 2019

どこから始めるUnity Test

Gotanda Unity #10 のLT資料です。

1d1580fb0945b0ffadff18e28bead3c5?s=128

いも

January 23, 2019
Tweet

Transcript

  1. 6.

    public class PlayerUnit : MonoBehaviour { [SerializeField] private Bullet _bulletPrefab

    = null; [SerializeField] private float _interval; [SerializeField] private float _speed; [SerializeField] private int _hp; private float _reloadTime; void Update() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); transform.position += new Vector3(horizontal, vertical, 0F) * _speed * Time.deltaTime; if (Input.GetButton("Fire1") && _reloadTime < 0) { var bullet = Instantiate(_bulletPrefab, transform.position, Quaternion.identity); bullet.SetUp(Vector2.up); _reloadTime = _interval; } _reloadTime -= Time.deltaTime; } void OnTriggerEnter2D(Collider2D collider) { if (collider.tag == "Bullet") { _hp--; if (_hp <= 0) { Destroy(gameObject); } } } }
  2. 11.

    namespace Tests { public class PlayerUnitTest { [UnityTest] public IEnumrator

    MoveTest() { var gameObject = new GameObject(); var playerUnit = gameObject.AddComponent<PlayerUnit>(); var beforePosition = playerUnit.transform.position; yield return null; Assert.AreNotEqual(beforePosition, playerUnit.transform.position); } } } ͜Μͳײ͡ʹॻ͚Δͱ͍͍ͳ
  3. 13.

    ௨Βͳ͍ཧ༝Λߟ͑Δ public class PlayerUnit : MonoBehaviour { [SerializeField] private Bullet

    _bulletPrefab = null; [SerializeField] private float _interval; [SerializeField] private float _speed; [SerializeField] private int _hp; private float _reloadTime; void Update() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); transform.position += new Vector3(horizontal, vertical, 0F) * _speed * Time.deltaTime; ~~~~ } } • _speedʹ஋͕ೖͬͯͳ͍ • ΩʔೖྗͰ͖ͯͳ͍ͷͰ࣮࣭0ϕΫτϧ SerializeField΋Input΋Test͔Β͸ѻ͑ͳ͍
  4. 14.

    ςετ͠΍͍͢΋ͷͱ͠ʹ͍͘΋ͷΛ෼཭͢Δ • Unity APIʹґଘ͍ͯ͠ͳ͍Ϋϥε • Unity APIʹґଘ͍ͯ͠ΔΫϥε • RigidBody,Animator etc..

    • MonoBehaviourΛܧঝͨ͠Ϋϥε • GUI෦෼ Unityଆʹۙͮ͘ʹͭΕͯςετͷ೉қ౓͸্͕͍ͬͯ͘ ςετ͠΍͍͢΋ͷ ςετ͠ʹ͍͘΋ͷ
  5. 15.

    ςετ͠΍͍͢ํ޲ʹدͤΔ public class PlayerUnit : MonoBehaviour { [SerializeField] private Bullet

    _bulletPrefab = null; [SerializeField] private float _interval; [SerializeField] private float _speed; [SerializeField] private int _hp; private float _reloadTime; void Update() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); transform.position += new Vector3(horizontal, vertical, 0F) * _speed * Time.deltaTime; ~~~~ } } • ܭࢉॲཧ͚ͩ੾Γग़ͤ͹ςετॻ͚ͦ͏ • Inputʹ௚઀ґଘ͠ͳ͚Ε͹ςετ࣌ͷೖྗΛ༻ҙͰ͖ͦ͏ ॻ͖ͮΒ͍ͱ͜Ζ͸ॻ͔ͳ͍
  6. 16.

    public class PlayerUnitMove { private float _speed; public PlayerUnitMove(float speed)

    { _speed = speed; } public Vector3 CalculateVelocity(Vector3 direction) => direction.normalized * _speed; } namespace Tests { public class PlayerUnitMoveTest { [Test] public void CalculateVelocityTest() { var move = new PlayerUnitMove(3); Assert.AreEqual(new Vector3(0, 3F,0), move.CalculateVelocity(Vector3.up)); } } } ܭࢉॲཧ͚ͩΫϥεΛ੾Γग़͢
  7. 17.

    public class PlayerUnit : MonoBehaviour { [SerializeField] private Bullet _bulletPrefab

    = null; [SerializeField] private float _interval; [SerializeField] private int _hp; private float _reloadTime; private PlayerUnitMove _move; public void Initialize(PlayerUnitMove move) { _move = move; } void Update() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); var direction = new Vector3(horizontal, vertical, 0F); transform.position += move.CalculateVelocity(direction) * Time.deltaTime; ~~~~ } } Ҡಈ஋ܭࢉ෦෼Λࠩ͠ସ͑ ΦϒδΣΫτ͸֎෦͔Β౉ͤΔΑ͏ʹ͢Δ
  8. 18.

    Input΁௚઀ґଘΛ΍ΊΔ public interface IPlayerUnitInput { Vector3 GetAxis(); } public class

    IUnityInput : IPlayerUnitInput { public Vector3 GetAxis() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); return new Vector3(horizontal, vertical, 0); } } ೖྗ෦෼ΛΠϯλϑΣʔεʹͯ͠ɺPlayerUnitʹΩʔೖྗΛҙࣝͤ͞ͳ͍
  9. 19.

    Input΁௚઀ґଘΛ΍ΊΔ ·ͨ΋ΦϒδΣΫτ͸֎෦͔Β౉ͤΔΑ͏ʹ͢Δ public class PlayerUnit : MonoBehaviour { [SerializeField] private

    Bullet _bulletPrefab = null; [SerializeField] private float _interval; [SerializeField] private int _hp; private float _reloadTime; private PlayerUnitMove _move; private IPlayerUnitInput _input; public void Initialize(PlayerUnitMove move, IPlayerUnitInput input) { _move = move; _input = input; } void Update() { var direction = _input.GetAxis(); transform.position += move.CalculateVelocity(direction) * Time.deltaTime; ~~~~ } }
  10. 20.

    namespace Tests { public class PlayerUnitTest { class MockInput :

    IPlayerUnitInput { private Vector3 _axis; public MockInput(Vector3 axis) { _axis = axis; } public Vector3 GetAxis() => _axis; } [UnityTest] public IEnumerator MoveTest() { var gameObject = new GameObject(); var playerUnit = gameObject.AddComponent<PlayerUnit>(); var mockInput = new MockInput(Vector3.up); playerUnit.Initialize(new PlayerUnitMove(1), mockInput); var beforePosition = playerUnit.transform.position; yield return null; Assert.AreNotEqual(beforePosition, playerUnit.transform.position); } } } ͪΐͬͱॻ͖௚ͯ͠ςετ࣮ߦ
  11. 21.

  12. 23.
  13. 25.

    Humble Object ύλʔϯ • ςετ͠ʹ͍͘΋ͷͱͦ͏Ͱͳ͍ ΋ͷΛ໌֬ʹ෼͚ͯ෼ׂ͢Δύλ ʔϯ • GUIͷॲཧͷৄࡉ͸ςετ͠ʹ͍͘ ͕ɺGUIͷৼΔ෣͍͸ςετͰ͖Δ

    • ӈͷίʔυ͸ɺ஄ͷੜ੒෦෼ͷς ετ͸೉͍͕͠ɺʮੜ੒͢Δͱ͍ ͏ৼΔ෣͍͕ߦΘΕ͔ͨʯ·Ͱ͸ ςετͰ͖Δ • MonoBehaviorʹ͸༗ޮ͔΋͠Εͳ ͍ public class PlayerUnitPresenter { private IPlayerUnitView _view; public void Shot(Vector3 position) { _view.Shot(position); } } public interface IPlayerUnitView { void Shot(Vector3 position); } public class PlayerUnitView : MonoBehaviour, IPlayerUnitView { public void Shot(Vector3 position) { // ஄࡞ͬͨΓ͢Δ } }
  14. 29.