unity-使用NGUI 两个Button实现左右切换功能

来源:互联网 发布:php 一句话木马 菜刀 编辑:程序博客网 时间:2024/05/21 23:46

界面搭建如下图:


在两个箭头的Button上添加脚本ClickToDragBtn.cs

using UnityEngine;//点击之后实现拖拽效果的切换public class ClickToDragBtn : MonoBehaviour{    /// <summary>    /// 枚举:按钮点击后要实现拖拽效果的方向    /// </summary>    public enum Orientation    {        None,        Left,        Right,    }    public Orientation orientaion = Orientation.Left;   //定义当前对象的朝向为:向左拖拽(--MenuBtn_index)    private UIDraggablePanel draggablePanel;    private UIGrid grid;    private UIScrollBar scrollbar;    private Vector4 vector;    void Awake()    {        draggablePanel = GameObject.Find("MenuPanel").GetComponent<UIDraggablePanel>();        grid = draggablePanel.transform.FindChild("UIGrid").GetComponent<UIGrid>();        scrollbar = GameObject.Find("SubPanel").transform.FindChild("MenuScrollBar").GetComponent<UIScrollBar>();    }    void LateUpdate()    {        vector = draggablePanel.GetComponent<UIPanel>().clipRange;        //获取裁剪区域        draggablePanel.GetComponent<UIPanel>().clipRange = new Vector4(vector.x, 0, vector.z, vector.w);        scrollbar.alpha = 0f;   //设置滑动条不可见    }    void OnClick()    {        if (orientaion == Orientation.Left && scrollbar.scrollValue < 1)        {            //如果方向是向左的,scrollbar的值改变控制滑动方向            scrollbar.scrollValue += 0.25f;        }        if (orientaion == Orientation.Right && scrollbar.scrollValue > 0)        {            scrollbar.scrollValue -= 0.25f;        }        //更新滑动面板        draggablePanel.LateUpdate();        //重置中心位置的对象        grid.GetComponent<UICenterOnChild>().Recenter();    }}
其中ScrollValue的值变化计算:

如果实现切换的按钮有5个,那么除去中间一个按钮,结果就为1/4;

如果实现切换的按钮有4个,那么除去中间一个按钮,结果就为1/3;

0 0
原创粉丝点击