unity3d如何用c#实现在按住鼠标左键时即时获取鼠标坐标?

在unity3d引擎下,用c#实现按住鼠标左键时能够一直获取坐标,当鼠标在起点线上面时打印"hh1",鼠标按住移到起点线下面时打印"hh2",再按住移到上面时打印"hh1"。求各位大神点拨!

我自己写的失败的代码。。

void Update () {
        int x = 0;
        location = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        location.z = 0;
        if (Input.GetMouseButton(0)) {
            print("getmouse");
            if(location.y>0){
                x=0;
            }
            if(location.y<0){
                x=1;
            }
            switch (x) {
            case 0:
                if(location.y<0){
                    print ("hh1");
                    x=1;
                }
                break;
            case 1:
                if(location.y>0){
                    print ("hh2");
                    x=0;
                }
                break;
            }
        }
    }

2d下起点线,坐标(0,0,0)
clipboard.png

阅读 6.8k
1 个回答

我自己这两天终于想出来了!
代码如下:

public class NewBehaviourScript : MonoBehaviour {
    Vector3 location;
    private static int x=0;
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
        location = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        location.z = 0;
        if (Input.GetMouseButton (0)) {
            switch (x) {
                case 0:
                    if (location.y > 0) {
                        print ("d1");
                        x =1;
                    }
                    if(location.y<0){
                        print ("x1");
                        x = 2;
                    }
            break;
            case 1:
                print ("d2");
                if(location.y < 0){
                    x=3;
                }
             break;
            case 2:
                print ("x2");
                if(location.y>0){
                    x=1;
                }
            break;
            case 3:
                print("down");
            break;
            }
        }else x=0;
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进