unity中对于scrollview下拉加载的方法
发布日期:2021-06-30 19:38:16
浏览次数:2
分类:技术文章
本文共 5789 字,大约阅读时间需要 19 分钟。
首先提供下NGUI工具接口
///然后改造一下NGUI的UIScrollView.cs脚本,添加onDrag托管,并添加到Drag()中调用/// 创建Panel /// /// 父对象 /// 名称 /// 坐标 ///Panel游戏对象 public static GameObject createPanel(GameObject parent, string panelName, Vector3 pos) { if (parent == null) return null; int depth = UIPanel.nextUnusedDepth; UIPanel panel = NGUITools.AddChild(parent); panel.depth = depth; panel.gameObject.name = panelName; panel.transform.localPosition = pos; panel.gameObject.layer = parent.layer; return panel.gameObject; } /// /// 创建可拖拽的裁剪区 /// /// 要被裁剪的Panel /// 裁剪区区域和大小 /// 拖拉方向 /// 锚点 /// 裁剪力度 ///UIScrollView对象 public static UIScrollView createClipAndDrag(GameObject dragPanel, Vector4 DragCenterAndRange, UIScrollView.Movement moveDiract, UIWidget.Pivot piovt, Vector2 clipSoftness) { UIPanel panel = dragPanel.GetComponent(); panel.clipping = UIDrawCall.Clipping.SoftClip; //裁剪方式// panel.baseClipRegion = DragCenterAndRange; //裁剪区区域和大小// panel.clipSoftness = clipSoftness; //裁剪力度// UIScrollView scrollPanel = dragPanel.AddComponent (); scrollPanel.movement = moveDiract; //拖拉方向// scrollPanel.contentPivot = piovt; //锚点// scrollPanel.disableDragIfFits = true; return scrollPanel; }
public OnDragNotification onDrag;///好了,创建列表/// Drag the object along the plane. /// public void Drag () { if (UICamera.currentScheme == UICamera.ControlScheme.Controller) return; if (enabled && NGUITools.GetActive(gameObject) && mShouldMove) { if (mDragID == -10) mDragID = UICamera.currentTouchID; UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta; // Prevents the drag "jump". Contributed by 'mixd' from the Tasharen forums. if (smoothDragStart && !mDragStarted) { mDragStarted = true; mDragStartOffset = UICamera.currentTouch.totalDelta; if (onDragStarted != null) onDragStarted(); } Ray ray = smoothDragStart ? UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos - mDragStartOffset) : UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos); float dist = 0f; if (mPlane.Raycast(ray, out dist)) { Vector3 currentPos = ray.GetPoint(dist); Vector3 offset = currentPos - mLastPos; mLastPos = currentPos; if (offset.x != 0f || offset.y != 0f || offset.z != 0f) { offset = mTrans.InverseTransformDirection(offset); if (movement == Movement.Horizontal) { offset.y = 0f; offset.z = 0f; } else if (movement == Movement.Vertical) { offset.x = 0f; offset.z = 0f; } else if (movement == Movement.Unrestricted) { offset.z = 0f; } else { offset.Scale((Vector3)customMovement); } offset = mTrans.TransformDirection(offset); } // Adjust the momentum if (dragEffect == DragEffect.None) mMomentum = Vector3.zero; else mMomentum = Vector3.Lerp(mMomentum, mMomentum + offset * (0.01f * momentumAmount), 0.67f); // Move the scroll view if (!iOSDragEmulation || dragEffect != DragEffect.MomentumAndSpring) { MoveAbsolute(offset); } else { Vector3 constraint = mPanel.CalculateConstrainOffset(bounds.min, bounds.max); if (constraint.magnitude > 1f) { MoveAbsolute(offset * 0.5f); mMomentum *= 0.5f; } else { MoveAbsolute(offset); } } // We want to constrain the UI to be within bounds if (restrictWithinPanel && mPanel.clipping != UIDrawCall.Clipping.None && dragEffect != DragEffect.MomentumAndSpring) { RestrictWithinBounds(true, canMoveHorizontally, canMoveVertically); } if (null != onDrag) onDrag(); } } }
private GameObject m_dragPanel; private UIScrollView m_scrollView; ///然后定义托管回掉函数/// 创建列表 /// private void createDragList() { m_dragPanel = NGUIComponentFactory.createPanel(m_gamePanel, "dragPanel", Vector3.zero); m_scrollView = NGUIComponentFactory.createClipAndDrag(m_dragPanel, new Vector4(0, 29.5f, 500, 360), UIScrollView.Movement.Vertical, UIWidget.Pivot.Top, Vector2.one); m_scrollView.onDragFinished = onPanelDragFinished; m_scrollView.onDrag = onPanelOnDrag; //创建列表项// }
private UILabel m_loadMoreTips; private int m_curDisplayNum; private void onPanelDragFinished() { if (null != m_loadMoreTips) { //创建更多项目// } } private void onPanelOnDrag() { if (null == m_dragPanel || null == m_scrollView) return; Vector3 constraint = m_dragPanel.GetComponent().CalculateConstrainOffset(m_scrollView.bounds.min, m_scrollView.bounds.max); if (null == m_loadMoreTips && constraint.y < 0) { m_loadMoreTips = NGUIComponentFactory.createLabel(m_dragPanel, "tipsLoadMore", ResourceManager.instance.myFont, new Vector3(0, 250f - 102f * m_curDisplayNum - 85f, 0), 30); m_loadMoreTips.overflowMethod = UILabel.Overflow.ResizeFreely; m_loadMoreTips.text = "加载更多"; } }
创建Label的接口如下
///好了,其他逻辑请自己脑补吧/// 创建Label /// /// 父对象 /// 名称 /// 字体 /// 坐标 /// 大小 ///UILabel对象 public static UILabel createLabel(GameObject parent, string labelName, UIFont font, Vector3 pos, int fontSize) { UILabel lbl = NGUITools.AddWidget(parent); lbl.gameObject.name = labelName; lbl.bitmapFont = font; lbl.fontSize = fontSize; Transform transform = lbl.gameObject.transform; transform.localPosition = pos; return lbl; }
转载地址:https://linxinfa.blog.csdn.net/article/details/49486793 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月12日 23时44分57秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Win10 Docker
2019-04-30
Python绘制动画并保存为gif/mp4 (matplotlib)
2019-04-30
PRM概率路线图
2019-04-30
ROS(六)——订阅者Subscriber的编程实现(C++ & Python)
2019-04-30
ROS(七)——话题消息的定义与使用
2019-04-30
yolov3入门实战
2019-04-30
B树 & B+树
2019-04-30
Node-Red(一)——简介与安装
2019-04-30
representation learning 表示学习/表征学习
2019-04-30
Haar特征
2019-04-30
Python 之 histogram直方图
2019-04-30
Python 之 Scatter散点图
2019-04-30
Python实现决策树 Desision Tree & 可视化
2019-04-30
决策树 Decision tree
2019-04-30
nominal和ordinal & 数据处理中四种基本数据类型
2019-04-30
Python 实现 Cross-validation
2019-04-30
Grid SearchCV(网格搜索)& Python实现
2019-04-30