首页 文章

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

提问于
浏览
0

EDIT : 显然,当我选择Windowed时,按钮可以正常工作 . 为什么是这样?如果没有选中这个盒子,它为什么不工作?

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

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

Code :

桨脚本:

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

Headers 屏幕脚本:

#pragma strict

function Start () {

}

function Update () {

}

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

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

1 回答

  • 0

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

    可以有两种方法来实现这一目标:
    1.简单的方法,让你的游戏全屏 .
    2.如果您希望在窗口框中使用游戏,则必须在播放器设置 - >分辨率中选中"Run In Background"选项 . 然后,学习如何挂钩鼠标事件:http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C

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

相关问题