unity中对于scrollview下拉加载的方法
2015-10-29 10:13
489 查看
首先提供下NGUI工具接口
然后改造一下NGUI的UIScrollView.cs脚本,添加onDrag托管,并添加到Drag()中调用
好了,创建列表
然后定义托管回掉函数
创建Label的接口如下
好了,其他逻辑请自己脑补吧
/// <summary> /// 创建Panel /// </summary> /// <param name="parent">父对象</param> /// <param name="panelName">名称</param> /// <param name="pos">坐标</param> /// <returns>Panel游戏对象</returns> public static GameObject createPanel(GameObject parent, string panelName, Vector3 pos) { if (parent == null) return null; int depth = UIPanel.nextUnusedDepth; UIPanel panel = NGUITools.AddChild<UIPanel>(parent); panel.depth = depth; panel.gameObject.name = panelName; panel.transform.localPosition = pos; panel.gameObject.layer = parent.layer; return panel.gameObject; } /// <summary> /// 创建可拖拽的裁剪区 /// </summary> /// <param name="dragPanel">要被裁剪的Panel</param> /// <param name="DragCenterAndRange">裁剪区区域和大小</param> /// <param name="moveDiract">拖拉方向</param> /// <param name="piovt">锚点</param> /// <param name="clipSoftness">裁剪力度</param> /// <returns>UIScrollView对象</returns> public static UIScrollView createClipAndDrag(GameObject dragPanel, Vector4 DragCenterAndRange, UIScrollView.Movement moveDiract, UIWidget.Pivot piovt, Vector2 clipSoftness) { UIPanel panel = dragPanel.GetComponent<UIPanel>(); panel.clipping = UIDrawCall.Clipping.SoftClip; //裁剪方式// panel.baseClipRegion = DragCenterAndRange; //裁剪区区域和大小// panel.clipSoftness = clipSoftness; //裁剪力度// UIScrollView scrollPanel = dragPanel.AddComponent<UIScrollView>(); scrollPanel.movement = moveDiract; //拖拉方向// scrollPanel.contentPivot = piovt; //锚点// scrollPanel.disableDragIfFits = true; return scrollPanel; }
然后改造一下NGUI的UIScrollView.cs脚本,添加onDrag托管,并添加到Drag()中调用
public OnDragNotification onDrag; /// <summary> /// Drag the object along the plane. /// </summary> 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; /// <summary> /// 创建列表 /// </summary> 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<UIPanel>().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的接口如下
/// <summary> /// 创建Label /// </summary> /// <param name="parent">父对象</param> /// <param name="labelName">名称</param> /// <param name="font">字体</param> /// <param name="pos">坐标</param> /// <param name="fontSize">大小</param> /// <returns>UILabel对象</returns> public static UILabel createLabel(GameObject parent, string labelName, UIFont font, Vector3 pos, int fontSize) { UILabel lbl = NGUITools.AddWidget<UILabel>(parent); lbl.gameObject.name = labelName; lbl.bitmapFont = font; lbl.fontSize = fontSize; Transform transform = lbl.gameObject.transform; transform.localPosition = pos; return lbl; }
好了,其他逻辑请自己脑补吧
相关文章推荐
- Unity中单例模式的使用
- Unity3D之AssetBundle资源加载封装
- 新手引导
- Unity3DAssetBundle的坑
- 增强现实(AR)笔记:Vuforia及Unity3D
- unity抛物线,平均速度下的运动轨迹
- Unity如何将包缩小到极致
- 自学Unity之做一个MOBA游戏4——小兵
- unity之RotateAround 一个物体围绕另外一个物体旋转
- OccTransVF
- XRay7
- UNITY 5.2 Application类
- Xray6
- Unity Shader入门
- 【Unity】模型高度计算
- unity3d中ProtoBuf的序列化和反序列化c#
- unity3d各种OpenFileDialog操作
- unity, editable mesh
- unity 5.0.1:nugi自适应手机分辨率
- Unity3D优化建议