当前位置:网站首页>Unity realizes UI backpack equipment dragging function
Unity realizes UI backpack equipment dragging function
2022-07-19 14:05:00 【Yun Xiaochuan】

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;// By default, rays cannot penetrate objects
public Transform IconList;
/// <summary>
/// Start dragging
/// </summary>
public void OnBeginDrag(PointerEventData eventData)
{
this.transform.SetAsLastSibling();
nowparent =this.transform.parent;// initial position
isRaycastLocationValid = false;// Set to be able to penetrate
transform.SetParent(IconList);// Put the currently dragged item under the outermost layer effect : Can block random UI Avoid others UI Block
}
/// <summary>
/// UI Follow mouse
/// </summary>
/// <param name="eventData"></param>
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
/// <summary>
/// Drag end
/// </summary>
public void OnEndDrag(PointerEventData eventData)
{
// Get items that may exist at the end of the mouse
GameObject go = eventData.pointerCurrentRaycast.gameObject;
if (go != null)// The position of the landing point is not empty
{
Debug.Log(go.name);// Print a drop point location name
if (go.tag == ("Gird"))// The end position of the mouse is an empty grid
{
Debug.Log(" Empty grid ");
SetParentAndPosition(transform, go.transform);
}
else if (go.tag == transform.tag)// The label is the same Put back
{
// Drag items 1 Put it under the end of the mouse
SetParentAndPosition(transform, go.transform);
}
else// Invalid location , The item returns to its original position
{
SetParentAndPosition(transform, nowparent);
}
}
else
{
SetParentAndPosition(transform, nowparent);
}
isRaycastLocationValid = true;// Rays cannot penetrate objects
}
// take child Put it in parent Make its sub object
private void SetParentAndPosition(Transform child, Transform parent)
{
child.SetParent(parent);
child.position = parent.position;// The coordinates of the child object follow those of the parent object
}
public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
{
return isRaycastLocationValid;
}
}边栏推荐
猜你喜欢

The NFT market pattern has not changed. Can okaleido set off a new round of waves?

Notes with a face value of 10exp (303)
![[code hoof set novice village 600 question] calculate the number of digits of an integer](/img/4b/4c2b6889decd30bb2e0f3b28dee0cf.png)
[code hoof set novice village 600 question] calculate the number of digits of an integer

(with source code) a variety of machine learning models (knn\lr\rf\ada\xg\gbdt...) Model training in precipitation downscaling under

NO.5整数的表示与运算

FreeRTOS personal notes - multi priority support

Tencent cloud object storage operation process

uniapp 高德地图定位功能

(附源码)多种机器学习模型(KNN\LR\RF\Ada\Xg\GBDT...)下的降水降尺度中的模型训练
![[Flink] Flink will report an error if it fails to set checkpoints once. Setlerablecheckpointfailurenumber does not work](/img/62/5761d8d7b2d8ea1bc74b609b6aa3c7.jpg)
[Flink] Flink will report an error if it fails to set checkpoints once. Setlerablecheckpointfailurenumber does not work
随机推荐
[7.13] code source - [hungry meals] [path count 2] [function sum]
STL string output and modification
【ACWing】2521. 数颜色
【动态规划】—— 最长上升子序列模型
Ranking of top ten ERP software systems at home and abroad!
Class 3 practice
[code hoof set novice village 600 question] calculate the number of digits of an integer
Is it safe for Hongye futures to open an account online? Are there any account opening guidelines?
洛谷P3512 [POI2010]PIL-Pilots 题解
Tke (k8s) deploy MySQL using CFS storage
类3实践
How to upgrade Flink job gracefully?
负载均衡有哪几种实现方式?
[7.15] code source - [neat array 2] [ternary cycle] [reverse pair on tree] [sequence of cochlea]
Start from here by counting the information of the broadcast room
asterisk:No compatible codecs, not accepting this offer!
How to earn money in low-level cities
【C语言】扫雷介绍与实现【数组与函数】
活动预告|Apache Doris x Apache SeaTunnel 联合 Meetup 开启报名!
Some common operation commands of the command line and solutions to common errors