在构建Unity游戏时,无法识别鼠标点击

时间:2015-07-23 01:44:29

标签: c# unity3d

编辑:显然,当我选择Windowed时,按钮可以正常工作。为什么是这样?如果没有选中该框,为什么它不起作用?

我在Unity中创建了一个游戏,一切都在Unity本身运行良好。当我构建并运行游戏时,我的按钮不再能识别鼠标点击。这是为什么?

这是一个基本的乒乓球游戏。我不知道为什么它可以在Unity中运行,而不是在Unity之外,如果它是代码,但这里是代码。

代码:

paddle script:

using UnityEngine;
using System.Collections;

public class paddle : MonoBehaviour {

    public float paddleSpeed = 1;
    public Vector3 playerPos = new Vector3(0,0,0);

    // Update is called once per frame
    void Update () {
        float yPos = gameObject.transform.position.y + (Input.GetAxis ("Vertical") * paddleSpeed);
        playerPos = new Vector3 (-20,Mathf.Clamp(yPos, -13F,13F),0);
        gameObject.transform.position = playerPos;
    }
}

球脚本:

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

    public float ballVelocity = 1500;

    private Rigidbody rb;
    private bool isPlay; //false by default
    int randInt; //random ball directon when game begins

    // Use this for initialization
    void Awake () {
        rb = gameObject.GetComponent<Rigidbody> ();
        randInt = Random.Range (1,3);
    }

    // Update is called once per frame
    void Update () {
        if(Input.GetMouseButton(0) == true && isPlay == false){
            transform.parent = null;
            isPlay = true;
            rb.isKinematic = false;
            if(randInt == 1){
                rb.AddForce(new Vector3(ballVelocity,ballVelocity,0));
            }
            if(randInt == 2){
                rb.AddForce(new Vector3(-ballVelocity,-ballVelocity,0));
            }
        }
    }
}
敌人脚本:

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

    public float speed = 8;
    private Vector3 targetPos;
    private Vector3 playerPos;
    private GameObject ballObj;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        ballObj = GameObject.FindGameObjectWithTag ("ball");
        if (ballObj != null) {
            targetPos = Vector3.Lerp (gameObject.transform.position, ballObj.transform.position, Time.deltaTime * speed);
            playerPos = new Vector3 (-20, Mathf.Clamp (targetPos.y, -13F, 13F), 0);
            gameObject.transform.position = new Vector3 (20, playerPos.y, 0);
        }
    }
}

得分脚本:

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour {



public TextMesh currScore;
    public GameObject ballPref;
    public Transform paddleObj;

    GameObject ball;
    private int score;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        ball = GameObject.FindGameObjectWithTag("ball");
        currScore.text = "" + score;
        }
    void OnTriggerEnter(Collider other) {
        if(other.tag == "ball"){
            score += 1;
            Destroy(ball);
            (Instantiate(ballPref, new Vector3(paddleObj.transform.position.x + 1, paddleObj.transform.position.y,0), Quaternion.identity) as GameObject).transform.parent = paddleObj;
        }
    }
}

标题屏幕脚本:

#pragma strict

function Start () {

}

function Update () {

}

function StartGame () {
    Application.LoadLevel("Main");
}

function ExitGame () {
    Application.Quit();
}

1 个答案:

答案 0 :(得分:0)

根据我的理解,即使鼠标指针不在游戏窗口内,您也希望Unity识别您的鼠标按钮事件。

有两种方法可以达到这个目的:
1.简单的方法,让你的游戏全屏 2.如果你想在一个窗口框中你的游戏,你必须检查&#34;在后台运行&#34;播放器设置中的选项 - &gt;解析度。然后,学习如何挂钩鼠标事件:http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C

请注意,挂钩鼠标事件教程仅适用于Windows,而不适用于Mac或Linux。我不知道如何使用Mac或Linux。