unity 从UI上拖出3D物体,(2D转3D)
效果展示:
2D转3D视频
UI结构
UI组件挂载
UI结构
这个脚本挂载到 3D物体身上
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class DragGame : MonoBehaviour
{[HideInInspector]public bool isDrag;public int z=8;void Start(){}// Update is called once per framevoid Update(){}IEnumerator OnMouseDown(){Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);//三维物体坐标转屏幕坐标//将鼠标屏幕坐标转为三维坐标,再计算物体位置与鼠标之间的距离var offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));isDrag = true;while (Input.GetMouseButton(0)){Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, z);var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;transform.position = curPosition;yield return new WaitForFixedUpdate();}}IEnumerator OnMouseUp(){isDrag = false;Whereabouts();yield return 0;}private void OnTriggerEnter(Collider other){//物体拖出后 碰到其他物体的逻辑}public void Whereabouts(){RaycastHit hit;//参数:当前物体,世界空间的方向,碰撞信息,最大距离if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, Mathf.Infinity)){transform.DOMove(new Vector3(hit.point.x, hit.point.y + (transform.localScale.y / 2), hit.point.z), 0.5f);}}
}
挂载到UI上面
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class KnapsackPanel : MonoBehaviour
{List<GameObject> scrollList = new List<GameObject>();//容量列表List<Toggle> toggles = new List<Toggle>();//背包类型列表GameObject to, sv, im;GameObject toggle;GameObject scrollView;Toggle Switch;public void Start(){toggle = transform.Find("GameObject/ToggleGroup/Toggle").gameObject;scrollView = transform.Find("GameObject/ScrollGroup/Scroll View").gameObject;Switch = transform.Find("GameObject/BG/Switch").GetComponent<Toggle>();RectTransform tf = transform.Find("GameObject").GetComponent<RectTransform>();Switch.onValueChanged.AddListener((arg) =>{if (arg){tf.transform.DOLocalMoveX(tf.transform.localPosition.x - tf.rect.width, 1f);}else{tf.transform.DOLocalMoveX(tf.transform.localPosition.x + tf.rect.width, 1f);}});CreateKnapsack();}/// <summary>/// 生成背包内容/// </summary>public void CreateKnapsack(){Toggle[] tog = transform.Find("GameObject/ToggleGroup").GetComponentsInChildren<Toggle>();for (int i = 0; i < tog.Length; i++){toggles.Add(tog[i]);}ScrollRect[] scrollbars= transform.Find("GameObject/ScrollGroup").GetComponentsInChildren<ScrollRect>(true);for (int i = 0; i < scrollbars.Length; i++){scrollList.Add(scrollbars[i].gameObject);Transform content = scrollbars[i].transform.Find("Viewport/Content");for (int j = 0; j < content.childCount; j++){int t = j;GameObject game = Resources.Load<GameObject>(content.GetChild(t).name);//需要拖拽出的3D物体DragGameCommand drag = new DragGameCommand(content.GetChild(t).gameObject, game, j, () => { Switch.isOn = true; });//dragGames.Add(drag);}}//类型切换事件绑定for (int i = 0; i < toggles.Count; i++){int t = i;toggles[i].onValueChanged.RemoveAllListeners();toggles[i].onValueChanged.AddListener((arg) =>{scrollList[t].SetActive(arg);});}}}
挂载到空物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class IEnumeratorManager : MonoBehaviour
{public static IEnumeratorManager instance;void Start(){instance = this;}
}
这个脚本 不需要挂载
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;public class DragGameCommand
{public GameObject instance;//UI图标public GameObject dragGame;//拖拽的物体GameObject game;//要创建的物体public GameObject scrollView;//对应的背包分类界面(拖回物体到UI的范围)public int z;//物体的Z轴距离int index;//在背包里排列的索引Action dragEnd;public DragGameCommand(GameObject image, GameObject _game, int _index, Action _dragEnd){instance = image;game = _game;scrollView = image.transform.parent.parent.parent.gameObject;AddListener();z = 8;int index = _index;dragEnd = _dragEnd;AddEvent(scrollView.transform);}#region 背包拖出物体/// <summary>/// 物体从UI 拖出的事件/// </summary>private void AddListener(){//给Image 动态添加 拖拽 事件EventTrigger eventTrigger = instance.gameObject.AddComponent<EventTrigger>();EventTrigger.Entry beginDrag = new EventTrigger.Entry();beginDrag.eventID = EventTriggerType.BeginDrag;beginDrag.callback.AddListener((v) =>{Debug.Log("开始拖拽");Image dragImage = instance.GetComponent<Image>();dragImage.raycastTarget = false;//UI置灰dragImage.color = new Color(1, 1, 1, 0.5f);string name = game.name;dragGame = GameObject.Instantiate(game);//创建3D物体DragGame dg = dragGame.GetComponent<DragGame>();dg.z = z;dragGame.name = name;ObjFollowMouse(dragGame);//让物体跟随鼠标});EventTrigger.Entry drag = new EventTrigger.Entry();drag.eventID = EventTriggerType.Drag;drag.callback.AddListener((v) =>{ObjFollowMouse(dragGame);//让物体跟随鼠标});EventTrigger.Entry endDrag = new EventTrigger.Entry();endDrag.eventID = EventTriggerType.EndDrag;endDrag.callback.AddListener((v) => {dragEnd?.Invoke();dragGame.GetComponent<DragGame>().Whereabouts();//下落});eventTrigger.triggers.Add(beginDrag);eventTrigger.triggers.Add(drag);eventTrigger.triggers.Add(endDrag);}/// <summary>/// UI跟随鼠标/// </summary>/// <param name="eventData"></param>private void ObjFollowMouse(GameObject gameObject){gameObject.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, z));}#endregion#region 物体放回背包Coroutine coroutine;//绑定背包界面鼠标移入和移出事件public void AddEvent(Transform viewport){EventTrigger eventr = viewport.gameObject.AddComponent<EventTrigger>();//给拖入范围UI,添加 鼠标进入和鼠标离开事件EventTrigger.Entry pointerEnter = new EventTrigger.Entry();pointerEnter.eventID = EventTriggerType.PointerEnter;pointerEnter.callback.RemoveAllListeners();//鼠标进入UIpointerEnter.callback.AddListener((v) =>{if (dragGame != null)//已经拖出来的物体{DragGame dragGame1 = dragGame.GetComponent<DragGame>();if (dragGame1.isDrag)//物体正在拖拽中{coroutine =IEnumeratorManager.instance. StartCoroutine(OnMouse(index));//放回背包事件}}});EventTrigger.Entry pointerExit = new EventTrigger.Entry();pointerExit.eventID = EventTriggerType.PointerExit;pointerExit.callback.RemoveAllListeners();//鼠标离开UIpointerExit.callback.AddListener((v) =>{if (coroutine != null){IEnumeratorManager.instance.StopCoroutine(coroutine);}});eventr.triggers.Add(pointerEnter);eventr.triggers.Add(pointerExit);}/// <summary>/// 物体放回背包/// </summary>/// <param name="index"></param>/// <returns></returns>IEnumerator OnMouse(int index){DragGame game = dragGame.GetComponent<DragGame>();Image image = instance.GetComponent<Image>();while (true){if (!game.isDrag && scrollView.activeInHierarchy)//物体结束拖拽 并且在对应的背包类型容量处于显示状态、{GameObject.DestroyImmediate(game.gameObject);image.raycastTarget = true;image.color = new Color(1, 1, 1, 1);dragGame = null;IEnumeratorManager.instance. StopCoroutine(coroutine);}yield return 0;}}#endregion
}
可以根据自己需求去修改,比如 动态生成背包里的类型和元素,
Demo上传了,可以下载,
相关文章:

unity 从UI上拖出3D物体,(2D转3D)
效果展示: 2D转3D视频 UI结构 UI组件挂载 UI结构 这个脚本挂载到 3D物体身上 using DG.Tweening; using System.Collections; using System.Collections.Generic; using UnityEngine;public class DragGame : MonoBehaviour {[HideInInspector]public bool isDrag…...

win10pycharm和anaconda安装和环境配置教程
windows10 64位操作系统下系统运行环境安装配置说明 下载和安装Anaconda,链接https://www.anaconda.com/download 下载完后,双击exe文件 将anaconda自动弹出的窗口全部关掉即可,然后配置高级系统变量 根据自己的路径,配置…...

[C++ 中]:6.类和对象下(static成员 + explicit +友元函数 + 内部类 + 编译器优化)
(static成员 explicit 友元函数 内部类 编译器优化) 一.static 成员:1.概念引入:1-1:定义全局变量记录个数? 2.如果有多个类需要分开去记录类对象的个数?2-1:可不可以声明成员变量解决&#…...
ONES Design UI 组件库环境搭建
这个 ONES Design UI 组件库 是基于 Ant Design 的 React UI 组件库,主要用于企业级研发管理工具的研发。 首先用 React 的脚手架搭建一个项目: npx create-react-app my-app cd my-app目前 ONES Design UI 组件库 托管在 ONES 私有的 npm 仓库上, 因此…...

支付宝AI布局: 新产品助力小程序智能化,未来持续投入加速创新
支付宝是全球领先的独立第三方支付平台,致力于为广大用户提供安全快速的电子支付/网上支付/安全支付/手机支付体验,及转账收款/水电煤缴费/信用卡还款/AA收款等生活服务应用。 支付宝不仅是一个支付工具,也是一个数字生活平台,通过…...

taro全局配置页面路由和tabBar页面跳转
有能力可以看官方文档:Taro 文档 页面路由配置,配置在app.config.ts里面的pages里: window用于设置小程序的状态栏、导航条、标题、窗口背景色,其配置项如下: tabBar配置:如果小程序是一个多 tab 应用&…...

【k8s】pod进阶
一、资源限制 1、资源限制的概念 当定义 Pod 时可以选择性地为每个容器设定所需要的资源数量。 最常见的可设定资源是 CPU 和内存大小,以及其他类型的资源。 当为 Pod 中的容器指定了 request 资源时,调度器就使用该信息来决定将 Pod 调度到哪个节点上…...

【设计模式】第18节:行为型模式之“迭代器模式”
一、简介 迭代器模式(Iterator Design Pattern),也叫作游标模式(Cursor Design Pattern)。 在通过迭代器来遍历集合元素的同时,增加或者删除集合中的元素,有可能会导致某个元素被重复遍历或遍…...

【数据结构】单链表OJ题
前言: 本节博客将讲解单链表的反转,合并有序链表,寻找中间节点及约瑟夫问题 文章目录 一、反转链表二、合并有序链表三、链表的中间结点四、环形链表的约瑟夫问题 一、反转链表 要反转链表,我们需要遍历链表并改变每个节点的 next 指针&#…...

智能工厂架构
引:https://www.bilibili.com/video/BV1Vs4y167Kx/?spm_id_from=333.788&vd_source=297c866c71fa77b161812ad631ea2c25 智能工厂框架 智能工厂五层系统框架 MES 数据共享 <...

阿里云多款ECS产品全面升级 性能最多提升40%
“阿里云始终围绕‘稳定、安全、性能、成本、弹性’的目标不断创新,为客户创造业务价值。”10月31日,杭州云栖大会上,阿里云弹性计算计算产品线负责人张献涛表示,通过持续的产品和技术创新,阿里云发布了HPC优化实例等多…...
责任链模式(Chain of Responsibility)
责任链模式是对象的行为模式。使多个对象都有机会处理请求,从而避免请求的发送者和接受者直接的耦合关系。 public abstract class Handler {protected Handler successor;public abstract void handlerRequest(String condition);protected Handler getSuccessor()…...

文件管理技巧:根据大小智能分类并移动至目标文件夹
在文件管理过程中,我们经常需要整理大量的文件。根据文件的大小,将其智能分类并移动至目标文件夹,可以帮助我们更高效地管理文件,提高工作效率。通过使用云炫文件管理器可以根据文件大小进行智能分类和移动至目标文件夹࿰…...

具有自主产权的SaaS门店收银系统全套源码输出
PHPMysql前后端分离, 小程序线上商城; 进销存管理库存盘点, 多仓库库存调拨, 会员系统。 消费者扫码查价系统。...

论文阅读:One Embedder, Any Task: Instruction-Finetuned Text Embeddings
1. 优势 现存的emmbedding应用在新的task或者domain上时表现会有明显下降,甚至在相同task的不同domian上的效果也不行。这篇文章的重点就是提升embedding在不同任务和领域上的效果,特点是不需要用特定领域的数据进行finetune而是使用instuction finetun…...
[BUUCTF NewStarCTF 2023 公开赛道] week3 crypto/pwn
居然把第3周忘了写笔记了. 后边难度上来了,还是很有意思的 Crypto Rabins RSA rsa一般要求e与phi互质,但rabin一般用2,都是板子题也没什么好解释的 from Crypto.Util.number import * from secret import flag p getPrime(64) q getPrime(64) assert p % 4 3 assert q %…...

软件测试---边界值分析(功能测试)
能对限定边界规则设计测试点---边界值分析 选取正好等于、刚好大于、刚好小于边界的值作为测试数据 上点: 边界上的点 (正好等于);必选(不考虑区开闭) 内点: 范围内的点 (区间范围内的数据);必选(建议选择中间范围) 离点: 距离上点最近的点 (刚好…...

使用pytorch处理自己的数据集
目录 1 返回本地文件中的数据集 2 根据当前已有的数据集创建每一个样本数据对应的标签 3 tensorboard的使用 4 transforms处理数据 tranfroms.Totensor的使用 transforms.Normalize的使用 transforms.Resize的使用 transforms.Compose使用 5 dataset_transforms使用 1 返回本地…...
http进一步认识
好久不见各位,今天为大家带来http协议的进一步认识 文章目录 👀http协议的认识👀新的改变 👀http协议的认识 http协议经历了三个版本的演化,HTTP0.9是第一个版本的协议,它的组成极其简单,只涉…...
grafana docker安装
grafana docker安装 Grafana是一款用Go语言开发的开源数据可视化工具,可以做数据监控和数据统计,带有告警功能。目前使用grafana的公司有很多,如paypal、ebay、intel等。 Grafana 是 Graphite 和 InfluxDB 仪表盘和图形编辑器。Grafana 是开…...

XML Group端口详解
在XML数据映射过程中,经常需要对数据进行分组聚合操作。例如,当处理包含多个物料明细的XML文件时,可能需要将相同物料号的明细归为一组,或对相同物料号的数量进行求和计算。传统实现方式通常需要编写脚本代码,增加了开…...
Python爬虫实战:研究MechanicalSoup库相关技术
一、MechanicalSoup 库概述 1.1 库简介 MechanicalSoup 是一个 Python 库,专为自动化交互网站而设计。它结合了 requests 的 HTTP 请求能力和 BeautifulSoup 的 HTML 解析能力,提供了直观的 API,让我们可以像人类用户一样浏览网页、填写表单和提交请求。 1.2 主要功能特点…...

铭豹扩展坞 USB转网口 突然无法识别解决方法
当 USB 转网口扩展坞在一台笔记本上无法识别,但在其他电脑上正常工作时,问题通常出在笔记本自身或其与扩展坞的兼容性上。以下是系统化的定位思路和排查步骤,帮助你快速找到故障原因: 背景: 一个M-pard(铭豹)扩展坞的网卡突然无法识别了,扩展出来的三个USB接口正常。…...
【Web 进阶篇】优雅的接口设计:统一响应、全局异常处理与参数校验
系列回顾: 在上一篇中,我们成功地为应用集成了数据库,并使用 Spring Data JPA 实现了基本的 CRUD API。我们的应用现在能“记忆”数据了!但是,如果你仔细审视那些 API,会发现它们还很“粗糙”:有…...

分布式增量爬虫实现方案
之前我们在讨论的是分布式爬虫如何实现增量爬取。增量爬虫的目标是只爬取新产生或发生变化的页面,避免重复抓取,以节省资源和时间。 在分布式环境下,增量爬虫的实现需要考虑多个爬虫节点之间的协调和去重。 另一种思路:将增量判…...

SAP学习笔记 - 开发26 - 前端Fiori开发 OData V2 和 V4 的差异 (Deepseek整理)
上一章用到了V2 的概念,其实 Fiori当中还有 V4,咱们这一章来总结一下 V2 和 V4。 SAP学习笔记 - 开发25 - 前端Fiori开发 Remote OData Service(使用远端Odata服务),代理中间件(ui5-middleware-simpleproxy)-CSDN博客…...
Go 语言并发编程基础:无缓冲与有缓冲通道
在上一章节中,我们了解了 Channel 的基本用法。本章将重点分析 Go 中通道的两种类型 —— 无缓冲通道与有缓冲通道,它们在并发编程中各具特点和应用场景。 一、通道的基本分类 类型定义形式特点无缓冲通道make(chan T)发送和接收都必须准备好࿰…...

FFmpeg:Windows系统小白安装及其使用
一、安装 1.访问官网 Download FFmpeg 2.点击版本目录 3.选择版本点击安装 注意这里选择的是【release buids】,注意左上角标题 例如我安装在目录 F:\FFmpeg 4.解压 5.添加环境变量 把你解压后的bin目录(即exe所在文件夹)加入系统变量…...

[ACTF2020 新生赛]Include 1(php://filter伪协议)
题目 做法 启动靶机,点进去 点进去 查看URL,有 ?fileflag.php说明存在文件包含,原理是php://filter 协议 当它与包含函数结合时,php://filter流会被当作php文件执行。 用php://filter加编码,能让PHP把文件内容…...
TCP/IP 网络编程 | 服务端 客户端的封装
设计模式 文章目录 设计模式一、socket.h 接口(interface)二、socket.cpp 实现(implementation)三、server.cpp 使用封装(main 函数)四、client.cpp 使用封装(main 函数)五、退出方法…...