天天看點

untiy 監聽螢幕點選 物體(實作)

第一種方式:

1.百度的第三方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TouchType : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {//判斷是否是點選事件
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                //如果是一根手指觸摸螢幕而且是剛開始觸摸螢幕 
                if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    Debug.Log("觸碰的對象 :" + hitInfo.collider.gameObject.name);
                    GameObject.Find("title/Text").GetComponent<Text>().text = hitInfo.collider.gameObject.name;
                    if (Input.GetTouch(0).tapCount == 2 && hitInfo.collider.gameObject.name == "wolun")
                    //判斷點選的次數
                    {
                        
                    }
                }
            }
        }


    }
}
           

2.相機需要添加(以下元件)

untiy 監聽螢幕點選 物體(實作)

3.被點選物體一定要包含檢測組鍵

比如:box ,meshColider,2dClips,等等

4.第一個方法是全局的,随便挂在一個物體下就行,全局就隻能出現一個不然事件觸發會觸發多次****

第二種方式:Trigger(元件可以事件,比較繁瑣)