当前位置:网站首页>简单的拖拽物体到物品栏
简单的拖拽物体到物品栏
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
边栏推荐
- Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
- Go reflection - go language Bible learning notes
- Inverse system of RC low pass filter
- [pytoch foundation] torch Split() usage
- 用D435i录制自己的数据集运行ORBslam2并构建稠密点云
- Jetpack 之 LifeCycle 组件使用详解
- 协程与多进程的完美结合
- io. Platform. packageRoot; // ignore: deprecated_ Member_ use
- Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
- test
猜你喜欢

Supplement: Annotation

C language: spoof games

Brushless motor drive scheme based on Infineon MCU GTM module

Coinbase: basic knowledge, facts and statistics about cross chain bridge

Go反射法则

229. 求众数 II

MySQL queries users logged in for at least N consecutive days

递归调用--排列的穷举

zynq平臺交叉編譯器的安裝

那些年我面试过的Android开发岗总结(附面试题+答案解析)
随机推荐
leetcode005--原地删除数组中的重复元素
Phishing for NFT
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
C language: Advanced pointer
leetcode004--罗马数字转整数
2020 is coming to an end, special and unforgettable.
Code007 -- determine whether the string in parentheses matches
Detailed explanation of life cycle component of jetpack
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
用D435i录制自己的数据集运行ORBslam2并构建稠密点云
zynq平臺交叉編譯器的安裝
A new method for evaluating the quality of metagenome assembly - magista
mysql ,binlog 日志查询
Eight misunderstandings that should be avoided in data visualization
从MySQL数据库迁移到AWS DynamoDB
What is a data island? Why is there still a data island in 2022?
leetcode003--判断一个整数是否为回文数
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
MySQL queries users logged in for at least N consecutive days
test