2,585
社区成员




void OnGUI()
{
if (Event.current.type == EventType.MouseDown)
{//记录鼠标按下的位置
first = Event.current.mousePosition;
}
if (Event.current.type == EventType.MouseDrag)
{//记录鼠标拖动的位置
second = Event.current.mousePosition;
if (second.x < first.x)
{//拖动的位置的x坐标比按下的位置的x坐标小时,响应向左事件
print("left");
}
if (second.x > first.x)
{//拖动的位置的x坐标比按下的位置的x坐标大时,响应向右事件
print("right");
}
float tempX = first.x-second.x;
//控制地图不移出屏幕
if (this.transform.position.x + tempX < 640)
{
return;
}
this.transform.Translate(new Vector3(tempX, 0, 0));
first = second;
}
}
以上代码,放到一个GO上,就能看到效果了,在pc 移动端都有有效