欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

unity设置鼠标按键焦点事件

程序员文章站 2024-02-10 22:11:28
...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class Mousepause : MonoBehaviour
{
    [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
    public static extern int SetCursorPos(int x, int y);
    public bool ck = false;
	// Use this for initialization
	void Start () {
       SetCursorPos(200,200);
	}
	
	// Update is called once per frame
	void Update () {
		if(!ck)
        {
            if(Input.mousePosition.x<1)
            {
                //Mathf.RoundToInt数学算法,取整
                SetCursorPos(Mathf.RoundToInt(Screen.width * 0.5f), Mathf.RoundToInt(Input.mousePosition.y));
            }
            if(Input.mousePosition.y<1)
            {
                SetCursorPos(Mathf.RoundToInt(Input.mousePosition.x), Mathf.RoundToInt(Screen.height * 0.5f));
            }
            if(Input.mousePosition.x>Screen.width-5)
            {
                SetCursorPos(Mathf.RoundToInt(Screen.width * 0.5f), Mathf.RoundToInt(Input.mousePosition.y));
            }
           
        }
	}
    private void OnApplicationFocus()
    {
    
        ck = !ck;
    }
}