在此游戏中,有许多带有标签“敌人”的对象,所有这些敌人都是同一类的实例,在该类中,我使用了“ OnMouseDown”方法来处理用户的单击,但这不是完美的方法,因为它与多点触控不兼容
我在网上四处张望,找到了解决方案:
public class TouchInput : MonoBehaviour {
public LayerMask touchInputMask;
private List<GameObject> touchList = new List<GameObject>();
private GameObject[] touchesOld;
private RaycastHit hit;
void update()
{
#if UNITY_EDITOR
if (Input.GetMouseButton(1) || Input.GetMouseButtonUp(1))
{
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, touchInputMask))
{
GameObject recipient = hit.transform.gameObject;
touchList.Add(recipient);
if (Input.GetMouseButtonDown(0))
{
recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
}
else if (Input.GetMouseButtonUp(0))
{
recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
}
else if (Input.GetMouseButton(0))
{
recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
}
foreach (GameObject g in touchesOld)
{
if (!touchList.Contains(g))
{
g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
#endif
if (Input.touchCount > 0)
{
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
foreach (Touch touch in Input.touches)
{
Ray ray = GetComponent<Camera>().ScreenPointToRay(touch.position);
if(Physics.Raycast(ray, out hit, touchInputMask))
{
GameObject recipient = hit.transform.gameObject;
touchList.Add(recipient);
if(touch.phase == TouchPhase.Began)
{
recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
}
else if (touch.phase == TouchPhase.Ended)
{
recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
}
else if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
{
recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
}
else if (touch.phase == TouchPhase.Canceled)
{
recipient.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
}
foreach (GameObject g in touchesOld)
{
if (!touchList.Contains(g))
{
g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
}
}
}
但是我认为该解决方案在2D游戏中不起作用(我尝试过,我将此脚本附加到了主摄像头,没有任何错误,但是似乎从来没有需要使用“ update”方法某些原因)。
有什么想法吗?
更新:
我正在使用的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class TouchInput : MonoBehaviour, IPointerDownHandler
{
void Start()
{
addPhysics2DRaycaster();
}
void addPhysics2DRaycaster()
{
Physics2DRaycaster physicsRaycaster = GameObject.FindObjectOfType<Physics2DRaycaster>();
if (physicsRaycaster == null)
{
Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
}
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}
//Implement Other Events from Method 1
}