Chapter 54 Unity Mobile Platform Input (Part 2)

In this chapter we introduce a simulator plugin. There are many such plug-ins, such as EasyTouch, Lean Touch, Joystick Pack and so on. EasyTouch is a very widely used plug-in that supports many common functions such as click, drag and drop, and remote sensing. Unfortunately, this plugin has been discontinued.

The new plugin is supposed to be "Easy Touch Controls" by both Hedgehog Team. Of course, we have other channels to download the plug-in, here we are using Easy Touch 5 Touchscreen Virtual Controls 5.0.17.unitypackage. Based on the above project, we import this resource package.

 

To upgrade the API, we click the "I Made a Backup, Go Ahead!" button.

After the import is complete, a window will pop up, just close it.

In the Project panel, we can see the imported resource directory and files.

Next, we right-click on the blank space of the Hierarchy layer panel and select "EasyTouch Controls" -> "Joystick", and then switch our Scene to "2D" mode, because this plug-in should be written based on UGUI.

 

We can see an emulator similar to a "joystick". Note that it's a GameObject named "New Joystick" in our Hierarchy pane. Let's look at its inspector.

The most important part of this plugin is the "ETC Joystick" script component.

JoystickName: the name of the virtual joystick game object;

Activated: Whether the virtual joystick is activated and available;

Visible: whether the virtual joystick is visible;

UseFixedUpdate: Use the FixedUpdate function to update the joystick information;

Unregister at disabling time: disable the virtual joystick when logging out;

First, let's look at the "Position & Size" item below it, through which we can place the joystick in the lower left corner, the settings are as follows:

Select the "Anchor" anchor point as Bottom Left and set the Spacing to 20 px. The reason for setting the spacing is that the joystick has room to move back and forth, left and right, and we are looking at the scene view at this time.

In "Sprites", we can set the background image of the "Joystick".

Next, is the configuration of our event, which is mainly divided into four event settings: Move, Touch, Down, and Press.

1.Move Events [Move Events]

OnMoveStart,OnMove,OnMoveSpeed,OnMoveEnd

When moving the virtual joystick control button, the corresponding event is triggered step by step during the movement.

2.Touch Events [touch event]

OnTouchStart,OnTouchUp

The start operation and operation end of the manipulation button are each executed once.

3.Down Events [direction event]

OnDownUp,OnDownRight,OnDownDown,OnDownLeft

When we press the virtual joystick control button to move to the final position of up, down, left, and right, it is triggered once.

4.Press Events [press event]

OnPressUp,OnPressRight,OnPressDown,OnPressLeft

When we press the virtual joystick control button to move to the final position of up, down, left, and right, it will always be triggered .

Let's take Move as an example.

Based on the event names, we have a rough idea of ​​how these events are triggered. I will use OnMove as an example. Let's create a "MoveScript.cs" script file and create the OnMoveTest method in it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveScript : MonoBehaviour
{
    public void OnMoveTest(Vector2 v)
    {

        Debug.Log("X=" + v.x);
        Debug.Log("Y=" + v.y);
    }
}

Then, we attach the script to the "Main Camera" before configuring the OnMove event.

Here, we cancel the previous "TouchScript.cs" script (the script used in the previous chapter)

Next, we can run the project directly (without going to the mobile phone to run the test), and then drag the joystick to test.

There is no need to explain the code, X and Y represent two directions, and their values ​​​​are between -1 and 1. If it is the X-axis, a positive number means moving to the right, and a negative number means moving to the left; if it is a Y-axis, a positive number means moving up, and a negative number means moving down. About the Easy Touch plug-in, we will introduce it here. For more content, you can go to the EasyTouchBundle folder under the Asset directory to view its official documents and cases. It is divided into two parts, EasyTouch and EasyTouchControls, each part contains Documentation documents and Examples cases, you can take a look by yourself.

The content involved in this course has been shared to Baidu Netdisk: https://pan.baidu.com/s/1e1jClK3MnN66GlxBmqoJWA?pwd=b2id

Guess you like

Origin blog.csdn.net/konkon2012/article/details/130613547