Unity——EasyTouch摇杆插件使用


EasyTouch摇杆插件使用

Demo展示

Unity——EasyTouch摇杆插件使用_插件使用


双指缩放在电脑端无法演示,功能是做了的;

插件名叫EasyTouch,有需要给我留言,不想开仓库传了;

创建摇杆点这里:

Unity——EasyTouch摇杆插件使用_初始化_02

初始化

On_JoystickMove这个回调是用来控制移动的;分两个方向,x和y轴;

登录后复制

void OnEnable()
{    
    EasyJoystick.On_JoystickMove += OnJoystickMove;
}

private void OnDisable()
{
    EasyJoystick.On_JoystickMove -= OnJoystickMove;
}

private void OnDestroy()
{
    EasyJoystick.On_JoystickMove -= OnJoystickMove;
}

自定义On_JoystickMove

我的摇杆物体名称叫做MoveControl;

登录后复制

void OnJoystickMove(MovingJoystick move)
{
    if (move.joystickName != "MoveControl") 
    {
        return;
    }

    float PositionX = move.joystickAxis.x; //获取摇杆偏移摇杆中心的x坐标
    float PositionY = move.joystickAxis.y; //获取摇杆偏移摇杆中心的y坐标

    Vector2 value = move.joystickValue;
    if (PositionY != 0 || PositionX != 0)
    {
        //设置控制角色或物体方块的朝向(当前坐标+摇杆偏移量)
        //transform.LookAt(new Vector3(transform.position.x + PositionX, transform.position.y,transform.position.z + PositionY));
        //移动角色或物体的位置(按其所朝向的位置移动)
        Vector3 dir = new Vector3(PositionX, PositionY, 0);
        
        transform.Translate(dir * Time.deltaTime * speed);
    }
}

Unity——EasyTouch摇杆插件使用_摇杆_03

这两个物体控制摇杆,EasyTouch一定不能删除

摇杆物体属性界面,xy轴速度要修改,不能为0;

手势功能

这套插件还支持双指缩放,滑动,手指画圈,点击,多指触碰,拖拽等;

具体api看注释:

登录后复制

public class Zoom : MonoBehaviour
{
    bool isStart = false;
    public Button BtnReset;
    public Button BtnLoadScene;
    public Text log;
    void OnEnable() {  
        EasyTouch.On_Swipe += On_Swipe;  
        EasyTouch.On_Drag += On_Drag;  
        EasyTouch.On_Swipe2Fingers += On_Swipe2Fingers;  
        EasyTouch.On_PinchEnd += On_PinchEnd;
        EasyTouch.On_PinchIn += On_PinchIn;
        EasyTouch.On_PinchOut += On_PinchOut;
        BtnReset.onClick.AddListener(ResetScene);  
        BtnLoadScene.onClick.AddListener(OnLoadScene);
    }  
 
    void OnDisable() {  
        EasyTouch.On_Swipe -= On_Swipe;  
        EasyTouch.On_Drag -= On_Drag;  
        EasyTouch.On_Swipe2Fingers -= On_Swipe2Fingers;  
        EasyTouch.On_PinchEnd -= On_PinchEnd;  
        EasyTouch.On_PinchIn -= On_PinchIn;
        EasyTouch.On_PinchOut -= On_PinchOut;
        BtnReset.onClick.RemoveListener(ResetScene);  
    }  
 
    void OnDestroy() {  
        EasyTouch.On_Swipe -= On_Swipe;  
        EasyTouch.On_Drag -= On_Drag;  
        EasyTouch.On_Swipe2Fingers -= On_Swipe2Fingers;  
        EasyTouch.On_PinchEnd -= On_PinchEnd;  
        EasyTouch.On_PinchIn -= On_PinchIn;
        EasyTouch.On_PinchOut -= On_PinchOut;
        BtnReset.onClick.RemoveListener(ResetScene);  
    }  
 
    /// <summary>  
    /// 重置cube和相机  
    /// </summary>  
    void ResetScene() {  
        transform.position = Vector3.zero;  
        transform.rotation = Quaternion.Euler(Vector3.zero);  
        Camera.main.transform.position = new Vector3(0, 0, -10);  
    }  
 
    /// <summary>  
    /// 滑动使cube旋转  
    /// </summary>  
    /// <param name="ges"></param>  
    void On_Swipe(Gesture ges) {  
        Vector3 vec = new Vector3(ges.deltaPosition.y, ges.deltaPosition.x, 0);  
        transform.Rotate(vec ,Space.World);  
        log.text = "滑动使cube旋转";
    }  
 
    /// <summary>  
    /// 拖拽移动cube  
    /// </summary>  
    /// <param name="ges"></param>  
    void On_Drag(Gesture ges) {  
        if (ges.pickObject == gameObject) {  
            transform.position = ges.GetTouchToWordlPoint(10);//相机z=-10 cube 0  
            log.text = "拖拽移动cube";
        }  
    }  
 
    /// <summary>  
    /// 双指滑动 平面移动相机  
    /// </summary>  
    /// <param name="ges"></param>  
    void On_Swipe2Fingers(Gesture ges) 
    {  
        Camera.main.transform.Translate(new Vector3(-ges.deltaPosition.x*Time.deltaTime*25,
            -ges.deltaPosition.y*Time.deltaTime*25, 0));  
        log.text = "双指滑动移动相机";
    }

    /// <summary>  
    /// 拉近拉远相机  
    /// </summary>  
    /// <param name="ges"></param>  
    private void On_PinchIn(Gesture gesture){
 
        if (gesture.pickObject == gameObject){
             
            float zoom = Time.deltaTime * gesture.deltaPinch;
            isStart = true;
            Vector3 scale = transform.localScale ;
            transform.localScale = new Vector3( scale.x - zoom, scale.y -zoom, scale.z-zoom);
        }  
    }
 
    private void On_PinchOut(Gesture gesture){
        if (gesture.pickObject == gameObject){
            float zoom = Time.deltaTime * gesture.deltaPinch;
            isStart = true;
            Vector3  scale = transform.localScale ;
            transform.localScale = new Vector3( scale.x + zoom, scale.y +zoom,scale.z+zoom);
 
        }
    }
    private void On_PinchEnd(Gesture gesture){
        if(isStart ){
            if (gesture.pickObject == gameObject){
                print ("End");
            }
        }
    }  

    void OnLoadScene()
    {
        SceneManager.LoadScene("TestJoyStick");
    }
}








免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空