J’ai un souci avec des contrôles sur iOS. Je développe un petit jeu de vaisseau avec 2 boutons permettant d’activer les moteurs pour faire avancer le vaisseau. (voir ce post viewtopic.php?f=12&t=18083).
- Si le joueur appuie sur un bouton : le vaisseau pivote
- Si le joueur appuie sur les 2 boutons : le vaisseau avance
- le joueur appuie sur les 2 boutons >> le vaisseau avance
- le joueur relâche un des 2 boutons >> le vaisseau avance toujours alors qu’il est censé s'arrêter et commencer à pivoter
https://youtu.be/WxSBuXpjZtU
Voilà le code que j’utilise pour gérer les boutons :
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class TouchController : MonoBehaviour
{
private List<RaycastResult> raycastResults = new List<RaycastResult>();
private Dictionary<int, List<RaycastResult>> raycastResultsByFingerId = new Dictionary<int, List<RaycastResult>>();
private Touch touch;
private PointerEventData pointer;
public GameParentController gameController;
private void Start()
{
pointer = new PointerEventData(EventSystem.current);
}
void Update()
{
if (Input.touchCount > 0)
{
pointer = new PointerEventData(EventSystem.current);
for (int i = 0; i < Input.touchCount; i++)
{
touch = Input.GetTouch(i);
// Handle finger movements based on TouchPhase
switch (touch.phase)
{
//When a touch has first been detected, change the message and record the starting position
case TouchPhase.Began:
// Record initial touch position.
//Debug.Log("DEBUT " + i);
pointer.position = touch.position;
EventSystem.current.RaycastAll(pointer, raycastResults);
if (raycastResults.Count > 0)
{
foreach (var go in raycastResults)
{
Debug.Log("TOUCH BEGAN : " + go.gameObject.name, go.gameObject);
if (go.gameObject.name == "ButtonMoteurGauche")
{
gameController.ActiverMoteurDroit();
}
else if (go.gameObject.name == "ButtonMoteurDroit")
{
gameController.ActiverMoteurGauche();
}
else if (go.gameObject.name == "Fire")
{
gameController.Fire();
}
}
}
break;
case TouchPhase.Ended:
// Report that the touch has ended when it ends
//Debug.Log("FIN " + i);
pointer.position = touch.position;
EventSystem.current.RaycastAll(pointer, raycastResults);
if (raycastResults.Count > 0)
{
foreach (var go in raycastResults)
{
Debug.Log("TOUCH END : " + go.gameObject.name, go.gameObject);
if (go.gameObject.name == "ButtonMoteurGauche")
{
gameController.StopMoteurDroit();
}
else if (go.gameObject.name == "ButtonMoteurDroit")
{
gameController.StopMoteurGauche();
}
}
}
break;
#if UNITY_IOS
case TouchPhase.Canceled:
// Report that the touch has ended when it ends
//Debug.Log("FIN " + i);
pointer.position = touch.position;
EventSystem.current.RaycastAll(pointer, raycastResults);
if (raycastResults.Count > 0)
{
foreach (var go in raycastResults)
{
Debug.Log("TOUCH END : " + go.gameObject.name, go.gameObject);
if (go.gameObject.name == "ButtonMoteurGauche")
{
gameController.StopMoteurDroit();
}
else if (go.gameObject.name == "ButtonMoteurDroit")
{
gameController.StopMoteurGauche();
}
}
}
break;
#endif
}
}
}
//#endif
}
}
J’ai essayé avec et sans le bloc “#if UNITY_IOS” pour TouchPhase.Canceled.
Qu’en pensez-vous ? Est-ce que je fais quelque chose de mal ?