首先提供下NGUI工具接口
/// /// 创建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;}
然后改造一下NGUI的UIScrollView.cs脚本,添加onDrag托管,并添加到Drag()中调用
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 momentumif (dragEffect == DragEffect.None) mMomentum = Vector3.zero;else mMomentum = Vector3.Lerp(mMomentum, mMomentum + offset * (0.01f * momentumAmount), 0.67f);// Move the scroll viewif (!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 boundsif (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 !&#61; m_loadMoreTips){//创建更多项目//}}private void onPanelOnDrag(){if (null &#61;&#61; m_dragPanel || null &#61;&#61; m_scrollView) return;Vector3 constraint &#61; m_dragPanel.GetComponent().CalculateConstrainOffset(m_scrollView.bounds.min, m_scrollView.bounds.max);if (null &#61;&#61; m_loadMoreTips && constraint.y <0){m_loadMoreTips &#61; NGUIComponentFactory.createLabel(m_dragPanel, "tipsLoadMore", ResourceManager.instance.myFont,new Vector3(0, 250f - 102f * m_curDisplayNum - 85f, 0), 30);m_loadMoreTips.overflowMethod &#61; UILabel.Overflow.ResizeFreely;m_loadMoreTips.text &#61; "加载更多";}}
创建Label的接口如下
/// /// 创建Label/// /// 父对象/// 名称/// 字体/// 坐标/// 大小/// UILabel对象public static UILabel createLabel(GameObject parent, string labelName, UIFont font, Vector3 pos, int fontSize){UILabel lbl &#61; NGUITools.AddWidget(parent);lbl.gameObject.name &#61; labelName;lbl.bitmapFont &#61; font;lbl.fontSize &#61; fontSize;Transform transform &#61; lbl.gameObject.transform;transform.localPosition &#61; pos;return lbl;}
好了&#xff0c;其他逻辑请自己脑补吧