我正在开发一款应用程序,它有许多可以漂浮但不会相互碰撞的球。这意味着它们重叠很多。我已经得到了它,如果你点击/触摸球,它们就会被摧毁。但是,如果一个球落在另一个球的后面,那么这个球不会被破坏,直到前面的球被清除。
上图显示了我正在寻找的内容,如果用户点击/触摸位置x,则销毁所有对象,而不仅仅是最前面的对象。任何帮助非常感谢。
这是我的输入脚本:
compile
在球上我只有一个:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TouchInput : MonoBehaviour {
public LayerMask touchInputMask;
private List<GameObject> touchList = new List<GameObject> ();
private GameObject[] touchesOld;
private RaycastHit2D hit;
void Update () {
#if UNITY_EDITOR
if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo (touchesOld);
touchList.Clear ();
hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, touchInputMask);
//Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (hit) {
GameObject recipient = hit.transform.gameObject;
touchList.Add (recipient);
if (Input.GetMouseButtonDown(0)) {
recipient.SendMessage ("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButtonUp(0)) {
recipient.SendMessage ("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButton(0)) {
recipient.SendMessage ("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);
}
}
foreach (GameObject g in touchesOld) {
if (!touchList.Contains (g)) {
if(g!=null) {
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) {
hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, touchInputMask);
if (hit) {
GameObject recipient = hit.transform.gameObject;
touchList.Add (recipient);
if (touch.phase == TouchPhase.Began) {
recipient.SendMessage ("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Ended) {
recipient.SendMessage ("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Stationary) {
recipient.SendMessage ("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Canceled) {
recipient.SendMessage ("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
}
}
}
foreach (GameObject g in touchesOld) {
if (!touchList.Contains (g)) {
if (g != null) {
g.SendMessage ("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
}
}
}
}
答案 0 :(得分:1)
@Savlon提供的答案,非常感谢:)
private RaycastHit2D[] hits;
...
hits = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, touchInputMask);
foreach(RaycastHit2D hit in hits) {
GameObject recipient = hit.transform.gameObject;
touchList.Add (recipient);
if (Input.GetMouseButtonDown(0)) {
recipient.SendMessage ("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButtonUp(0)) {
recipient.SendMessage ("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButton(0)) {
recipient.SendMessage ("OnTouch",hit.point,SendMessageOptions.DontRequireReceiver);
}
}