说一个关于unity3d uiwrapcontent的列子,有源代码,供大家参考学习,这个也是NGUI 3.5.5中加入的UIWrapContent,它会将不显示的Scroll Item设为disabled,这样就使得每帧更新的Scroll Item减少到当前显示的那几个,就再也不会出现掉帧的情况了。

不过UIWrapContent只能创建循环的Scroll View,不过只需要简单的几处修改,就能实现在普通的Scroll View上。

复制一份UIWrapContent.cs,重命名为UIBetterGrid.cs,修改类名

  1. // line: 19 - 20, file: UIWrapContent.cs
  2. [AddComponentMenu("NGUI/Interaction/Wrap Content")]
  3. public class UIWrapContent : MonoBehaviour


  4. 修改后的代码如下:

  5. // line: 19 - 20, file: UIBetterGrid.cs
  6. [AddComponentMenu("NGUI/Interaction/Better Grid")]
  7. public class UIBetterGrid : MonoBehaviour


  8. 修改初始化代码

  9. // line: 52 - 54, file: UIWrapContent.cs
  10. mScroll.restrictWithinPanel = false;
  11. if (mScroll.dragEffect == UIScrollView.DragEffect.MomentumAndSpring)
  12. mScroll.dragEffect = UIScrollView.DragEffect.Momentum;


  13. 修改后的代码如下:

  14. // line: 52 - 54, file: UIBetterGrid.cs
  15. mScroll.restrictWithinPanel = true;
  16. //if (mScroll.dragEffect == UIScrollView.DragEffect.MomentumAndSpring)
  17. //mScroll.dragEffect = UIScrollView.DragEffect.Momentum;


  18. 注释创建首尾循环的代码

  19. // line 159 - 170, file: UIBetterGrid.cs 
  20. //if (distance < -extents)
  21. //{
  22. //t.localPosition += new Vector3(extents * 2f, 0f, 0f);
  23. //distance = t.localPosition.x - center.x;
  24. //UpdateItem(t, i);
  25. //}
  26. //else if (distance > extents)
  27. //{
  28. //t.localPosition -= new Vector3(extents * 2f, 0f, 0f);
  29. //distance = t.localPosition.x - center.x;
  30. //UpdateItem(t, i);
  31. //}

  32. // line 190 - 201, file: UIBetterGrid.cs
  33. //if (distance < -extents)
  34. //{
  35. //t.localPosition += new Vector3(0f, extents * 2f, 0f);
  36. //distance = t.localPosition.y - center.y;
  37. //UpdateItem(t, i);
  38. //}
  39. //else if (distance > extents)
  40. //{
  41. //t.localPosition -= new Vector3(0f, extents * 2f, 0f);
  42. //distance = t.localPosition.y - center.y;
  43. //UpdateItem(t, i);
  44. //}


  45. 修改UIScrollView.cs

  46. // line 173, file: UIWrapContent.cs
  47. mBounds = NGUIMath.CalculateRelativeWidgetBounds(mTrans, mTrans);  


  48. 修改后的代码如下:

  49. // line 173, file: UIBetterGrid.cs
  50. mBounds = NGUIMath.CalculateRelativeWidgetBounds(mTrans, mTrans,true);