当前位置:网站首页>简单的拖拽物体到物品栏
简单的拖拽物体到物品栏
2022-04-23 04:41:00 【龙胖胖的博客】
代码:需要移动物体上的代码(这里是血瓶)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DragDrop : MonoBehaviour,IPointerDownHandler,IBeginDragHandler ,IEndDragHandler,IDragHandler{
private RectTransform m_rectTransform;
public Canvas m_Canvas;
private CanvasGroup m_CanvasGroup;
void Start()
{
m_rectTransform = GetComponent<RectTransform>();
m_CanvasGroup = GetComponent<CanvasGroup>();
}
/// <summary>
/// 开始拖放前
/// </summary>
/// <param name="eventData"></param>
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("OnBeginDrag");
m_CanvasGroup.blocksRaycasts = false;
m_CanvasGroup.alpha = 0.6f;
}
/// <summary>
/// 拖拽
/// </summary>
/// <param name="eventData"></param>
public void OnDrag(PointerEventData eventData)
{
m_rectTransform.anchoredPosition += eventData.delta/ m_Canvas.scaleFactor;
}
/// <summary>
/// 拖拽结束
/// </summary>
/// <param name="eventData"></param>
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("OnEndDrag拖拽结束");
m_CanvasGroup.blocksRaycasts = true;
m_CanvasGroup.alpha = 1f;
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("OnPointerDown");
}
}
代码:物品格子代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class IteamSlot : MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData eventData)
{
if(eventData.pointerDrag!=null)
{
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
}
}
}
把脚本放到格子上就行了,
有点不好,鼠标移动到格子上才有放入的效果(有需要的可以试试)
版权声明
本文为[龙胖胖的博客]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qiao2037641855/article/details/107068280
边栏推荐
- 【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
- Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
- Leetcode004 -- Roman numeral to integer
- RC低通滤波器的逆系统
- 国外LEAD,联盟经理常见问答
- 智能电子秤全国产化电子元件推荐方案
- IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
- Small volume Schottky diode compatible with nsr20f30nxt5g
- 记录一下盲注脚本
- leetcode004--罗马数字转整数
猜你喜欢
520.检测大写字母
智能电子秤全国产化电子元件推荐方案
Apache Bench(ab 压力测试工具)的安装与使用
Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
Chapter 4 - understanding standard equipment documents, filters and pipelines
520. Detect capital letters
Supplement 14: cmake practice project notes (to be continued 4 / 22)
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
兼容NSR20F30NXT5G的小体积肖特基二极管
Installation and use of Apache bench (AB pressure test tool)
随机推荐
基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
Go反射—Go语言圣经学习笔记
383. Ransom letter
Use recyclerview to realize left-right side-by-side classification selection
leetcode006--查找字符串数组中的最长公共前缀
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
Brushless motor drive scheme based on Infineon MCU GTM module
Leetcode003 -- judge whether an integer is a palindrome number
HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
Small volume Schottky diode compatible with nsr20f30nxt5g
Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
Apache Bench(ab 压力测试工具)的安装与使用
Coinbase: basic knowledge, facts and statistics about cross chain bridge
520. Detect capital letters
Recommended scheme for national production of electronic components for wireless charging
Installation and use of Apache bench (AB pressure test tool)
QML advanced (V) - realize all kinds of cool special effects through particle simulation system
兼容NSR20F30NXT5G的小体积肖特基二极管
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
从MySQL数据库迁移到AWS DynamoDB