Unity Asset(3)虚拟摇杆插件 EasyTouch

来源:互联网 发布:上海盘石软件电话 编辑:程序博客网 时间:2024/05/17 01:48

Unity Asset(3)虚拟摇杆插件 EasyTouch

对于移动平台上的RPG类的游戏,我们常用虚拟摇杆来控制人物角色的行走和一些行为.

当然unity也自带了摇杆Joystick,用起来也简单,但存在不少局限,不会满足普通mmo游戏的需求,比如指定显示区域或者是更改一些素材等等,而这些EasyTouch插件都已经帮你实现.

下面我将一个简单的实例,和该插件提供的例子来介绍这个插件。


一、一个简单的实例

1、import“EasyTouch”资源包。
2、创建空物体,命名为EasyTouch(当然你也可以改成其他名字)。
3、添加EasyTouch.cs脚本在刚刚创建的空物体(EasyTouch)上。
4、选择改物体但不要将BroadcastMessages勾选。
5、创建一个新的C#脚本,命名MyFirstTouch。
6、添加这些方法。


MyFirstTouch.cs

using UnityEngine;using System.Collections;public class MyFirstTouch : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}// Subscribe to eventsvoid OnEnable(){EasyTouch.On_TouchStart += On_TouchStart;}// Unsubscribevoid OnDisable(){EasyTouch.On_TouchStart -= On_TouchStart;}// Unsubscribevoid OnDestroy(){EasyTouch.On_TouchStart -= On_TouchStart;}// Touch start eventpublic void On_TouchStart(Gesture gesture){Debug.Log( "Touch in " + gesture.position);}}



7、再创建一个空物体,命名为Receiver。
8、将MyFirstTouch脚本添加到空物体Receiver上。
9、运行并且点击,会发现控制台打印了当前按下的坐标。





二、Example1

Example for EasyButton


该例子效果图如下:







Bullet.cs

using UnityEngine;using System.Collections;public class Bullet : MonoBehaviour {private float startTime;// Use this for initializationvoid Start () {startTime = Time.realtimeSinceStartup;}// Update is called once per framevoid Update () {if (Time.realtimeSinceStartup-startTime<2){transform.Translate(new Vector3(0,0,1) * 0.8f * Time.deltaTime);}else{Destroy(gameObject);}}}

ETPlayer.cs

using UnityEngine;using System.Collections;public class ETPlayer : MonoBehaviour {public GameObject bullet;private Transform model;private Transform gun;void OnEnable(){EasyJoystick.On_JoystickMove += On_JoystickMove;EasyJoystick.On_JoystickMoveEnd += On_JoystickMoveEnd;//EasyButton.On_ButtonPress += On_ButtonPress;EasyButton.On_ButtonUp += On_ButtonUp;//EasyButton.On_ButtonDown += On_ButtonDown;}void Fire(){//if (buttonName=="Fire"){Instantiate( bullet, gun.transform.position, gun.rotation);//}}void OnDisable(){EasyJoystick.On_JoystickMove -= On_JoystickMove;EasyJoystick.On_JoystickMoveEnd -= On_JoystickMoveEnd;//EasyButton.On_ButtonPress -= On_ButtonPress;EasyButton.On_ButtonUp -= On_ButtonUp;}void OnDestroy(){EasyJoystick.On_JoystickMove -= On_JoystickMove;EasyJoystick.On_JoystickMoveEnd -= On_JoystickMoveEnd;//EasyButton.On_ButtonPress -= On_ButtonPress;EasyButton.On_ButtonUp -= On_ButtonUp;}void Start(){model = transform.FindChild("Model").transform;gun = transform.FindChild("Gun").transform;}void On_JoystickMove( MovingJoystick move){float angle = move.Axis2Angle(true);transform.rotation  = Quaternion.Euler( new Vector3(0,angle,0));transform.Translate( Vector3.forward * move.joystickValue.magnitude * Time.deltaTime);model.animation.CrossFade("Run");}void On_JoystickMoveEnd (MovingJoystick move){model.animation.CrossFade("idle");}/*void On_ButtonPress (string buttonName){if (buttonName=="Fire"){Instantiate( bullet, gun.transform.position, gun.rotation);}}*/void On_ButtonUp (string buttonName){if (buttonName=="Exit"){Application.LoadLevel("StartMenu");}}}

三、Example2

FirstPerson-DirectMode-DoubleJoystick

效果图:




其中Exit.cs如下。很好理解:


using UnityEngine;using System.Collections;public class ExitButton : MonoBehaviour {void OnEnable(){EasyButton.On_ButtonUp += On_ButtonUp;}void OnDisable(){EasyButton.On_ButtonUp -= On_ButtonUp;}void OnDestroy(){EasyButton.On_ButtonUp -= On_ButtonUp;}void On_ButtonUp (string buttonName){Application.LoadLevel("StartMenu");}}

四、Examp3


ThirdPerson-DirectEventMode-DoubleJoystick

效果图:







JoystickEvent.cs

using UnityEngine;using System.Collections;/// <summary>/// Joystick event/// </summary>public class JoystickEvent : MonoBehaviour {void OnEnable(){EasyJoystick.On_JoystickMove += On_JoystickMove;EasyJoystick.On_JoystickMoveEnd += On_JoystickMoveEnd;}void OnDisable(){EasyJoystick.On_JoystickMove -= On_JoystickMove;EasyJoystick.On_JoystickMoveEnd -= On_JoystickMoveEnd;}void OnDestroy(){EasyJoystick.On_JoystickMove -= On_JoystickMove;EasyJoystick.On_JoystickMoveEnd -= On_JoystickMoveEnd;}void On_JoystickMoveEnd(MovingJoystick move){if (move.joystickName == "Move_Turn_Joystick"){animation.CrossFade("idle");}}void On_JoystickMove( MovingJoystick move){if (move.joystickName == "Move_Turn_Joystick"){//if (Mathf.Abs(move.joystickAxis.y)>0 && Mathf.Abs(move.joystickAxis.y)<0.5){animation.CrossFade("walk");}else if (Mathf.Abs(move.joystickAxis.y)>=0.5){animation.CrossFade("run");}}}}


五、手势识别

TouchStart.cs

using UnityEngine;using System.Collections;public class TouchStart : MonoBehaviour {private TextMesh textMesh;// Subscribe to eventsvoid OnEnable(){EasyTouch.On_TouchStart += On_TouchStart;EasyTouch.On_TouchDown += On_TouchDown;EasyTouch.On_TouchUp += On_TouchUp;}void OnDisable(){UnsubscribeEvent();}void OnDestroy(){UnsubscribeEvent();}void UnsubscribeEvent(){EasyTouch.On_TouchStart -= On_TouchStart;EasyTouch.On_TouchDown -= On_TouchDown;EasyTouch.On_TouchUp -= On_TouchUp;}void Start () {textMesh =(TextMesh) transform.Find("TexttouchStart").transform.gameObject.GetComponent("TextMesh");}// At the touch beginning public void On_TouchStart(Gesture gesture){// Verification that the action on the objectif (gesture.pickObject == gameObject)gameObject.renderer.material.color = new Color( Random.Range(0.0f,1.0f),  Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));}// During the touch is downpublic void On_TouchDown(Gesture gesture){// Verification that the action on the objectif (gesture.pickObject == gameObject)textMesh.text = "Down since :" + gesture.actionTime.ToString("f2");}// At the touch endpublic void On_TouchUp(Gesture gesture){// Verification that the action on the objectif (gesture.pickObject == gameObject){gameObject.renderer.material.color = new Color( 1f,1f,1f);textMesh.text ="Touch Start/Up";}}}

点击事件。Tap.cs

using UnityEngine;using System.Collections;public class Tap : MonoBehaviour {// Subscribe to eventsvoid OnEnable(){EasyTouch.On_SimpleTap += On_SimpleTap;}void OnDisable(){UnsubscribeEvent();}void OnDestroy(){UnsubscribeEvent();}void UnsubscribeEvent(){EasyTouch.On_SimpleTap -= On_SimpleTap;}// Simple tapprivate void On_SimpleTap( Gesture gesture){// Verification that the action on the objectif (gesture.pickObject == gameObject){gameObject.renderer.material.color = new Color( Random.Range(0.0f,1.0f),  Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));}}}

双击事件。DoubleTap.cs

using UnityEngine;using System.Collections;public class DoubleTap : MonoBehaviour {// Subscribe to eventsvoid OnEnable(){EasyTouch.On_DoubleTap += On_DoubleTap;}void OnDisable(){UnsubscribeEvent();}void OnDestroy(){UnsubscribeEvent();}void UnsubscribeEvent(){EasyTouch.On_DoubleTap -= On_DoubleTap;}// Double tap  private void On_DoubleTap( Gesture gesture){// Verification that the action on the objectif (gesture.pickObject == gameObject){ gameObject.renderer.material.color = new Color( Random.Range(0.0f,1.0f),  Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));}}}

长按事件。LongTap.cs

using UnityEngine;using System.Collections;public class LongTap : MonoBehaviour {private TextMesh textMesh;// Subscribe to eventsvoid OnEnable(){EasyTouch.On_LongTapStart += On_LongTapStart;EasyTouch.On_LongTap += On_LongTap;EasyTouch.On_LongTapEnd += On_LongTapEnd;}void OnDisable(){UnsubscribeEvent();}void OnDestroy(){UnsubscribeEvent();}void UnsubscribeEvent(){EasyTouch.On_LongTapStart -= On_LongTapStart;EasyTouch.On_LongTap -= On_LongTap;EasyTouch.On_LongTapEnd -= On_LongTapEnd;}void Start(){textMesh = transform.Find("TextLongTap").transform.gameObject.GetComponent("TextMesh") as TextMesh;}// At the long tap beginning private void On_LongTapStart( Gesture gesture){// Verification that the action on the objectif (gesture.pickObject==gameObject){gameObject.renderer.material.color = new Color( Random.Range(0.0f,1.0f),  Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));}}// During the long tap private void On_LongTap( Gesture gesture){// Verification that the action on the objectif (gesture.pickObject==gameObject){textMesh.text = gesture.actionTime.ToString("f2");}}// At the long tap endprivate void On_LongTapEnd( Gesture gesture){// Verification that the action on the objectif (gesture.pickObject==gameObject){gameObject.renderer.material.color = Color.white;textMesh.text="Long tap";}}}

拖拽事件。Drag.cs

using UnityEngine;using System.Collections;public class Drag : MonoBehaviour {private TextMesh textMesh;private Vector3 deltaPosition;// Subscribe to eventsvoid OnEnable(){EasyTouch.On_Drag += On_Drag;EasyTouch.On_DragStart += On_DragStart;EasyTouch.On_DragEnd += On_DragEnd;}void OnDisable(){UnsubscribeEvent();}void OnDestroy(){UnsubscribeEvent();}void UnsubscribeEvent(){EasyTouch.On_Drag -= On_Drag;EasyTouch.On_DragStart -= On_DragStart;EasyTouch.On_DragEnd -= On_DragEnd;}void Start(){textMesh = transform.Find("TextDrag").transform.gameObject.GetComponent("TextMesh") as TextMesh;}// At the drag beginning void On_DragStart( Gesture gesture){// Verification that the action on the objectif (gesture.pickObject == gameObject){gameObject.renderer.material.color = new Color( Random.Range(0.0f,1.0f),  Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));// the world coordinate from touch for z=5Vector3 position = gesture.GetTouchToWordlPoint(5);deltaPosition = position - transform.position;}}// During the dragvoid On_Drag(Gesture gesture){// Verification that the action on the objectif (gesture.pickObject == gameObject){// the world coordinate from touch for z=5Vector3 position = gesture.GetTouchToWordlPoint(5);transform.position = position - deltaPosition;// Get the drag anglefloat angle = gesture.GetSwipeOrDragAngle();textMesh.text = gesture.swipe.ToString() + " / angle :" + angle.ToString("f2");}}// At the drag endvoid On_DragEnd(Gesture gesture){// Verification that the action on the objectif (gesture.pickObject == gameObject){transform.position= new Vector3(3f,1.8f,-5f);gameObject.renderer.material.color = Color.white;textMesh.text="Drag me";}}}


0 0