<>Unity Simple backpack equipment drag and drop

describe : In a simple UI In the scene, the items in the backpack can be dragged each other , Exchange of the same equipment in the equipment column , Different equipment cannot drag each other .

1. It's through the label Tag And layer Layer To determine whether the equipment is in the backpack or in the equipment bar

2. The script is hung on the items in the backpack , Each equipment tag and item tag are different, divided into Weapon,Clothes,Wing,Shoes,

3. The layers are divided into knapsack layers Bag Equipment level Weapon. Labeling of objects passing radiographic testing Tag And layer Layer.

Hang the script on the equipment or items

Complete code sent
using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public
class Drag : MonoBehaviour,IBeginDragHandler,IEndDragHandler,IDragHandler,
ICanvasRaycastFilter{ private Transform nowparent;// Record the original coordinate position private bool
isRaycastLocationValid= true;// The default ray cannot penetrate the object public void OnBeginDrag(
PointerEventData eventData) { nowparent = transform.parent;// initial position
isRaycastLocationValid= false;// Set to penetrable } public void OnDrag(PointerEventData
eventData) { transform.position = Input.mousePosition; } public void OnEndDrag(
PointerEventData eventData) { // Get items that may exist at the end of the mouse GameObject go = eventData.
pointerCurrentRaycast.gameObject; if (go != null)// Landing position is not empty { Debug.Log(go.name);
// Print a drop point location name if (go.tag==("Gird") && go.layer.Equals(9))
// The end position of the mouse is empty grid and knapsack layer ( Judge whether to drag equipment or props to the empty object of the backpack ) { SetParentAndPosition(transform,go.
transform); } else if((go.tag.Equals("Prop") || go.tag.Equals("Weapon") || go.
tag.Equals("Clothes") || go.tag.Equals("Wing") || go.tag.Equals("Shoes")) && go.
transform.parent.gameObject.layer.Equals(9) && transform.parent.gameObject.layer
.Equals(9)) // The end of the mouse drop is also an object of the backpack, and the location of the drop point is the number of layers of the parent object , At this time, the number of layers of the parent object is also knapsack layer ( It was judged that the objects in the backpack were exchanged at will )
{ // Drag items 1 Put it under the end of the mouse SetParentAndPosition(transform,go.transform.parent);
// Put the mouse to the end of the item 2 Put it in the original item 1 The location of SetParentAndPosition(go.transform,nowparent); if (
transform.position == go.transform.position) { Debug.Log("error"); } } else if (
go.tag == transform.tag && go.layer.Equals(10))// Same label , And it's on the equipment level ( Judge whether to place equipment from the backpack to the equipment bar ) {
// Drag items 1 Put it under the end of the mouse SetParentAndPosition(transform, go.transform); } else if (go
.tag == transform.tag && transform.gameObject.layer == go.layer)
// The labels are the same and the layers are the same ( Determine the equipment exchange position in the equipment bar and the same type of equipment exchange position dragged from the equipment bar to the backpack ) { // Drag items 1 Put it under the end of the mouse
SetParentAndPosition(transform, go.transform.parent); SetParentAndPosition(go.
transform,nowparent); } else// inactive position , Return the item to its original position { SetParentAndPosition(transform,
nowparent); } } else { SetParentAndPosition(transform,nowparent); }
isRaycastLocationValid= true;// Rays cannot penetrate objects } // take child put to parent Do its sub objects under private void
SetParentAndPosition(Transform child,Transform parent) { child.SetParent(parent)
; child.position = parent.position;// The child's coordinates follow the parent's } public bool
IsRaycastLocationValid(Vector2 sp, Camera eventCamera) { return
isRaycastLocationValid; } }

Technology
©2019-2020 Toolsou All rights reserved,
【Java8 New features 1】Lambda Expression summary What is a process , Concept of process ?hdfs dfs Common basic commands java When creating objects, you must _Java Basic test questions Generation of random numbers + Figure guessing game When will we enter the old generation ?HDFS Common commands ( summary ) It is never recommended to spend a lot of time studying for employment pythonmacOS Big Sur The installation could not be completed Big Sur Why can't I install it ?Python pyttsx3| Text reading ( Various languages )