团结 |带有 phics2D Collider 的多点触控
Unity | Multitouch with phics2D Collider
我在我的场景中创建了带有图像的 sprite,并使它们像按钮一样工作。我想要的只是所有按钮都像多点触摸屏一样同时工作。下面的代码是我从现在开始所做的。我使用具有游戏对象的 TouchList。我认为 TouchList.Count 会增加。但是,TouchList.Counts 始终显示 (number)1。
我绝对不能同时触摸两个游戏对象,因为第一个触摸的游戏对象的状态更改为 "Exit"。如何在 TouchList 中添加触摸的游戏对象?
请告诉我如何修复此代码。
void Update () {
if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
Vector2 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
RaycastHit2D hit1 = Physics2D.Raycast(pos, Vector2.zero);
//foreach (Touch touch in Input.touches) {
if (hit1.collider != null) {
GameObject recipient = hit1.transform.gameObject;
touchList.Add(recipient);
//Debug.Log ("recipient : " + hit1.transform.gameObject);
Debug.Log ("I'm hitting "+hit1.collider.name);
Debug.Log ("touchList.Count : " + touchList.Count);
//recipient.SendMessage("OnColliderHit",hit1.point,SendMessageOptions.DontRequireReceiver);
if (Input.GetMouseButtonDown(0)) {
recipient.SendMessage("OnTouchDown",hit1.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButtonUp(0)) {
recipient.SendMessage("OnTouchUp",hit1.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButton(0)) {
recipient.SendMessage("OnTouchStay",hit1.point,SendMessageOptions.DontRequireReceiver);
}
}
//}
foreach (GameObject g in touchesOld) {
if (!touchList.Contains(g)) {
g.SendMessage("OnTouchExit",hit1.point,SendMessageOptions.DontRequireReceiver);
}
}
}
}
这是我修改后的代码。它运行良好。:-)
void Update () {
if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
foreach (Touch touch in Input.touches) {
Debug.Log ("touch.position : " + touch.position.ToString());
Vector2 pos = Camera.main.ScreenToWorldPoint(touch.position);
RaycastHit2D hit1 = Physics2D.Raycast(pos, Vector2.zero);
if (hit1.collider != null) {
GameObject recipient = hit1.transform.gameObject;
touchList.Add(recipient);
//Debug.Log ("recipient : " + hit1.transform.gameObject);
Debug.Log ("I'm hitting "+hit1.collider.name);
Debug.Log ("touchList.Count : " + touchList.Count);
//recipient.SendMessage("OnColliderHit",hit1.point,SendMessageOptions.DontRequireReceiver);
if (Input.GetMouseButtonDown(0)) {
recipient.SendMessage("OnTouchDown",hit1.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButtonUp(0)) {
recipient.SendMessage("OnTouchUp",hit1.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButton(0)) {
recipient.SendMessage("OnTouchStay",hit1.point,SendMessageOptions.DontRequireReceiver);
}
}
}
foreach (GameObject g in touchesOld) {
if (!touchList.Contains(g)) {
g.SendMessage("OnTouchExit",SendMessageOptions.DontRequireReceiver);
}
}
}
}
我在我的场景中创建了带有图像的 sprite,并使它们像按钮一样工作。我想要的只是所有按钮都像多点触摸屏一样同时工作。下面的代码是我从现在开始所做的。我使用具有游戏对象的 TouchList。我认为 TouchList.Count 会增加。但是,TouchList.Counts 始终显示 (number)1。 我绝对不能同时触摸两个游戏对象,因为第一个触摸的游戏对象的状态更改为 "Exit"。如何在 TouchList 中添加触摸的游戏对象?
请告诉我如何修复此代码。
void Update () {
if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
Vector2 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
RaycastHit2D hit1 = Physics2D.Raycast(pos, Vector2.zero);
//foreach (Touch touch in Input.touches) {
if (hit1.collider != null) {
GameObject recipient = hit1.transform.gameObject;
touchList.Add(recipient);
//Debug.Log ("recipient : " + hit1.transform.gameObject);
Debug.Log ("I'm hitting "+hit1.collider.name);
Debug.Log ("touchList.Count : " + touchList.Count);
//recipient.SendMessage("OnColliderHit",hit1.point,SendMessageOptions.DontRequireReceiver);
if (Input.GetMouseButtonDown(0)) {
recipient.SendMessage("OnTouchDown",hit1.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButtonUp(0)) {
recipient.SendMessage("OnTouchUp",hit1.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButton(0)) {
recipient.SendMessage("OnTouchStay",hit1.point,SendMessageOptions.DontRequireReceiver);
}
}
//}
foreach (GameObject g in touchesOld) {
if (!touchList.Contains(g)) {
g.SendMessage("OnTouchExit",hit1.point,SendMessageOptions.DontRequireReceiver);
}
}
}
}
这是我修改后的代码。它运行良好。:-)
void Update () {
if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
foreach (Touch touch in Input.touches) {
Debug.Log ("touch.position : " + touch.position.ToString());
Vector2 pos = Camera.main.ScreenToWorldPoint(touch.position);
RaycastHit2D hit1 = Physics2D.Raycast(pos, Vector2.zero);
if (hit1.collider != null) {
GameObject recipient = hit1.transform.gameObject;
touchList.Add(recipient);
//Debug.Log ("recipient : " + hit1.transform.gameObject);
Debug.Log ("I'm hitting "+hit1.collider.name);
Debug.Log ("touchList.Count : " + touchList.Count);
//recipient.SendMessage("OnColliderHit",hit1.point,SendMessageOptions.DontRequireReceiver);
if (Input.GetMouseButtonDown(0)) {
recipient.SendMessage("OnTouchDown",hit1.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButtonUp(0)) {
recipient.SendMessage("OnTouchUp",hit1.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButton(0)) {
recipient.SendMessage("OnTouchStay",hit1.point,SendMessageOptions.DontRequireReceiver);
}
}
}
foreach (GameObject g in touchesOld) {
if (!touchList.Contains(g)) {
g.SendMessage("OnTouchExit",SendMessageOptions.DontRequireReceiver);
}
}
}
}