当前位置:网站首页>unity 实现UI-背包装备拖拽功能
unity 实现UI-背包装备拖拽功能
2022-07-17 20:03:00 【云小川】

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Drag : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler, ICanvasRaycastFilter
{
private Transform nowparent;//记录原始坐标位置
private bool isRaycastLocationValid = true;//默认射线不能穿透物体
public Transform IconList;
/// <summary>
/// 开始拖拽
/// </summary>
public void OnBeginDrag(PointerEventData eventData)
{
this.transform.SetAsLastSibling();
nowparent =this.transform.parent;//初始位置
isRaycastLocationValid = false;//设置为可以穿透
transform.SetParent(IconList);//将当前拖拽的物品放在最外层下 效果:可以挡住随意UI 避免呗其他UI挡住
}
/// <summary>
/// UI跟随 鼠标
/// </summary>
/// <param name="eventData"></param>
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
/// <summary>
/// 拖拽结束
/// </summary>
public void OnEndDrag(PointerEventData eventData)
{
//获取鼠标终点位置可能存在的物品
GameObject go = eventData.pointerCurrentRaycast.gameObject;
if (go != null)//落点位置不为空
{
Debug.Log(go.name);//打印一下落点位置名称
if (go.tag == ("Gird"))//鼠标终点位置是空格子
{
Debug.Log("空格子");
SetParentAndPosition(transform, go.transform);
}
else if (go.tag == transform.tag)//标签相同 放回
{
//将拖拽的物品1放到鼠标终点下的位置
SetParentAndPosition(transform, go.transform);
}
else//无效位置,物品回到原来的位置
{
SetParentAndPosition(transform, nowparent);
}
}
else
{
SetParentAndPosition(transform, nowparent);
}
isRaycastLocationValid = true;//射线不可以穿透物体
}
// 将child放到parent下做其子物体
private void SetParentAndPosition(Transform child, Transform parent)
{
child.SetParent(parent);
child.position = parent.position;//子物体的坐标跟随父物体
}
public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
{
return isRaycastLocationValid;
}
}边栏推荐
- STL string find substring
- [7.13] code source - [hungry meals] [path count 2] [function sum]
- STL string output and modification
- Luogu p2422 good feeling solution
- [learn FPGA programming from scratch -53]: high level chapter - FPGA development based on IP core - principle and configuration of PLL PLL IP core (Xilinx)
- uniapp 高德地图定位功能
- [postgraduate entrance examination vocabulary training camp] day 5 - alarm, cooperate, point, benefit, industrial, revolution, mechanism
- 【7.15】代码源 -【整齐的数组2】【三进制循环】【树上逆序对】【蜗蜗的数列】
- No.4 bits, bytes, information storage
- 【7.14】代码源 -【拆方块】【XOR Inverse】【连续子序列】【三角果计数】
猜你喜欢

【码蹄集新手村 600 题】格式化的输入输出,使用 0 来代替补全的空格

ImportError: DLL load failed while importing win32api: 找不到指定的程序。

Some puzzles about data dictionary
![[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

FreeRTOS implementation of idle tasks and blocking delay

命令行的一些常用操作命令及常见错误的解决办法

Onvif protocol related: 3.1.4 get the stream address in digest mode

微服务调用组件feign实战

chrome插件用于信息收集

The NFT market pattern has not changed. Can okaleido set off a new round of waves?
随机推荐
Introduction:Multiple DataFrames
"Technology podcast month" day 10: meta podcast: talk about Podcasting
[code shoe set novice village question 600] for the formatting control of strings, that is, the width and accuracy of strings
【7.12】Codeforces Round #806 (Div. 4)
How to earn money in low-level cities
【码蹄集新手村 600 题】针对于字符串的格式化控制,即字符串的宽度与精度
[7.9] code source - [number selection] [sequence operation] [minimum or spanning tree]
Onvif protocol related: 3.1.1 digest access authorization
uniapp 高德地图定位功能
谷歌浏览器开发者工具的使用(掌握!)
FreeRTOS implementation of idle tasks and blocking delay
Onvif protocol related: 3.1.4 get the stream address in digest mode
NO.2汇编初步
AcWing第 60 场周赛
S32K148_ Can drive (bare metal development)
Onvif protocol related: 3.1.2 get the token list in digest mode
TKE(K8S)部署mysql使用CFS存储
面试记录
无声的AI:昇腾AI如何用大模型破解手语学习的难题?
【码蹄集新手村 600 题】格式化的输入输出,使用 0 来代替补全的空格