usingUnityengine;usingSystem.Collections.Generic;usingDG. tweening;usingUnityengine.eventsystems;usingUnityengine.ui; Public classTestui:monobehaviour, Ipointerclickhandler, Ipointerdownhandler, Ipointeruphandler, IDragHandler, Ienddraghandler, Ipointerenterhandler, ipointerexithandler{//private Graphicraycaster Graphicraycaster; //private EventSystem EventSystem; //private Recttransform Image; PublicRecttransform Canvas;//get the Ugui coordinates of the canvas PrivateRecttransform Imgrect;//get the Ugui coordinates of the pictureVector2 offset =NewVector3 ();//to get the difference between a mouse and a pictureVector3 Imgreducescale =NewVector3 (0.7f,0.7f,1);//Set Picture ZoomVector3 Imgnormalscale =NewVector3 (1,1,1);//Normal Size voidStart () {//Graphicraycaster = Gameobject.find ("Canvas"). Getcomponent<graphicraycaster> (); //EventSystem = Gameobject.find ("EventSystem"). Getcomponent<eventsystem> (); //Image = Gameobject.find ("Canvas/image"). Getcomponent<recttransform> (); //setrecttransformsize (Image, New Vector2 (2, 2));Imgrect= getcomponent<recttransform>(); } //Update is called once per frame voidUpdate () {//if (checkguiraycastobjects ()) return; //Debug.Log (EventSystem.current.gameObject.name); //if (input.getmousebuttondown (0))//{ //if (EventSystem.current.IsPointerOverGameObject ())// { //Debug.Log ("Current Touch on UI"); //Debug.logerror (EventSystem.current.IsPointerOverGameObject ()); // } //Else//Debug.Log ("Currently no touch on the UI"); //} } BOOLcheckguiraycastobjects () {pointereventdata eventData=NewPointereventdata (EventSystem); Eventdata.pressposition=input.mouseposition; Eventdata.position=input.mouseposition; List<RaycastResult> list =NewList<raycastresult>(); Graphicraycaster.getcomponent<GraphicRaycaster>(). Raycast (EventData, list); Debug.Log (list. Count); returnList. Count >0; } Public voidOnpointerclick (Pointereventdata eventData) {//if (Eventdata.clickcount = = 2)//{ //This.transform.DOScale (New Vector3 (1.5f, 1.5f, 1.5f), 0.5f); //} //if (Eventdata.clickcount = = 1)//{ //This.transform.DOScale (Vector3.one, 0.5f); //} } Private voidSetinsetandsizefromparentedge (Gameobject obj) {varRT = Obj.gameobject.getcomponent<recttransform>(); Rt. Setinsetandsizefromparentedge (RectTransform.Edge.Right,0, -); Rt. Setinsetandsizefromparentedge (RectTransform.Edge.Bottom,0, -); } Public Static voidsetrecttransformsize (Recttransform trans, Vector2 newSize) {Vector2 oldsize=trans.rect.size; Vector2 deltasize= NewSize-oldsize; Trans.offsetmin= Trans.offsetmin-NewVector2 (deltasize.x * trans.pivot.x, DELTASIZE.Y *trans.pivot.y); Trans.offsetmax= Trans.offsetmax +NewVector2 (deltasize.x * (1f-trans.pivot.x), Deltasize.y * (1f-trans.pivot.y)); } //call the interface corresponding to Ipointerdownhandler when the mouse is pressed Public voidOnpointerdown (Pointereventdata eventData) {Vector2 MouseDown= Eventdata.position;//Record screen coordinates when the mouse is pressedVector2 Mouseuguipos =NewVector2 ();//defines the Ugui coordinates of a receive return//recttransformutility.screenpointtolocalpointinrectangle (): Convert screen coordinates to UGUI coordinates//Canvas: Coordinates to which object to convert, here IMG parent class is canvas, we use canvas//Eventdata.entereventcamera: Which camera does this event do?//Out Mouseuguipos: Returns the converted Ugui coordinates//Isrect: Method returns a bool value to determine if the mouse pressed point is on the object being converted BOOLIsrect = Recttransformutility.screenpointtolocalpointinrectangle (Canvas, MouseDown, Eventdata.entereventcamera, outMouseuguipos); if(Isrect)//if the { //calculates the difference between the center of the picture and the mouse pointoffset = imgrect.anchoredposition-Mouseuguipos; } } //call the corresponding interface when the mouse is lifted Ipointeruphandler Public voidOnpointerup (Pointereventdata eventData) {//offset = Vector2.zero; } //call the corresponding interface when the mouse is dragged Idraghandler Public voidOndrag (Pointereventdata eventData) {Vector2 Mousedrag= Eventdata.position;//screen coordinates when the mouse is draggedVector2 Uguipos =NewVector2 ();//used to receive the converted drag coordinates//similar to the above BOOLIsrect = Recttransformutility.screenpointtolocalpointinrectangle (Canvas, Mousedrag, Eventdata.entereventcamera, outUguipos); if(isrect) {//sets the Ugui coordinates of the picture to be the same as the Ugui coordinates of the mouseImgrect.anchoredposition = offset +Uguipos; } } //call the corresponding interface when the mouse is over drag ienddraghandler Public voidOnenddrag (Pointereventdata eventData) {//offset = Vector2.zero; } //call the corresponding interface when the mouse enters the picture Ipointerenterhandler Public voidOnpointerenter (Pointereventdata eventData) {//Imgrect.localscale = Imgreducescale;//Zoom Out Picture } //call the corresponding interface when the mouse exits the picture Ipointerexithandler Public voidonpointerexit (Pointereventdata eventData) {//Imgrect.localscale = Imgnormalscale;//Reply to Picture }}
Implementation of Ugui, drag-and-drop interface events in Unity