Unity3D easytouch虚拟摇杆及其应用

1、easytouch的下载地址
2、设置joystick游戏物体

3、joystick.cs(绑定到joystick游戏物体)

using UnityEngine;
using System.Collections;

public class joystick : MonoBehaviour {

    protected Transform m_player;

    void Awake()
    {
        GameObject obj=GameObject.FindGameObjectWithTag("Player");
        if ( obj!=null )
        {
            m_player=obj.transform;
        }
    }
    //当摇杆可用时注册事件
    void OnEnable()
    {
        EasyJoystick.On_JoystickMove += OnJoystickMove;
        EasyJoystick.On_JoystickMoveEnd += OnJoystickMoveEnd;
    }

    //当摇杆不可用时移除事件
    void OnDisable()
    {
        EasyJoystick.On_JoystickMove -= OnJoystickMove;
        EasyJoystick.On_JoystickMoveEnd -= OnJoystickMoveEnd;
    }

    //当摇杆销毁时移除事件
    void OnDestroy()
    {
        EasyJoystick.On_JoystickMove -= OnJoystickMove;
        EasyJoystick.On_JoystickMoveEnd -= OnJoystickMoveEnd;
    }

    //当摇杆处于停止状态时,角色进入待机状态
    void OnJoystickMoveEnd(MovingJoystick move)
    {
        if (move.joystickName == "EasyJoystick")
        {
            //animation.CrossFade("idle");
        }
    }

    //当摇杆处于移动状态时,角色开始奔跑
    void OnJoystickMove(MovingJoystick move)
    {
        if (move.joystickName != "EasyJoystick")
        {
            return;
        }
        //获取摇杆偏移量
        float joyPositionX = move.joystickAxis.x;
        float joyPositionY = move.joystickAxis.y;
        if (joyPositionY != 0 || joyPositionX != 0)
        {
            //更新组件移动位置
            m_player.Translate( new Vector3( -joyPositionX/10, 0, -joyPositionY/10 ) );
        }
    }
}

4、shoot.cs(绑定到button游戏物体)

using UnityEngine;
using System.Collections;

public class shoot : MonoBehaviour {

    public Transform m_rocket;
    float m_rocketRate = 0;
    public AudioClip m_shootClip;
    protected Transform m_player;

    void Awake()
    {
        GameObject obj=GameObject.FindGameObjectWithTag("Player");
        if ( obj!=null )
        {
            m_player=obj.transform;
        }
    }

    //当摇杆可用时注册事件
    void OnEnable()
    {
        EasyButton.On_ButtonPress += On_ButtonPress;
        EasyJoystick.On_JoystickMoveEnd += OnJoystickMoveEnd;
    }

    //当摇杆不可用时移除事件
    void OnDisable()
    {
        EasyButton.On_ButtonPress += On_ButtonPress;
        EasyJoystick.On_JoystickMoveEnd -= OnJoystickMoveEnd;
    }

    //当摇杆销毁时移除事件
    void OnDestroy()
    {
        EasyButton.On_ButtonPress += On_ButtonPress;
        EasyJoystick.On_JoystickMoveEnd -= OnJoystickMoveEnd;
    }

    //当摇杆处于停止状态时,角色进入待机状态
    void OnJoystickMoveEnd(MovingJoystick move)
    {

    }
    //按下事件响应
    void On_ButtonPress (string buttonName)
    {

            m_rocketRate -= Time.deltaTime;
            if ( m_rocketRate <= 0 )
            {
                m_rocketRate = 0.1f;

                if (buttonName == "ShootButton") 
                {
                    Instantiate( m_rocket, m_player.position, m_player.rotation );
                    m_player.audio.PlayOneShot(m_shootClip);
                }
            }

    }
    // Use this for initialization
    void Start () {

    }
    // Update is called once per frame
    void Update () {

    }
}
5、easytouch包含实例,可供参考。

你可能感兴趣的:(unity3d)