2016-07-04 1 views
1

このスクリプトは、ヒットしたゲームオブジェクトがキャンバスにない場合にのみ、コンソールにメッセージを表示します。キャンバス内にあるボタン上でマウスボタンが離されると、スクリプトは何もデバッグしません。これをどうすれば解決できますか?RaycastHitはキャンバスに問題がありますか?

RaycastHit hit; 

void Update() 
{ 
    if(Input.GetMouseButtonUp(0)) 
    { 
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
     //RayHit hit; 
     if(Physics.Raycast(ray, out hit)) 
     { 
      // do what you want 
      Debug.Log(hit.collider.gameObject.tag); 
     } 
    } 
} 
+0

UIはキャンバスですか?もし間違っているだけならば、[UnityのUIについてのチュートリアル](https://unity3d.com/learn/tutorials/topics/user-interface-ui)をお読みください。 – Logman

答えて

1

このように使用して、どのUIオブジェクトをクリックするかを取得できます。

if (Input.GetMouseButtonDown(0)) 
    { 
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 


     if (EventSystem.current.IsPointerOverGameObject()) 
     { 
      Debug.Log(EventSystem.current.currentSelectedGameObject.GetComponent<Text>().name); 

     } 

EventSystem.current.currentSelectedGameObject.GetComponent()。クリックされたオブジェクトを返します

に名前を付け、

EventSystem.current.IsPointerOverGameObject()

UIオブジェクトがクリックかどうかをチェックします。

関連する問題