2017-01-10 59 views
1

Image统一触摸和用户界面按钮

就像上面的图片,我们有一个模型,并在它前面的画布。它在脚下有对撞机,我也需要在按钮上点击事件。我想要的是当用户点击按钮时,gameobject无动作,但现在很难单击该按钮。

这里是我下面的代码:

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 
    { 
     EventSystem es = EventSystem.current; 
     Debug.Log("IsPointerOverGameObject === " + es.IsPointerOverGameObject(Input.GetTouch(0).fingerId)); 
     Debug.Log("currentSelectedGameObject === " + es.currentSelectedGameObject); 
     if (!(es.IsPointerOverGameObject(Input.GetTouch(0).fingerId) && es.currentSelectedGameObject != null)) 
     { 
      Debug.Log("Handle touch === "); 
      HandleTouchEvent(); 
     } 
    } 

是否有人有任何想法?

+0

他们只是按钮? –

+0

@TimCooley是的,ScrollRect中的按钮 –

回答

0

我认为当你触摸gameobject你的用户界面和其他gameobjects会受到影响,所以你应该排除用户使用这样的UI元素的时代两者

public class TouchExample : MonoBehaviour { 
    void Update() 
    { 
    // Check if there is a touch 
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 
     { 
     // Check if finger is over a UI element 
     if(EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) 
     { 
    Debug.Log("UI is touched"); 
    //so when the user touched the UI(buttons) call your UI methods 
     } 
    else 
     { 
    Debug.Log("UI is not touched"); 
    //so here call the methods you call when your other in-game objects are touched 
     } 
     } 
    } 
    }