[Unity][iOS/WP8/Android] Tap Gesture Helper

Voici une classe d’aide pour interagir avec le “tap” ou, plus simplement, l’appui sur l’écran dans un jeu 2D. On utilise la technique de raycasting pour détecter les collisions :

   1: using UnityEngine;
   2:  
   3: namespace Assets.Scripts.Framework
   4: {
   5:     public static class TapHelper
   6:     {
   7:         /// <summary>
   8:         /// Get touch position
   9:         /// </summary>
  10:         /// <returns>Current touch position</returns>
  11:         public static Vector3 GetTouchedPosition()
  12:         {
  13:             Input.simulateMouseWithTouches = true;
  14:             if (Input.touchCount > 0 || Input.GetMouseButtonDown(0))
  15:             {
  16: #if UNITY_EDITOR
  17:                 return Input.mousePosition;
  18: #else
  19:                 return Input.touches[0].position;
  20:  
  21: #endif
  22:             }
  23:             return Vector3.zero;
  24:         }
  25:  
  26:         /// <summary>
  27:         /// Return if a game object is touch
  28:         /// </summary>
  29:         /// <param name="name">game object name</param>
  30:         /// <returns>true if touched, otherwise false</returns>
  31:         public static bool IsGameObjectTouched(string name)
  32:         {
  33:             Input.simulateMouseWithTouches = true;
  34:             if (Input.touchCount > 0 || Input.GetMouseButtonDown(0))
  35:             {
  36: #if UNITY_EDITOR
  37:                 var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, 1f);
  38: #else
  39:                 var hit = Physics2D.Raycast(Camera.main.ScreenPointToRay(Input.touches[0].position).origin, Vector2.zero, 1f);
  40: #endif
  41:  
  42:                 if (hit.collider != null)
  43:                 {
  44:                     return hit.collider.name == name;
  45:                 }
  46:             }
  47:  
  48:             return false;
  49:         }
  50:  
  51:         /// <summary>
  52:         /// Return if a game object is touched
  53:         /// </summary>
  54:         /// <param name="objectToDetect">instance of a game object</param>
  55:         /// <returns>true if touched, otherwise false</returns>
  56:         public static bool IsGameObjectTouched(GameObject objectToDetect)
  57:         {
  58:             Input.simulateMouseWithTouches = true;
  59:             if (Input.touchCount > 0 || Input.GetMouseButtonDown(0))
  60:             {
  61: #if UNITY_EDITOR
  62:                 var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, 1f);
  63: #else
  64:                 var hit = Physics2D.Raycast(Camera.main.ScreenPointToRay(Input.touches[0].position).origin, Vector2.zero, 1f);
  65: #endif
  66:  
  67:                 if (hit.collider != null)
  68:                 {
  69:                     return hit.collider.gameObject == objectToDetect;
  70:                 }
  71:             }
  72:  
  73:             return false;
  74:         }
  75:     }
  76: }

Ces 3 méthodes fonctionnent aussi directement dans l’éditeur de Unity Sourire

Ces billets pourraient aussi vous intéresser

Vous nous direz ?!

Commentaires

comments powered by Disqus