资源简介
当我们做一些农场游戏或者类似可编辑战场类游戏时经常会遇到从UI上拖动物体在3D场景里生成,要实现这种从UI上拖物体的效果其实挺简单,代码不复杂,主要是想法很重要,一个优秀的程序员不只是技术扎实,更重要的是要多思考,不思考的程序员只是一个码农。

代码片段和文件信息
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dragobject : MonoBehaviour {
//只针对指定的层级进行拖动
public layerMask _draglayerMask;
//指定当前要拖动的对象
public Transform currentTransform;
//是否可以拖动当前对象
public bool isDrag = false;
//用于存储当前需要拖动的对象在屏幕空间中的坐标
Vector3 screenPos = Vector3.zero;
//当前需要拖动对象的坐标相对于鼠标在世界空间坐标中的偏移量
Vector3 offset = Vector3.zero;
void Update () {
if (Input.GetMouseButtonDown (0)) {
//将鼠标输入点转化为一条射线
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hitinfo;
//如果当前对象与指定的层级发生碰撞,表示当前对象可以被拖动
if (Physics.Raycast (ray out hitinfo 1000f _draglayerMask)) {
isDrag = true;
//将当前需要拖动的对象赋值为射线碰撞到的对象
currentTransform = hitinfo.transform;
//将当前对象的世界坐标转化为屏幕坐标
screenPos = Camera.main.WorldToScreenPoint (currentTransform.position);
//将鼠标的屏幕坐标转换为世界空间坐标,再与当前要拖动的对象计算两者的偏移量
offset = currentTransform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x Input.mousePosition.y screenPos.z));
} else {
isDrag = false;
}
}
if (Input.GetMouseButton (0)) {
if (isDrag == true) {
var currentScreenPos = new Vector3 (Input.mousePosition.x Input.mousePosition.y screenPos.z);
//鼠标的屏幕空间坐标转化为世界坐标,并加上偏移量
var currentPos = Camera.main.ScreenToWorldPoint (currentScreenPos) + offset;
currentTransform.position = currentPos;
}
}
if (Input.GetMouseButtonUp (0)) {
isDrag = false;
currentTransform = null;
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-07-31 07:41 Placeob
目录 0 2018-07-31 07:41 Placeob
文件 1347 2018-07-31 07:41 Placeob
文件 7207 2018-07-31 07:41 Placeob
目录 0 2018-07-31 07:41 Placeob
文件 213 2018-07-31 07:41 Placeob
目录 0 2018-07-31 07:41 Placeob
文件 2019 2018-07-31 07:41 Placeob
文件 229 2018-07-31 07:41 Placeob
文件 2012 2018-07-31 07:41 Placeob
文件 229 2018-07-31 07:41 Placeob
文件 1991 2018-07-31 07:41 Placeob
文件 229 2018-07-31 07:41 Placeob
文件 2019 2018-07-31 07:41 Placeob
文件 229 2018-07-31 07:41 Placeob
文件 213 2018-07-31 07:41 Placeob
目录 0 2018-07-31 07:41 Placeob
文件 2718 2018-07-31 07:41 Placeob
文件 231 2018-07-31 07:41 Placeob
文件 2709 2018-07-31 07:41 Placeob
文件 231 2018-07-31 07:41 Placeob
文件 2704 2018-07-31 07:41 Placeob
文件 231 2018-07-31 07:41 Placeob
文件 213 2018-07-31 07:41 Placeob
目录 0 2018-07-31 07:41 Placeob
文件 679 2018-07-31 07:41 Placeob
文件 229 2018-07-31 07:41 Placeob
文件 676 2018-07-31 07:41 Placeob
文件 229 2018-07-31 07:41 Placeob
文件 678 2018-07-31 07:41 Placeob
文件 229 2018-07-31 07:41 Placeob
............此处省略533个文件信息
- 上一篇:GSM模块的使用说明
- 下一篇:vivado烧写与擦除flash.pdf
相关资源
- layui后台管理模板
- 嵌入式图形界面MiniGUI的示例程序9例
- PowerBuilder*图书馆管理系统*需求分析
- 十以内加减法练习 powerbuilder源码
- pb 实现仿BS界面 dw菜单 powerbuild
- STM32基于rt_thread操作系统的SDHC卡文件
- DOS播放器 QuickViewPro破解版
- GUI银行管理系统
- 分治法快速排序算法QuickSort C
- STM32F103RBT6驱动UC1698控制芯片的160160黑
- ADNS-3080光流传感器测试程序Arduino
- Unity3D登录界面工程
- powerbuilder获取本地IP地址或主机名
- unity3d反编译工具
- pb (powerbuilder) 编写的自动转台控制
- powerbuilder 计算器小程序
- 拼图游戏源代码 powerbuilder 9.0实例
- 仿126 网易 163 邮箱 界面
- 用PB编写俄罗斯方块
- 《PowerBuilder9.0实用教程》的实例源代
- Design of Analog CMOS Integrated Circuits 拉扎
- EESkill NRF24L01 无线模块用户手册
-
UnityWebPla
yer及UnityWebPla yerFull -
UnityWebPla
yerFull - System Design Interview - An Insider’s Guide
- vue+elmentui+ueditor +KityFormula数学公式 编
- layuiAdmin:layuiAdmin后台管理模板完全由
- SunnyUI.Net
- .NET WinForm 和 WPF 的UI界面库,收集整合
- VCS lab 和 VCS lab guide
评论
共有 条评论