首页 技术 正文
技术 2022年11月15日
0 收藏 524 点赞 4,412 浏览 1957 个字

  要是那几个状态栏不能拖动的话岂不是显得太呆板了,于是我又参考Unity官方视频教程学习了如何实现拖动状态栏的功能,还挺简单的。

  比如说要拖动这个PanelStatus面板,我只让使用者通过拖动其Text组件来实现拖动整个面板移动的效果。

3dContactPointAnnotationTool开发日志(十)

  只要为其Text绑定一个DragPanel.cs脚本,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;public class DragPanel : MonoBehaviour, IPointerDownHandler, IDragHandler
{
private Vector2 pointerOffset;
private RectTransform rectTransformCanvas;
private RectTransform rectTransformPanel;
void Awake()
{
rectTransformCanvas = GetComponentInParent<Canvas>().transform as RectTransform;
rectTransformPanel = transform.parent as RectTransform;
}
public void OnPointerDown(PointerEventData data)
{
rectTransformPanel.SetAsLastSibling();//把该组件放到UI最前面
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransformPanel,data.position,data.pressEventCamera,out pointerOffset);
} public void OnDrag(PointerEventData data)
{
Vector2 localPointerPosition;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
rectTransformCanvas,data.position,data.pressEventCamera,out localPointerPosition))
{
rectTransformPanel.localPosition = localPointerPosition - pointerOffset;
}
}}

  大概意思就是在OnPointerDown里获取按下鼠标时鼠标指针相对于panel的位置pointerOffset,在OnDrag中获取鼠标指针相对于canvas的位置localPointerPosition,然后localPointerPosition – pointerOffset就是panel的位置了。

  固定位置的组件可以被拖动了,效果如下:

3dContactPointAnnotationTool开发日志(十)

  有时候可能还想将这些面板给隐藏起来,于是又添加了三个按钮来控制这三个面板是否显示。直接调用控制对象的setActive即可,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class ButtonPanelControllerOnClick : MonoBehaviour {
public GameObject panel;//对应的面板
private bool active;//对应的面板是否被激活
private void Start()
{
active = true;
panel.SetActive(active);
GetComponent<Image>().color = Color.cyan;
}
public void OnClick()
{
active=!active;
if (active)
{
GetComponent<Image>().color = Color.cyan;
}
else {
GetComponent<Image>().color = Color.white;
}
panel.SetActive(active);
}
}

  效果如下,还挺好玩的:

3dContactPointAnnotationTool开发日志(十)

3dContactPointAnnotationTool开发日志(十)

  由于之前的代码里用到了FindWithTag函数,然而这个函数是找不到active为false的对象的。为了避免这种尴尬情况,我又创建了一个ObjManager对象来管理那些需要被查找的对象,将它们丢到ObjManager的脚本中存起来,以后谁要取就直接从这个脚本实例中拿就好了。

3dContactPointAnnotationTool开发日志(十)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893