UI 滑动面板的移动控制
2024-08-22 08:43:49
通过拖拽的方式移动面板,在单个界面看到多个视图面板
需要导入UnityEngine.EventSystems; 事件命名空间
进而继承两个重要的接口IBeginDragHandler,IDragHandler,继承接口中的方法必须全部实现,直接右键转到定义,且方法必须为public。
/***
*
*
*
* 滑动面板的移动控制
*
*
*
*
*
*
*
*/
using System.Collections;
using UnityEngine.UI; //UI命名空间
using UnityEngine.EventSystems; //事件系统命名空间
using System.Collections.Generic;
using UnityEngine; public class SliderPanelMovingControl : MonoBehaviour,IBeginDragHandler,IDragHandler { public float FloSliderPanelSmoothPara = 5F; //滑动面板弹性系数
private float _FloStartPositionX; //开始的鼠标X的位置
private float _FloPosX; //鼠标移动的位置偏移量 // Use this for initialization
void Start () { } //开始拖拽
public void OnBeginDrag(PointerEventData eventData)
{
_FloStartPositionX = eventData.position.x;
} //拖拽中
public void OnDrag(PointerEventData eventData)
{
_FloPosX = eventData.position.x - _FloStartPositionX;
//移动面板,需要偏移量
this.transform.Translate(new Vector3(_FloPosX/, , ), Space.World);
} // Update is called once per frame
void Update () {
if (this.transform.localPosition.x > )
{
//移动范围受限处理(左边缘)
this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(, , ), Time.deltaTime * FloSliderPanelSmoothPara);
} if (this.transform.localPosition.x < -)
{
//移动范围受限处理(右边缘)
this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(-, , ), Time.deltaTime * FloSliderPanelSmoothPara);
} //整版“卡位”
if (this.transform.localPosition.x < && this.transform.localPosition.x>=-)
{
this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(-, , ), Time.deltaTime * FloSliderPanelSmoothPara);
}
else if (this.transform.localPosition.x < - && this.transform.localPosition.x >= -)
{
this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(-, , ), Time.deltaTime * FloSliderPanelSmoothPara);
}
else if (this.transform.localPosition.x < - && this.transform.localPosition.x >= -)
{
this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(-, , ), Time.deltaTime * FloSliderPanelSmoothPara);
}
else if (this.transform.localPosition.x < - && this.transform.localPosition.x >= -)
{
this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(-, , ), Time.deltaTime * FloSliderPanelSmoothPara);
}
}
}
之后对滑动面板进行完善:localPosition相对于父对象的位置,需要用到一个重要的函数Vector3.Lerp(),
这是一个差值处理函数,Lerp中有3个参数,(from,to,0-1之间的数),to参数是对位置的限制,相当于是摄像机跟随,很灵活。
最新文章
- 【C++】 struct结构自动对齐的问题
- CRC 冗余校验计算
- php 基础知识
- plsql记住登录密码
- 《Java实验四》
- 【BZOJ】【1855】【SCOI2010】/【HDOJ】【3401】股票交易
- 常用的Python字符串常量
- 【转】c/c++各种字符、字符串类型转换
- hdu 6059---Kanade&#39;s trio(字典树)
- PhiloGL学习(6)——深情奉献:快乐的一家
- VFS四大对象之三 struct dentry
- 【linux】linux系统中常见配置文件说明
- 【Android Studio】Gradle配置及问题解决
- python_day12_html
- 转:深入理解Java G1垃圾收集器
- 我的CSS命名规则
- SpringMVC源码阅读:视图解析器
- 统计SQL Server所有表记录数
- PHP获取目录和的方法通过魔术变量;通过超级全局变量;通过相关函数等等:
- 撩课-Web大前端每天5道面试题-Day8
热门文章
- Hibernate **关于hibernate4.3版本之后org.hibernate.service.ServiceRegistryBuilder被弃用**
- react用class关键字来创建组件
- Error: File &#39;C:\somepath\bin\ARM\Release\App_1.1.218.0_scale-100.appx&#39; not found.
- int,String转换
- Codeforces 719A 月亮
- CSS初步学习
- P1387 最大正方形 dp
- 【BZOJ4042】【CERC2014】parades 状压DP
- mysql 0x80004005 unable to connect to any of the specified mysql hosts
- 使用Putty执行Rsync命令