Unity中給3D物體添加按鈕點擊功能


1.將下面該段代碼賦給攝像機

void Update ()
{
    RaycastHit hit = new RaycastHit();
    for (int i = 0; i < Input.touchCount; ++i)
    {
        if (Input.GetTouch(i).phase.Equals(TouchPhase.Began))
        {
            // Construct a ray from the current touch coordinates
            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
            if (Physics.Raycast(ray, out hit))
            {
                hit.transform.gameObject.SendMessage("OnMouseDown");
            }
         }
    }
}
  2.為要被點擊的3D物體創建一個C#腳本,並   添加OnMouseDown函數,以使上段代碼進行觸發執行;   

void OnMouseDown()
{
    DoSomething();
}
This script can be attached to any GameObjects that you want to use as buttons.   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM