当我创建Unity游戏时,无法识别鼠标点击

本文关键字:识别 鼠标 创建 Unity 游戏 | 更新日期: 2023-09-27 18:04:03

EDIT:显然,当我选择窗口时,按钮工作。为什么会这样?为什么没有选中这个框就不能工作?

我在Unity中创造了一款游戏,并且在Unity中一切都运行良好。当我创建并运行游戏时,我的按键不再识别我的鼠标点击。为什么会这样?

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

代码:

桨脚本:

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();
}

当我创建Unity游戏时,无法识别鼠标点击

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

有两种方法可以达到这个目的:
1. 最简单的方法是,让你的游戏全屏。2. 如果你想让游戏出现在窗口框中,你必须在Player settings -> Resolution中勾选"Run in Background"选项。然后,学习如何挂钩鼠标事件:http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C

请注意,钩子鼠标事件教程只适用于Windows,不包括Mac和Linux。我不知道在Mac或Linux上怎么做