我的代码中有一个bug :当我将鼠标释放出屏幕时,unity无法检测到"GetMouseButtonUp",而当我将释放的鼠标移回屏幕时,它检测到不应该检测到的"GetMouseButton“。
所以当鼠标离开屏幕时,我想模拟发送一个鼠标打开事件,让单位检测"GetMouseButtonUp“。
如何模拟鼠标事件?
发布于 2015-08-20 13:34:14
伪码
bool mouseIsDown = false;
public void Update()
{
Rect screenRect = new Rect(-Screen.width/2, -Screen.height/2, Screen.width, Screen.height);
If(screenRect.Contains(Input.mousePosition))
{
if(mouseIsDown && !Input.GetMouseButton(0))
OnMouseUp();
if(Input.GetMouseButton(0))
{
OnMouse();
if(!mouseIsDown)
{
OnMouseDown();
mouseIsDown = true;
}
}
}
}
public void OnMouseDown()
{
// Do something
}
public void OnMouse()
{
// Do something
}
public void OnMouseUp()
{
// Do something
}
发布于 2015-08-22 00:23:33
我不确定我是否正确理解了你的问题。但是,您可以像使用Start()
Update()
Awake()
等函数一样使用这些函数
void OnMouseUp () {
}
void OnMouseDown (){
}
https://stackoverflow.com/questions/32108317
复制相似问题