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.