欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity EasyTouch摇杆插件使用示例详解

程序员文章站 2022-03-04 23:20:04
easytouch摇杆插件使用,先给大家展示下效果图:demo展示双指缩放在电脑端无法掩饰,竖屏将就看看吧;插件名叫easytouch,有需要给我留言,不想开仓库传了;创建摇杆点这里:初始化on_jo...

easytouch摇杆插件使用,先给大家展示下效果图:

demo展示

Unity EasyTouch摇杆插件使用示例详解

双指缩放在电脑端无法掩饰,竖屏将就看看吧;

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

创建摇杆点这里:

Unity EasyTouch摇杆插件使用示例详解

初始化

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摇杆插件使用示例详解

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

手势功能

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

具体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");
    }
}

到此这篇关于unity easytouch摇杆插件使用的文章就介绍到这了,更多相关unity easytouch摇杆插件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!