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摇杆插件内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)