2015-04-28 23 views
0

我寫了一個多點觸摸系統,可以正常使用collider2d(使用它們作爲按鈕)的對象,我可以在我的android手機中移動播放器。 但是,當我從UI畫布系統使用圖像並將此代碼添加到圖像時,它不會檢測到任何東西?! 這裏是我的代碼:統一觸摸控制用戶界面圖像

using UnityEngine; 
using System.Collections; 

public class Jump : MonoBehaviour { 

public float jumpForce; 
private GameObject hero; 

void Start() { 
    hero = GameObject.Find("hero"); 
} 

void Update() { 

    if (Input.touchCount > 0) 
    { 
     Touch[] myTouches = Input.touches; 
     for(int i = 0; i < Input.touchCount; i++) 
     { 
      if(Input.GetTouch(i).phase == TouchPhase.Began) 
      { 
       CheckTouch(Input.GetTouch(i).position, "began"); 
      } 
      else if (Input.GetTouch(i).phase == TouchPhase.Ended) 
      { 
       CheckTouch(Input.GetTouch(i).position, "ended"); 
      } 
     } 

    } 
} 

void CheckTouch (Vector3 pos, string phase) 
{ 

    Vector3 wp = Camera.main.ScreenToWorldPoint (pos); 
    Vector2 touchPos = new Vector2 (wp.x, wp.y); 

    Collider2D hit = Physics2D.OverlapPoint(touchPos); 

    if(hit.gameObject.name == "JumpButton" && hit && phase == "began") 
    { 
     hero.rigidbody2D.AddForce(new Vector2(0f, jumpForce)); //Add jump force to hero 
     audio.Play(); 
    } 
} 
} 

任何幫助嗎?

回答

1

圖形項目在畫布上,你需要使用GraphicRaycaster代替Physics2D使用UnityEngine.UI

1

你不必與觸摸自己的工作了。只需實現IPointerClickHandler界面,並確保您有EventSystem和適當的raycaster出現在場景中。