Unity逻辑代码开发介绍_屏幕触控事件封装

下面是我封装的一个获取触控事件的类,使用的时候在管理类中,new一个类,然后在update调用就好了:

 

using UnityEngine;
using System.Collections;

public class MouseStatus {

    private bool[] _mouseDown = new bool[3];

    private bool[] _mouseJustDown = new bool[3];
    private bool[] _mouseJustUp = new bool[3];

    private bool _mousePosInit = false;
    private Vector3 _mousePos;
    private Vector3 _mouseLastPos;
    private Vector3[] _mouseJustDownPos = new Vector3[3];
    private Vector3[] _mouseJustUpPos = new Vector3[3];

    public enum KEY
    {
        LEFT,
        RIGHT,
        MIDDLE,
    }

    public MouseStatus()
    {
    }

    // Update is called once per frame
    public void Update () {

        for(int key = 0; key < 3; ++key)
        {
            _mouseDown[key] = Input.GetMouseButton(key);
            _mouseJustDown[key] = Input.GetMouseButtonDown(key);
            _mouseJustUp[key] = Input.GetMouseButtonUp(key);

            if(_mouseJustDown[key])
            {
                _mouseJustDownPos[key] = Input.mousePosition;
            }
            if(_mouseJustUp[key])
            {
                _mouseJustUpPos[key] = Input.mousePosition;
            }
        }

        if (!_mousePosInit) {
            _mousePos = Input.mousePosition;
            _mouseLastPos = _mousePos;
        } else {
            _mouseLastPos = _mousePos;
            _mousePos = Input.mousePosition;
        }
        _mousePosInit = true;


//        Debug.Log (_mouseJustUp[(int)KEY.LEFT]);
    }

    public bool GetMouseDown(KEY key = KEY.LEFT)
    {
        return _mouseDown[(int)key];
    }

    public bool GetMouseJustDown(KEY key = KEY.LEFT)
    {
        return _mouseJustDown[(int)key];
    }

    public bool GetMouseJustUp(KEY key = KEY.LEFT)
    {
        return _mouseJustUp[(int)key];
    }

    public Vector3 GetMouseLastPos()
    {
        return _mouseLastPos;
    }
    
    public Vector3 GetMousePos()
    {
        return _mousePos;
    }

    public Vector3 GetMouseJustDownPos(KEY key = KEY.LEFT)
    {
        return _mouseJustDownPos[(int)key];
    }
    
    public Vector3 GetMouseJustUpPos(KEY key = KEY.LEFT)
    {
        return _mouseJustUpPos[(int)key];
    }
    

    public static bool UnprojectMousePosition(out Vector3 worldPosition, Vector3 mousePosition)
    {
        GameObject mainCamera = (GameObject)GameObject.Find ("Main Camera");
        CameraControl cameraControl = mainCamera.GetComponent<CameraControl> ();
        bool result = cameraControl.ProjectScreenPointToPlane (out worldPosition, mousePosition, 0);
        return result;

    }

}
 

 

如有错误,大家可以一起交流

 联系方式 qq: 940299880

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值