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

到此這篇關於Unity EasyTouch搖桿插件使用的文章就介紹到這瞭,更多相關Unity EasyTouch搖桿插件內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: