亲宝软件园·资讯

展开

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

小紫苏xw 人气:2
想了解Unity EasyTouch摇杆插件使用示例详解的相关内容吗,小紫苏xw在本文为您仔细讲解Unity EasyTouch摇杆插件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Unity,EasyTouch摇杆插件,Unity,EasyTouch使用,下面大家一起来学习吧。

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

Demo展示

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

插件名叫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);
    }
}

这两个物体控制摇杆,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");
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论