【Unity3D】 UGUI实现ScrollView上下拉刷新
2019-03-08 14:46
127 查看
上下拉刷新在工程中经常用到,但是看到网上的一些例子感觉稍显复杂,这里给出自己写的一个简单例子。继承自ScrollRect
[code]using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class ScrollRectRef : ScrollRect { //高度 往下拉是负数 往上拉是正数 float f = -30f; //是否刷新 bool isRef = false; //是否处于拖动 bool isDrag = false; //显示、隐藏刷新字段 public Callback<bool> callback1; //如果满足刷新条件 执行的方法 public Callback callback2; protected override void Awake() { base.Awake(); onValueChanged.AddListener(ScrollValueChanged); } /// <summary> /// 当ScrollRect被拖动时 /// </summary> /// <param name="vector">被拖动的距离与Content的大小比例</param> void ScrollValueChanged(Vector2 vector) { //如果不拖动 当然不执行之下的代码 if (!isDrag) return; //这个就是Content RectTransform rect = GetComponentInChildren<ContentSizeFitter>().GetComponent<RectTransform>(); //如果拖动的距离大于给定的值 if (f > rect.rect.height * vector.y) { isRef = true; callback1?.Invoke(true); } else { isRef = false; callback1?.Invoke(false); } } public override void OnBeginDrag(PointerEventData eventData) { base.OnBeginDrag(eventData); isDrag = true; } public override void OnEndDrag(PointerEventData eventData) { base.OnEndDrag(eventData); callback1?.Invoke(false); if (isRef) callback2?.Invoke(); isRef = false; isDrag = false; } }
给出另外一个测试脚本
[code]using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Test : MonoBehaviour { public Text text; public GameObject s; // Use this for initialization void Start () { CreateObjs(); GetComponent<ScrollRectRef>().callback1 = (state) => { s.SetActive(state); }; GetComponent<ScrollRectRef>().callback2 = CreateObjs; } /// <summary> /// 创建列表 /// </summary> void CreateObjs() { for (int i = 0; i < 7; i++) { Text t = Instantiate(text, text.transform.parent, false); t.gameObject.SetActive(true); t.text = (text.transform.parent.childCount - 1).ToString(); } } }
第一次上传 ,如有错误 ,请多多指正
相关文章推荐
- 不借助第三方插件利用ScrollView自身delegate实现下拉刷新和上拉加载
- Unity3D中使用UGUI实现省市选择器
- Unity3d之ScrollView实现图片浏览切换功能----折磨的学习
- [unity3d]UGUI实现unity摇杆
- Android 下拉刷新控件之ScrollView版本实现
- Android 下拉刷新控件之ScrollView版本实现
- ScrollView 实现下拉刷新(二)
- React-native IT喵~ ScrollView 嵌套ListView 如何实现上拉加载、下拉刷新
- 时光煮雨 Unity3D让物体动起来③—UGUI DoTween&Unity Native2D实现
- Android 下拉刷新控件之ScrollView版本实现 .
- Android UI设计之<十三>自定义ScrollView,实现QQ空间阻尼下拉刷新和渐变菜单栏效果
- UGUI实现ScrollView无限滚动效果
- Unity3d UGUI 滚动视图ScrollView的使用教程之简易双肩包
- Unity3D UGUI实现冷却时间的技能图标
- [原创]unity3D学习【功能实现】之六:延续功能五,血条,点击减血等ugui
- <Android>使用ScrollView 实现 ListView 的下拉刷新
- Unity3D实现2D人物动画① UGUI&Native2D序列帧动画
- Unity3D实现UGUI 图片拖拽旋转和拖拽移动
- Unity3d UGUI 通用Confirm确认对话框实现(Inventory Pro学习总结)
- Unity3D在2D游戏中利用UGUI实现分辨率自适应的一种可行实践方案