Unity----Panes, panels and windows(官方教程)
- 通过panel创建一个窗口,实现拖拽,限制拖拽区域,改变窗口大小
原视频链接:https://unity3d.com/cn/learn/tutorials/modules/intermediate/live-training-archive/panels-panes-windows?playlist=17111
1,通过接口IPointerDownHandler, IDragHandler在函数OnPointerDown, OnDrag中实现panels本地坐标的移动,完成拖拽功能
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;public class DragPanel : MonoBehaviour, IPointerDownHandler, IDragHandler
{private Vector2 pointerOffset;private RectTransform canvasRectTransform;private RectTransform panelRectTransform;void Awake(){Canvas canvas = GetComponentInParent<Canvas>();if (canvas != null){canvasRectTransform = canvas.transform as RectTransform;panelRectTransform = transform as RectTransform;}}public void OnPointerDown(PointerEventData data){panelRectTransform.SetAsLastSibling();RectTransformUtility.ScreenPointToLocalPointInRectangle(panelRectTransform, data.position, data.pressEventCamera, out pointerOffset);}public void OnDrag(PointerEventData data){if (panelRectTransform == null)return;Vector2 localPointerPosition;if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, data.position, data.pressEventCamera, out localPointerPosition)){panelRectTransform.localPosition = localPointerPosition - pointerOffset;}}}
相关:
eventSystem详解 : http://www.manew.com/blog-56596-2917.html
ScreenPointToLocalPointInRectangle : http://gad.qq.com/article/detail/41551
2,通过在panel组件下增加一个透明image用作darg区域,将脚本DragPanel附加到该组件上,并将
panelRectTransform = transform as RectTransform;改为panelRectTransform = transform.parent as RectTransform;
实现只拖拽部分区域。
3,实现函数ClampToWindow来限制传入OnDrag函数中传入ScreenPointToLocalPointInRectangle的screenpoint的区域,以避免panel被拖拽出屏幕
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;public class DragPanel : MonoBehaviour, IPointerDownHandler, IDragHandler
{private Vector2 pointerOffset;private RectTransform canvasRectTransform;private RectTransform panelRectTransform;void Awake(){Canvas canvas = GetComponentInParent<Canvas>();if (canvas != null){canvasRectTransform = canvas.transform as RectTransform;panelRectTransform = transform.parent as RectTransform;}}public void OnPointerDown(PointerEventData data){panelRectTransform.SetAsLastSibling();RectTransformUtility.ScreenPointToLocalPointInRectangle(panelRectTransform, data.position, data.pressEventCamera, out pointerOffset);}public void OnDrag(PointerEventData data){if (panelRectTransform == null)return;Vector2 pointerPostion = ClampToWindow(data);Vector2 localPointerPosition;if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, pointerPostion, data.pressEventCamera, out localPointerPosition)){panelRectTransform.localPosition = localPointerPosition - pointerOffset;}}Vector2 ClampToWindow(PointerEventData data){Vector2 rawPointerPosition = data.position;Vector3[] canvasCorners = new Vector3[4];canvasRectTransform.GetWorldCorners(canvasCorners);float clampedX = Mathf.Clamp(rawPointerPosition.x, canvasCorners[0].x, canvasCorners[2].x);float clampedY = Mathf.Clamp(rawPointerPosition.y, canvasCorners[0].y, canvasCorners[2].y);Vector2 newPointerPosition = new Vector2(clampedX, clampedY);return newPointerPosition;}
}
4,通过Button来控制panel的开关,注册到Button的OnClick事件上
using UnityEngine;
using System.Collections;public class TogglePanelButton : MonoBehaviour {public void TogglePanel (GameObject panel) {panel.SetActive (!panel.activeSelf);}
}
5,通过更改panel的sizeDelta来更改窗口的大小
在panel上添加一透明image,附加以下脚本即可,由于这里是通过sizeDelta来更改,所以panel的锚点最好设置成聚在一起的一个点,不然放大缩小都会出现一些问题
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;public class ResizePanel : MonoBehaviour, IPointerDownHandler, IDragHandler {public Vector2 minSize;public Vector2 maxSize;private RectTransform rectTransform;private Vector2 currentPointerPosition;private Vector2 previousPointerPosition;void Awake () {rectTransform = transform.parent.GetComponent();}public void OnPointerDown (PointerEventData data) {rectTransform.SetAsLastSibling();RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out previousPointerPosition);}public void OnDrag (PointerEventData data) {if (rectTransform == null)return;Vector2 sizeDelta = rectTransform.sizeDelta;RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out currentPointerPosition);Vector2 resizeValue = currentPointerPosition - previousPointerPosition;sizeDelta += new Vector2 (resizeValue.x, -resizeValue.y);sizeDelta = new Vector2 (Mathf.Clamp (sizeDelta.x, minSize.x, maxSize.x),Mathf.Clamp (sizeDelta.y, minSize.y, maxSize.y));rectTransform.sizeDelta = sizeDelta;previousPointerPosition = currentPointerPosition;}
}
6,在panel上附加以下脚本使得每次点击该窗口都能让它显示在最上面
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;public class FocusPanel : MonoBehaviour, IPointerDownHandler {private RectTransform panel;void Awake () {panel = GetComponent (); }public void OnPointerDown (PointerEventData data) {panel.SetAsLastSibling ();}}
Unity----Panes, panels and windows(官方教程)相关推荐
- [Unity3d]unity从入门到精通官方教程
<Unity 4.x 入门到精通>完整pdf 一百多M: http://download.csdn.net/detail/s10141303/6912127 如果不能正常下载 也可以加 ...
- Unity 官方教程 学习
Interface & Essentials Using the Unity Interface 1.Interface Overview https://unity3d.com/cn/lea ...
- unity官方教程-TANKS(一)
unity官方教程TANKS,难度系数中阶. 跟着官方教程学习Unity,通过本教程你可以学会使用Unity开发游戏的基本流程. 一.环境 Unity 版本 > 5.2 Asset Store ...
- 如何在Windows上做Python开发?微软出了官方教程(附链接)
来源:机器之心 本文附教程,建议阅读5分钟. 本文为你分享微软最近发布的关于在Windows上做Python开发的一系列官方教程. 在Windows上做Python开发太痛苦?微软最近发布了一系列官方 ...
- 微软官方教程教你如何在Windows上做Python开发?
关注上方"深度学习技术前沿",选择"星标公众号", 资源干货,第一时间送达! 教程地址:https://docs.microsoft.com/zh-cn/win ...
- Unity官方教程Ruby大冒险的自学笔记
Unity官方教程Ruby大冒险的自学笔记 一. //正确例子: void Update(){//获取运动矢量moveX = Input.GetAxisRaw("Horizontal&quo ...
- asp.net web开发步骤_如何在Windows上做Python开发?微软出了官方教程
机器之心报道 参与:路 在 Windows 上做 Python 开发太痛苦?微软最近发布了一系列官方教程,终于-- 教程地址:https://docs.microsoft.com/zh-cn/wind ...
- 噩梦射手 安装包资源包提供下载 Unity官方教程 Survival Shooter 资源已经失效了!? Unity3D休闲射击类游戏《Survival Shooter》完整源码
Unity官方教程 (Survival Shooter) 资源已经失效了! 可能是版本太老了 中文名叫噩梦射手? 找了半天找了这个版本 的 放到这里吧 [这个游戏主角是必死的,就看能坚持多久啦] 网 ...
- 乐鑫Esp32学习之旅② 巧用eclipes编辑器,官方教程在Windows下搭建esp32开发环境,打印 “Hello World”。
本系列博客学习由非官方人员 半颗心脏 潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途.如有不对之处,请留言,本人及时更改. 1. 爬坑学习新旅程,虚拟机搭建esp32开发环境,打印 " ...
- Unity性能优化(2)-官方教程Diagnosing performance problems using the Profiler window翻译
http://www.cnblogs.com/alan777/p/6135703.html Unity性能优化(2)-官方教程Diagnosing performance problems using ...
最新文章
- OpenStack文件注入相关分析(转载)
- 想转行ML/AI却没有方向?这篇指南告诉你!
- quicktime无法安装
- 文巾解题 278. 第一个错误的版本
- 故障分析--主从复制故障1
- tab栏切换 动画的相关方法上 动画的相关方法下 隐藏动画案例 隐藏动画练习
- Eclipse 报java.lang.OutOfMemoryError: PermGen space错
- Java面向对象(5)--类的成员构造器(构造方法)
- 3_python基础—运算符 1
- 规划极限编程阅读笔记01
- java 时间格式化 星期_Java SimpleDateFormate时间格式化
- 在 Visual Studio 2010 中配置SharpPcap
- 解决Xcode 9.x 没有代码提示
- 【Maven】阿里云镜像仓库
- 百度排名批量查询_企业网站核心关键词排名消失,什么原因?
- 快速解决打印机后台程序服务没有运行的问题
- 关于trycatchfinal返回值问题
- RHEL6: Server panicked in 'redirfs' module
- 小迪-65-内网安全
- linux的应用界面设计,技术|Xperience UI 设计理念:优雅的 Linux 桌面设计欣赏