Lerp线性渐变
Mathf.Lerp 的含义就是,从某一个值,到某一个值的过渡,进度百分比,
using UnityEngine.EventSystems;
IBeginDragHandle接口(开始拖动)IEndDargHandle接口(结束拖动)
只要继承了IBeginDragHandler、IDragHandler、和IEndDragHandler这三个接口,并实现了OnBeginDrag、OnDrag和OnEndDrag这三个方法,我们就可以实现拖拽功能。其中,OnBeginDrag处理开始拖动时要做什么事,OnDrag处理拖动过程中要做什么事,OnEndDrag处理拖动结束时要做什么事
C#中不支持多重继承,但是可以继承多个接口
只要继承了IBeginDragHandler、IDragHandler、和IEndDragHandler这三个接口,并实现了OnBeginDrag、OnDrag和OnEndDrag这三个方法,我们就可以实现拖拽功能。其中,OnBeginDrag处理开始拖动时要做什么事,OnDrag处理拖动过程中要做什么事,OnEndDrag处理拖动结束时要做什么事
C#中不支持多重继承,但是可以继承多个接口
horizontalNormalizedPosition 横向滑动的距离
offset 偏移量
以下代码为实现页面滑动及Toggle翻页功能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
public class hd : MonoBehaviour,IBeginDragHandler,IEndDragHandler {
ScrollRect Scrollrect;
public Toggle[] toggle;
// Use this for initialization
public float[] pageArray = new float[] { 0, 0.25f, 0.5f, 0.75f, 1 };//每一页的位置百分比
bool IsDraging = false;
public int index = 0;
float targetHorizatorPosition = 0; //目标位置百分比
public float smoothing = 2;
void Start () {
Scrollrect = GetComponent<ScrollRect>();
}
// Update is called once per frame
void Update () {
ScrollRect Scrollrect;
public Toggle[] toggle;
// Use this for initialization
public float[] pageArray = new float[] { 0, 0.25f, 0.5f, 0.75f, 1 };//每一页的位置百分比
bool IsDraging = false;
public int index = 0;
float targetHorizatorPosition = 0; //目标位置百分比
public float smoothing = 2;
void Start () {
Scrollrect = GetComponent<ScrollRect>();
}
// Update is called once per frame
void Update () {
Scrollrect.horizontalNormalizedPosition = Mathf.Lerp(Scrollrect.horizontalNormalizedPosition, targetHorizatorPosition, Time.deltaTime * smoothing);//线性渐变
}
}
public void OnBeginDrag(PointerEventData eventData)
{
IsDraging = true;
}
{
IsDraging = true;
}
public void OnEndDrag(PointerEventData eventData)
{
IsDraging = false;
float posX = Scrollrect.horizontalNormalizedPosition;//从滑动开始到结束的滑动距离
//Scrollrect.horizontalNormalizedPosition 横向滑动的距离介于O到1之间
float offsetl = Mathf.Abs(pageArray[index] - posX);//左偏移的距离
for (int i = 0; i < pageArray.Length; i++)
{
float offsetr = Mathf.Abs(pageArray[i] - posX);
if (offsetl>offsetr)
{
index = i;
}
}
targetHorizatorPosition = pageArray[index];
toggle[index].isOn = true;
}
public void toggle1(bool isOn)
{
if (isOn)
{
targetHorizatorPosition = pageArray[0];
}
}
public void toggle2(bool isOn)
{
if (isOn)
{
targetHorizatorPosition = pageArray[1];
}
}
public void toggle3(bool isOn)
{
if (isOn)
{
targetHorizatorPosition = pageArray[2];
}
}
public void toggle4(bool isOn)
{
if (isOn)
{
targetHorizatorPosition = pageArray[3];
}
}
public void toggle5(bool isOn)
{
if (isOn)
{
targetHorizatorPosition = pageArray[4];
}
}
}
{
IsDraging = false;
float posX = Scrollrect.horizontalNormalizedPosition;//从滑动开始到结束的滑动距离
//Scrollrect.horizontalNormalizedPosition 横向滑动的距离介于O到1之间
float offsetl = Mathf.Abs(pageArray[index] - posX);//左偏移的距离
for (int i = 0; i < pageArray.Length; i++)
{
float offsetr = Mathf.Abs(pageArray[i] - posX);
if (offsetl>offsetr)
{
index = i;
}
}
targetHorizatorPosition = pageArray[index];
toggle[index].isOn = true;
}
public void toggle1(bool isOn)
{
if (isOn)
{
targetHorizatorPosition = pageArray[0];
}
}
public void toggle2(bool isOn)
{
if (isOn)
{
targetHorizatorPosition = pageArray[1];
}
}
public void toggle3(bool isOn)
{
if (isOn)
{
targetHorizatorPosition = pageArray[2];
}
}
public void toggle4(bool isOn)
{
if (isOn)
{
targetHorizatorPosition = pageArray[3];
}
}
public void toggle5(bool isOn)
{
if (isOn)
{
targetHorizatorPosition = pageArray[4];
}
}
}
重点为拖拽过程中的算法
拓展:卡片类拖拽方法:继承IDropHandler, IPointerEnterHandler, IPointerExitHandler这三个接口,同样也实现对应的三个方法OnDrop、OnPointerEnter和OnPointerExit。其中,OnDrop处理松开鼠标左键时要做什么事,OnPointerEnter处理鼠标指针进入挂载该脚本的物体区域时要做什么事,OnPointerExit处理处理鼠标指针移出挂载该脚本的物体区域时要做什么事