TouchEventManager.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //#define ENABLE_LOG
  2. /*
  3. * Copyright (c) 2014 - 2022 t_saki@serenegiant.com
  4. */
  5. using UnityEngine;
  6. namespace Serenegiant {
  7. public class TouchEventManager
  8. {
  9. public enum TouchState
  10. {
  11. // タッチ無し
  12. None = -1,
  13. // タッチ開始
  14. Began = TouchPhase.Began,
  15. // タッチ移動
  16. Moved = TouchPhase.Moved,
  17. // タッチ静止
  18. Stationary = TouchPhase.Stationary,
  19. // タッチ終了
  20. Ended = TouchPhase.Ended,
  21. // タッチキャンセル
  22. Canceled = TouchPhase.Canceled,
  23. }
  24. public class TouchEvent
  25. {
  26. public Vector2 position;
  27. public TouchState state;
  28. /**
  29. * コンストラクタ
  30. * @param touched
  31. * @param position
  32. * @param phase
  33. */
  34. public TouchEvent(Vector2? position = null, TouchState state = TouchState.Began)
  35. {
  36. if (position == null)
  37. {
  38. this.position = new Vector2(0, 0);
  39. }
  40. else
  41. {
  42. this.position = (Vector2)position;
  43. }
  44. this.state = state;
  45. }
  46. /**
  47. * コピーコンストラクタ
  48. * @param other
  49. */
  50. public TouchEvent(TouchEvent other)
  51. {
  52. this.position = new Vector2(other.position.x, other.position.y);
  53. this.state = other.state;
  54. }
  55. }
  56. private TouchEvent touchEvent = new TouchEvent();
  57. /**
  58. * デフォルトコンストラクタ
  59. */
  60. public TouchEventManager()
  61. {
  62. }
  63. /**
  64. * タッチ状態の更新
  65. * MonoBehaviourの下位クラスのUpdateから呼ぶこと
  66. */
  67. public void Update()
  68. {
  69. touchEvent.state = TouchState.None;
  70. if (Application.isEditor)
  71. { // エディタで実行中…マウスの状態でタッチイベントをシミュレートする
  72. if (Input.GetMouseButtonDown(0))
  73. { // タッチしたとき
  74. touchEvent.state = TouchState.Began;
  75. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  76. Console.WriteLine("タッチした:");
  77. #endif
  78. }
  79. if (Input.GetMouseButtonUp(0))
  80. { // 離したとき
  81. touchEvent.state = TouchState.Ended;
  82. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  83. Console.WriteLine("タッチした:");
  84. #endif
  85. }
  86. if (Input.GetMouseButton(0))
  87. { // 押し続けているとき
  88. touchEvent.state = TouchState.Moved;
  89. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  90. Console.WriteLine("押し続けている");
  91. #endif
  92. }
  93. if (touchEvent.state != TouchState.None)
  94. { // タッチイベントがあるときは座標を取得
  95. touchEvent.position = Input.mousePosition;
  96. }
  97. }
  98. else
  99. { // 実機で実行中
  100. if (Input.touchCount > 0)
  101. {
  102. Touch touch = Input.GetTouch(0);
  103. touchEvent.position = touch.position;
  104. touchEvent.state = (TouchState)touch.phase;
  105. }
  106. }
  107. }
  108. /**
  109. * 現在のタッチ状態を取得
  110. */
  111. public TouchEvent GetTouch()
  112. {
  113. return new TouchEvent(touchEvent);
  114. }
  115. }
  116. } // namespace Serenegiant