Recently in a scrollview under a single item to implement long press the other effects appear. You can do this normally on the Ngui. But there are problems on the 2D toolkit. Sliding ScrollView on Ngui is actually dragging each item to achieve the drag effect. The 2D toolkit on the ScrollView has a large collision body. By triggering the collision body to implement the drag. The difference here is that when you want to implement a long press on the item on the 2D toolkit, it is obscured by the collision of the front scrollview. This is when I use a ray to trigger the long press function. (In fact, the button is triggered by radiation) Ray trigger There is a problem is the level. There may be a lot of collisions in front of the button you want to trigger, so what we need to figure out is how to find what you want in the case of many colliding bodies. Here, let's start with the whole logic and then look at the code. First Ray is required to shoot each frame, so we write Ray logic in the update function. The Ray function Physics.raycast (NewRay, out Hitinfo, and _layermask) returns the first collision in the Ray. So what we're going to do is we first shoot the first ray through the screen, and when the Ray hits the collision body, it's what we need. If not, we can obtain the point behind this collision body by the point of collision and the width of the collision body in the hitinfo of this function output and the collider.size.z of the collision body. At this new point we are emitting a ray in the screen to detect the next collision until we find the point we want. Here we can find it by hand.
When triggered, the Vector3 is captured according to the point of the click, and the X, Y is saved so that he can continue to find the gameobject according to the coordinates.
When it is found, it makes a click effect, so here is a script that notifies the listener like an event Action,clickhandler
You can see that a process has been opened below. The function of this process is to determine whether the button is consistently pressed by the time Gap longpressintervaltime.
If the button is not released during this time, a long-press event is triggered. And now you need to add the logic to release the button. is judged by the rays.
This is based on the points and target Gameobject that were previously retained to determine whether you have clicked
Found releasing the trigger release function
This completes the click of a button. Loosen. and long press functions. There are a variety of logic constraints and judgments in the middle. Specific code to see the script. Using Unityengine;
Using System.Collections;
Using System;
public class Raycastgameobject:monobehaviour {
Public Action ClickHandler;
Public Action Releasehandler;
Public Action Longpresshandler;
Public Gameobject targetgameobject = null;
public float longpressintervaltime = 1.5f;
public string layername = "";
BOOL _itempressing = false;
BOOL _checkingpressing = false;
Vector3 _raycaseoriginvector3 = new Vector3 ();
Layermask _layermask;
F_panel _panel;
void Awake ()
{
if (_panel = = null)
Getpanelinparent (transform);
if (Targetgameobject = = null)
Targetgameobject = Gameobject;
if (LayerName = = "")
{
_layermask = 1 << gameobject.layer;
LayerName = Layermask.layertoname (Gameobject.layer);
}
Else
_layermask = 1 << layermask.nametolayer (layername);
}
Update is called once per frame
void Update ()
{
if (_panel = = null)
{
Getpanelinparent (transform);
}
Else
{
if (!_checkingpressing && input.getmousebutton (0) && PopUpManager.Get.CurrentPopup.name = = _ Panel.name)
{
_checkingpressing = true;
_raycaseoriginvector3 = new Vector3 (input.mouseposition.x, INPUT.MOUSEPOSITION.Y, 0);
Raycastgameobjectclick (Camera.main.ScreenPointToRay (input.mouseposition), targetgameobject);
}
else if (_itempressing)
{
Raycastgameobjectrelease (New Ray (_raycaseoriginvector3, New Vector3 (0, 0, 1)), targetgameobject);
}
}
}
void Raycastgameobjectclick (Ray Ray, Gameobject TargetObject)
{
Ray NewRay = ray;
Raycasthit Hitinfo;
if (Physics.raycast (NewRay, out Hitinfo, _layermask))
{
Debug.drawline (Newray.origin, hitinfo.point);
if (HitInfo.collider.gameObject = = null)
{
_checkingpressing = false;
Return
}
else if (hitInfo.collider.gameObject! = targetObject)
{
_raycaseoriginvector3 = Hitinfo.point;
Vector3 newpoint = new Vector3 (Hitinfo.point.x, Hitinfo.point.y, Hitinfo.point.z + hitinfo.transform.getcomponent< Boxcollider> (). size.z);
Raycastgameobjectclick (New Ray (Newpoint, New Vector3 (0, 0, 1)), targetObject);
}
Else
{
Onitempressedenter ();
Return
}
}
Else
{
_checkingpressing = false;
}
}
void Raycastgameobjectrelease (Ray Ray, Gameobject TargetObject)
{
if (Input.getmousebutton (0))
{
Ray NewRay = ray;
Raycasthit Hitinfo;
if (Physics.raycast (NewRay, out Hitinfo, _layermask))
{
Debug.drawline (Newray.origin, hitinfo.point);
Gameobject gameobj = HitInfo.collider.gameObject;
if (gameobj! = targetObject)
Onitempressedend ();
}
}
Else
{
Onitempressedend ();
}
}
void Onitempressedenter ()
{
_itempressing = true;
if (ClickHandler! = null)
ClickHandler ();
Startcoroutine (Waitandcheckpress (longpressintervaltime));
}
void Onitempressedend ()
{
_checkingpressing = false;
_itempressing = false;
if (Releasehandler! = null)
Releasehandler ();
}
IEnumerator waitandcheckpress (float seconds)
{
Yield return new waitforseconds (seconds);
if (_itempressing)
{
if (PopUpManager.Get.CurrentPopup.name = = _panel.name)
Longpresshandler ();
}
}
void Getpanelinparent (Transform _t)
{
if (_t.parent! = null)
{
F_panel Getpanel = _t.parent.getcomponent<f_panel> ();
if (Getpanel = = null)
{
Getpanelinparent (_t.parent);
}
Else
{
_panel = Getpanel;
}
}
}}
"Komatsu teaches you to develop" unity practical skills "RAY trigger button