我在我的场景中创建了带有图像的精灵,我像按钮一样工作。我想要的是所有按钮都像多点触控屏幕一样同时工作。下面的代码是我从现在开始做的。我使用具有游戏对象的TouchList。我认为TouchList.Count会增加。但是,TouchList.Counts始终显示(数字)1。 我绝对不能同时触摸两个游戏对象,因为第一个被触摸的游戏对象的状态被改为"退出"。如何在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);
}
}
}
}
答案 0 :(得分:0)
这是我修改过的代码。效果很好。: - )
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);
}
}
}
}