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

查看:599
本文介绍了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)

解决方案

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

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

这篇关于unity3d如何用c#实现在按住鼠标左键时即时获取鼠标坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆