Unity EasyTouch official case study

1. Code to detect gesture events

1. EasyTouch4.x writing

  

 

  First, manually add the EasyTouch object in the Hierarchy window, taking the touch gesture as an example, the code is as follows:

 1 using UnityEngine;
 2 using HedgehogTeam.EasyTouch;       // 注意 using
 3 
 4 public class TouchMe : MonoBehaviour {
 5 
 6     private TextMesh textMesh;
 7     private Color startColor;
 8 
 9     // 注册事件
10     void OnEnable(){
11         EasyTouch.On_TouchStart += On_TouchStart;
12         EasyTouch.On_TouchDown += On_TouchDown;
13         EasyTouch.On_TouchUp += On_TouchUp;
14     }
15  
16      void OnDisable(){
 17          UnsubscribeEvent();
 18      }
 19      
20      void OnDestroy(){
 21          UnsubscribeEvent();
 22      }
 23      
24      // Unsubscribe event 
25      void UnsubscribeEvent(){
 26          EasyTouch.On_TouchStart -= On_TouchStart;        // Touch start 
27          EasyTouch.On_TouchDown -= On_TouchDown;          // When touch is in progress 
28          EasyTouch.On_TouchUp -= On_TouchUp;              // Touch end 
29      }
30      
31      void Start () {
 32          // Get component 
33          textMesh =(TextMesh) GetComponentInChildren<TextMesh> ();
 34          startColor = gameObject.GetComponent<Renderer> ().material.color;
 35      }
 36      
37      // Touch start
 38      // Note that the time function needs to include a Gesture parameter 
39      private  void On_TouchStart(Gesture gesture){
 40          if (gesture.pickedObject == gameObject){
 41              // Random color 
42              RandomColor();
 43          }
 44     }
 45      
46      // When the touch is in progress 
47      private  void On_TouchDown(Gesture gesture){
 48          
49          // Verification that the action on the object 
50          if (gesture.pickedObject == gameObject){
 51              // Display touch event duration 
52              textMesh. text = " Down since : " + gesture.actionTime.ToString( " f2 " );
 53          }
 54  
55      }
 56      
57      // touch end 
58      private  void On_TouchUp(Gesture gesture){
59         // Verification that the action on the object
60         if (gesture.pickedObject == gameObject){
61             // 恢复原来的显示
62             gameObject.GetComponent<Renderer>().material.color = startColor;
63             textMesh.text ="Touch me";
64         }
65     }
66     
67     // 随机颜色
68     private void RandomColor(){
69         gameObject.GetComponent<Renderer>().material.color = new Color( Random.Range(0.0f,1.0f),  Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));
70     }
71 }

  This code mainly realizes that when the object is touched, the color is randomly changed and the duration of the touch event is displayed, and the original display is restored when the touch ends.

  Note: Touch refers to touching the screen with a finger on the mobile phone, which is equivalent to clicking with a mouse on the PC. Also remember to logout the event.

  There are also gesture events: Tap, Long Tap, Double tap, Drag, etc. The implementation is similar.

 

  In addition, if you want to achieve two-finger operation, you only need to add 2Fingers after the corresponding api function, such as On_TouchStart2Fingers.

  EasyTouch can simulate two-hand operation on the PC side in the following ways:

    ctrl + left mouse button click to determine the first finger position, then alt + left mouse button click to determine the second finger position.

  

2. EasyTouch5.x writing

  After 5.x, there is no need to add EasyTouch manually, the system will add it automatically, but it is still recommended to add it manually. Take the Swipe gesture as an example, the code is as follows:

1  public  class RTS_NewSyntaxe : MonoBehaviour {    
 2      void Update () {
 3          // Get the current gesture 
4          Gesture current = EasyTouch.current;
 5          if (current == null )
 6              return ;
 7  
8          // The component moves when sliding 
9          if ( current.type == EasyTouch.EvtType.On_Swipe){
 10              transform.Translate( Vector3.left * current.deltaPosition.x / Screen.width);
 11              transform.Translate( Vector3.back * current.deltaPosition.y / Screen.height);
12         }
13 }

  You can see that we can detect the gesture type in real time in the Update function. Other gesture types are implemented similarly.

 

二、QuickGesture

   

  EasyTouch5.x provides a more convenient way to realize gesture recognition QuickGesture.

  Take the gesture long press (LongTap) as an example, add the component QuickLongTap on the object that needs to add gestures, and the component properties are as follows:

  

  Among them, Name is used to find and modify the corresponding properties in the code. This component property implements changing the color of the object when you start to click on the object.

  If we need to deal with the object when clicked at the same time, then we can add another QuickLongTap component and change ActionTriggering to InPress, as follows:

  

  The corresponding control code section is as follows:

1  // Modify the color of the object 
2  public  void ChangeColor(Gesture gesture){
 3      RandomColor();
 4  }
 5      
6  // Display the duration of the press time 
7  public  void TimePressed(Gesture gesture){
 8      textMesh.text = " Down since : " + gesture.actionTime.ToString( " f2 " );
 9 }

 

 

   Of course, there are other QuickSwipe, QuickPinch (zoom), QuickEnterOverExit (note that this is for the entry and exit of the mobile phone, so it is the entry and exit in the pressed state), QuickTouch and so on.

 

Three, the core component EasyTouch

   

1. GUI compatibilty

  

  If UnityUICompatibility is unchecked, the EasyTouch event will have no effect on UGUI.

  If AutoupdatepickedUnityUI is checked, EasyTouch will update currentGessture every frame. (That is, if you do not check the currentGessture during your move, it will always be the information when you just clicked)

 

2. The next properties still do not understand TODO

 

Fourth, the virtual joystick JoyStick

   

  First create a Joystick object as shown above. The properties are as follows:

  

 

1. Position & Size

   

  The current Type is Static. If NoReturnOfTheThumb is checked, the joystick will not automatically move back to the original position after we pull it out.

  If the Type is selected as Dynamic, the properties are as follows:

  

  The joystick is not displayed by default, only when we click on the corresponding area, the joystick will appear. We can also set the area where the joystick can appear by setting the JoystickArea.

 

2. Axes properties

  

  If EnableUnityAxes is checked, we can also use the azimuth key to simulate the joystick direction on the PC side.

   If Turn & Move Direction Action is checked, the joystick will control the movement and steering of the object at the same time. We can also check Lock in jump to disable turning and moving when we jump.

  Sometimes the game object needs to run the game before it can be created, we can check the Auto link on tag name to find the controlled game object through the tag.

 

3. Camera

   

  After checking Enable tracking, we can follow the camera. We can customize the following objects, or we can find the following objects by tag.

  Camera mode has Follow and Smooth Follow. When we choose Follow, it is a simple camera follow, keeping the offset unchanged. And when we choose Smooth Follow, when the direction of our object changes, the direction of the camera will also change, so we can only see the back of the object.

 

4. Sprites

  

  We can modify the joystick to our homemade picture.

 

5. Events

  

  我们可以注册相应的事件函数,当事件发生的时候调用相应的函数。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324605309&siteId=291194637