unity设置鼠标按键焦点事件

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


你可能感兴趣的:(U3D基础学习)