Unity——EasyTouch搖桿外掛使用

小紫蘇xw發表於2021-10-21

EasyTouch搖桿外掛使用

Demo展示

雙指縮放在電腦端無法掩飾,豎屏將就看看吧;

外掛名叫EasyTouch,有需要給我留言,不想開倉庫傳了;

建立搖桿點這裡:

image-20211021225546856

初始化

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

image-20211021224653380

這兩個物體控制搖桿,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");
    }
}

相關文章