Backpack system events, beginners

There are four interface classes that are indispensable for the backpack system. These four interface classes are required for dragging and exchanging items.

1、IInitializePotentialDragHandler

2、INBeginDragHandler

3、IDragHandler

4、IEndDragHandler

The implementation class must inherit 4 interfaces and override their methods

public class bags : MonoBehaviour,IInitializePotentialDragHandler,IBeginDragHandler,IDragHandler, IEndDragHandler

 public void OnInitializePotentialDrag(PointerEventData eventData)
    {
        print(111);
    }


    public void OnBeginDrag(PointerEventData eventData)
    {
        print(222);
    }

    public void OnDrag(PointerEventData eventData)
    {
        print(333);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        print(444);
    }

Here I will explain in detail the meaning of the methods I implemented on the four interfaces.

void OnInitializePotentialDrag(PointerEventData eventData)方法:

It is a method that is implemented the moment the mouse clicks on the target.

void OnBeginDrag(PointerEventData eventData) Method:

It is the method called on the frame when you drag the target

void OnDrag(PointerEventData eventData) method:

It is a method called when you drag the target. If you hold down the left button while dragging, this method will not be implemented. This method can only be called when dragging.

void OnEndDrag(PointerEventData eventData) method:

It is the method called on the frame when you release the mouse.

void OnInitializePotentialDrag(PointerEventData eventData)

Everyone can find that the parameters of the four methods are all PointerEventData eventData, and the content represented by the four parameters is also the same. My understanding here is: eventData is the object clicked by the mouse, which is the mouse event. Here is a very simple example.

public class ChangePicture : MonoBehaviour,IInitializePotentialDragHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    private Image pictures;
    private Image img;
    public void OnInitializePotentialDrag(PointerEventData eventData)
    {
        //判断点击对象
        if (eventData.pointerEnter.gameObject)
        {
            //获取点击对象的Image组件,去进行图片切换
            img = eventData.pointerEnter.gameObject.GetComponent<Image>();
            pictures.sprite = img.sprite;//图片切换
        }
    }


    public void OnBeginDrag(PointerEventData eventData)
    {
        print(222);
    }

    public void OnDrag(PointerEventData eventData)
    {
        print(333);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        print(44);
    }


    // Start is called before the first frame update
    void Start()
    {
        pictures = GameObject.Find("phone").GetComponent<Image>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

if (eventData.pointerEnter.gameObject)
        {
            img = eventData.pointerEnter.gameObject.GetComponent<Image>();
            pictures.sprite = img.sprite;
        }

This if judgment is a very typical example. It will switch pictures according to the object you click.

When you click on the first gem, the white picture on the left will switch to a gem

In the same way, when you click on two elixirs, they will switch to elixirs.

 

 So the parameters in the method here correspond to the click object.

 

 

Guess you like

Origin blog.csdn.net/m0_71624363/article/details/129411233