Unity检查是否单击了对象

1 c# unity-game-engine

请记住,我是团结的新手.我创建了一个2D安卓游戏应用程序.

我创建了一个开始按钮(从图像中)并将一个Box colider和一个C#脚本附加到它上面.

当我点击"按钮"时,我希望程序移动到下一个"级别",除了我希望它只能工作,如果我点击对象而不是游戏中的任何地方.

这是C#:

using UnityEngine;
using System.Collections;

public class StartGame : MonoBehaviour {


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    if (Input.GetMouseButtonDown(0)) {
        Application.LoadLevel("Game");
    }

}

}
Run Code Online (Sandbox Code Playgroud)

我搜索了很多关于这一点,也许有人说,要解决这个问题,你必须使用,Ray并RaycastHit但我不能得到那个工作,要么.

这是我尝试用的Ray&RaycastHit

// Update is called once per frame
void Update () {
    if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0))) 
    {
        RaycastHit hit;

        Ray ray;

        #if UNITY_EDITOR
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        #elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
        ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
        #endif

        if(Physics.Raycast(ray, out hit))
        {
            Application.LoadLevel("Game");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助都会如此苛刻.

maZ*_*ZZu 5

可能最简单的方法是在游戏对象中添加框对撞机和脚本组件,包括下面的功能.

void OnMouseOver()
{
    if (Input.GetMouseButtonDown(0))
    {
        Application.LoadLevel("Game");
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我猜你原来的代码不适用于你的场景,因为你正在使用box collider 2d.Physics.Raycast正在测试3D对撞机.当我使用3D版本运行它时box collider它工作正常.