Items Drag [U3D_RPG Game development Items Management (v)]_ game development

Source: Internet
Author: User
Before on the internet to find a lot of methods, is always not suitable for themselves, or is not used, and so on, I used the version of 2017, here "comprehensive" analysis. First create a script Inventoryitem hangs on the image that needs to be dragged, Here the image is another image of the child object, because the child's imget I used to display the object of the object of the picture, and the father of the image I used to display the picture of the lattice, so that there is the contents of the lattice, can be used to drag, and, lattice label I set as " Inventoryitemgird ", drag and drop at the end can be judged by the label is not a lattice.
1. Inheritance interface

U3d Ugui has a corresponding interface to facilitate our drag-and-drop function, in addition to the original need to inherit the Monobehaviour, we write the script to inherit three interfaces, but also to import the first file unityengine.eventsystems;

Using Unityengine.eventsystems;
public class Inventoryitem:monobehaviour, Ibegindraghandler, Idraghandler, Ienddraghandler
{
}
2. Implementing abstract methods

The abstract method is also three

Using Unityengine.eventsystems;
public class Inventoryitem:monobehaviour, Ibegindraghandler, Idraghandler, Ienddraghandler
{
    ///<summary >
    ///Onbegindrag method to implement interface, deal with the things to do when you start dragging
    ///</summary>
    ///<param name= "EventData" ></ param> public
    void Onbegindrag (Pointereventdata eventdata)
    {

    }
    ///<summary>
    /// Implement the Ondrag method of the interface, handle what to do in drag
    ///</summary>
    ///<param name= "EventData" ></param>
    The public void Ondrag (Pointereventdata eventdata)
    {

    }
    ///<summary>
    ///implements the Onenddrag method of the interface, Method at end of processing
    ///</summary>
    ///<param name= "eventdata" ></param> public
    void Onenddrag (Pointereventdata eventdata)
    {

    }
}
3. To mask
Drag and drop when we will encounter a problem, that is, drag the object of the parent is a lattice, and the lattice is located in different layers, as shown:

 I name the lattice inventory-item-gird-00 to inventory-item-gird-34 a total of 20 lattice, here the first lattice inventory-item-gird-00 in the back of the other lattice, The closer the UI object is to the camera's outer layer, the more it will be hidden from the inventory-item-gird-00 when it is dragged to the position of the inventory-item-gird-34 lattice. We're going to create a stealth mask and not a canvas on the bottom.
: Canvas, when we need to drag an item, set the parent object of the item to Canvas, drag and drop items will be located in the outermost layer, will not be any UI occlusion, it should be noted that when the drag end we need to set the parent object back, otherwise it will lead to the original lattice under the image. 
    Private Transform imageparent;//Drag objects of the parent object
    private Canvas imagefather;//get the outermost canvas
    private Vector2 drugingrecttransform;//the original position of the picture being dragged is public
    void Onbegindrag (Pointereventdata eventdata)
    {
        Imagefather = Gameobject.find ("Canvas"). Getcomponent<canvas> ()//get to the outermost lattice
        Drugingrecttransform = myimage.recttransform.position;//Get the initial position
        Imageparent = transform.parent;//Gets the transform transform of the parent object
        . SetParent (Imagefather.transform)//place items at outermost
    }
    ///<summary>///Place
    an object under another object
    ///</ summary>
    ///<param name= "Child" > as a Sub object </param>///<param name=
    "parent" > as the Parent object </ param>
    private void Setparentandposition (Transform child, Transform Parent)//to place children under parent as sub objects
    { Child
        . SetParent (parent);
        Child.position = parent.position;
    }
4. Achieve penetration
When we drag the end of the time, we need to determine whether the location of the mouse is a lattice, if it is a lattice can be put into the items, if not to let the items restore position. So the problem is, drag and drop items always in the mouse position, that is, to judge the location of the mouse to The rays are always judged by the objects we are dragging. So we let the items we're dragging are not detected by the mouse rays, that is, the objects are penetrated.
This time our items are located under the Canvas, so we just need to let Canvas to penetrate the line, first to add a component: Canvas Group

The blocks raycasts option under this component indicates whether the object will mask the mouse rays, and then we can control the switch by code.
    <summary>
    ///Onbegindrag method to implement interface, handle the things to do when you start dragging
    ///</summary>
    ///<param name= " EventData "></param> public
    void Onbegindrag (Pointereventdata eventdata)
    {
        Imagefather = Gameobject.find ("Canvas"). Getcomponent<canvas> ()//Get the outermost canvas
        Drugingrecttransform = myimage.recttransform.position;//get the initial position
        imageparent = transform.parent;//Gets the transform transform of the parent object
        . SetParent (Imagefather.transform)//Put the item on the outermost
        gameobject.find ("Canvas"). Getcomponent<canvasgroup> (). blocksraycasts = false;//rays can penetrate objects
    }
So the way to start dragging items is fully realized.
5. Goods Placement
A drag-and-drop method needs to be implemented before the item is placed:
    <summary>
    ///Implement Interface Ondrag method, handle what to do in drag
    ///</summary>
    ///<param name= "EventData" ></param> public
    void Ondrag (Pointereventdata eventdata)
    {this
        . Getcomponent<recttransform> (). position=input.mouseposition;//the left mouse button while holding and dragging, the object follows the mouse movement
    }
The last thing to do is drag and drop the end of the method. The last way to exchange items can be done without the tube, I wrote to myself, all the ideas have been very clear.
    <summary>///Onenddrag method of implementing interface, processing the method at the end///</summary>///<param name= "EventData" >& lt;/param> public void Onenddrag (Pointereventdata eventdata) {setparentandposition (transform, Imagepar ENT)/Recovery Parent Object transform.position = drugingrecttransform;//Original original exchangeofgoods (eventdata);//Exchange Items Ga Meobject.find ("Canvas").
    Getcomponent<canvasgroup> (). blocksraycasts = false;//ui event penetration: Swap for items that cannot penetrate the///<summary>///lattice  </summary>///<param name= "eventdata" > Parameter pointereventdata used to get the mouse endpoint location </param> private void Exchangeofgoods (Pointereventdata eventdata) {gameobject go = eventdata.pointercurrentraycast.gameobject;//get
            Object under the mouse//If the object is not empty and the object is an item grid if (Go!= null && go.tag.Equals ("Inventoryitemgird")) { Inventoryitemgrid Fatherofthis = this. Getcomponentinparent<inventoryitemgrid> ()//Get the script of the start grid inventorYitemgrid fatherofthat = go.
            Getcomponent<inventoryitemgrid> ()///Get the end-grid script//If the grid is empty lattice if (fatherofthat.id = 0) {Fatherofthat.setid (Fatherofthis.id,fatherofthis.num)//storage of items to the destination grid Fatherofthis.clearin FO ()//clear the original grid information} else//two lattice exchange goods {Inventoryitemgrid TEMP = Fatheroftha t;//a script Fatherofthat.setid (fatherofthis.id, fatherofthis.num) for the temporary storage of the finish line;//store items to the destination grid fatherof This.setid (Temp.id, temp.num);//store items to the beginning grid}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.