UI 滑动面板的移动控制

通过拖拽的方式移动面板,在单个界面看到多个视图面板

需要导入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/500, 0, 0), Space.World);
   }
    
    // Update is called once per frame
    void Update () {
        if (this.transform.localPosition.x > 0)
        {
            //移动范围受限处理(左边缘)
            this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(0, 0, 0), Time.deltaTime * FloSliderPanelSmoothPara);
        }

        if (this.transform.localPosition.x < -3200)
        {
            //移动范围受限处理(右边缘)
            this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(-3200, 0, 0), Time.deltaTime * FloSliderPanelSmoothPara);
        }

        //整版“卡位”
        if (this.transform.localPosition.x < 0 && this.transform.localPosition.x>=-800)
        {
            this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(-800, 0, 0), Time.deltaTime * FloSliderPanelSmoothPara);
        }
        else if (this.transform.localPosition.x < -800 && this.transform.localPosition.x >= -1600)
        {
            this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(-1600, 0, 0), Time.deltaTime * FloSliderPanelSmoothPara);
        }
        else if (this.transform.localPosition.x < -1600 && this.transform.localPosition.x >= -2400)
        {
            this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(-2400, 0, 0), Time.deltaTime * FloSliderPanelSmoothPara);
        }
        else if (this.transform.localPosition.x < -2400 && this.transform.localPosition.x >= -3200)
        {
            this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(-3200, 0, 0), Time.deltaTime * FloSliderPanelSmoothPara);
        }
    }
}

之后对滑动面板进行完善:localPosition相对于父对象的位置,需要用到一个重要的函数Vector3.Lerp(),

这是一个差值处理函数,Lerp中有3个参数,(from,to,0-1之间的数),to参数是对位置的限制,相当于是摄像机跟随,很灵活。

原文地址:https://www.cnblogs.com/Optimism/p/9709913.html