1 using UnityEngine; 2 using System.Collections; 3 4 public class TurnPage : MonoBehaviour 5 { 6 public UIButton btn_leftOrTop; 7 public UIButton btn_rightOrButtom; 8 public int m_RowOrLineNum; // 当前界面显示的行数或者列数 9 public float strength = 8f; 10 11 private UIScrollView m_scrollView; 12 private Vector2 m_initialPosition; // scroll初始位置 13 private UIGrid grid; 14 private Bounds itemBounds; 15 16 private float allRowsNum; // 总共有多少列(行) 17 private float allPagesNum; // 总共有多少页 18 private float pageOffset; // 每页的偏移量 19 20 private void Start() 21 { 22 UIManager.SetButtonEventHandler(btn_leftOrTop.gameObject, EnumButtonEvent.OnClick, OnTurnLeftOrTop, 0, 0); 23 UIManager.SetButtonEventHandler(btn_rightOrButtom.gameObject, EnumButtonEvent.OnClick, OnTurnRightOrButtom, 0, 0); 24 btn_leftOrTop.enabled = false; 25 btn_rightOrButtom.enabled = false; 26 } 27 public IEnumerator DelayInit() 28 { 29 yield return new WaitForSeconds(0.5f); 30 OnInit(); 31 } 32 public void OnInit() 33 { 34 m_initialPosition = transform.localPosition; // 记录开始位置 35 m_scrollView = this.GetComponent<UIScrollView>();// 取到scroll 36 37 // 处理UIGrid 38 grid = this.GetComponentInChildren<UIGrid>(); 39 if (grid == null) 40 return; 41 42 float cutSize = 0; 43 switch (m_scrollView.movement) 44 { 45 case UIScrollView.Movement.Horizontal: 46 // 计算总共有多少列 47 itemBounds = NGUIMath.CalculateRelativeWidgetBounds(grid.GetChild(1).gameObject.transform); // 默认取第一个的子物体 48 cutSize = grid.cellWidth - itemBounds.size.x; 49 allRowsNum = (m_scrollView.bounds.size.x + cutSize) / grid.cellWidth; 50 allPagesNum = allRowsNum / m_RowOrLineNum;// 计算总共有多少页 51 pageOffset = (m_scrollView.bounds.size.x + cutSize) / allRowsNum * m_RowOrLineNum; // 计算每页的偏移量 52 break; 53 case UIScrollView.Movement.Vertical: 54 // 计算总共有多少行 55 itemBounds = NGUIMath.CalculateRelativeWidgetBounds(grid.GetChild(1).gameObject.transform); // 默认取第一个的子物体 56 cutSize = grid.cellHeight - itemBounds.size.y; 57 allRowsNum = (m_scrollView.bounds.size.y + cutSize) / grid.cellHeight; 58 allPagesNum = allRowsNum / m_RowOrLineNum;// 计算总共有多少页 59 pageOffset = (m_scrollView.bounds.size.y + cutSize) / allRowsNum * m_RowOrLineNum; // 计算每页的偏移量 60 break; 61 default: 62 break; 63 } 64 65 btn_leftOrTop.enabled = true; 66 btn_rightOrButtom.enabled = true; 67 } 68 69 private void OnTurnLeftOrTop(ButtonScript obj, object args, int param1, int param2) 70 { 71 Turn(false); 72 } 73 private void OnTurnRightOrButtom(ButtonScript obj, object args, int param1, int param2) 74 { 75 Turn(true); 76 } 77 private void Turn(bool forward) 78 { 79 float currPage; 80 float tempAllPage; 81 switch (m_scrollView.movement) 82 { 83 case UIScrollView.Movement.Horizontal: 84 currPage = GetCurrPage(forward, transform.localPosition.x); 85 tempAllPage = allPagesNum; 86 tempAllPage -= currPage; 87 88 if (tempAllPage < 1) 89 { 90 float movedX = (allRowsNum - m_RowOrLineNum) * (grid.cellWidth); 91 Vector3 toPositionX = new Vector3(-movedX, m_scrollView.transform.localPosition.y, m_scrollView.transform.localPosition.z); 92 SpringPanel.Begin(m_scrollView.panel.cachedGameObject, toPositionX, strength); 93 m_scrollView.panel.clipOffset.Set(-transform.localPosition.x, m_scrollView.panel.clipOffset.y); 94 } 95 else 96 { 97 float movedX = currPage * grid.cellWidth * m_RowOrLineNum; 98 Vector3 toPositionX = new Vector3(-movedX, m_scrollView.transform.localPosition.y, m_scrollView.transform.localPosition.z); 99 SpringPanel.Begin(m_scrollView.panel.cachedGameObject, toPositionX, strength); 100 m_scrollView.panel.clipOffset.Set(-transform.localPosition.x, m_scrollView.panel.clipOffset.y); 101 } 102 103 break; 104 case UIScrollView.Movement.Vertical: 105 currPage = GetCurrPage(forward, transform.localPosition.y); 106 tempAllPage = allPagesNum; 107 tempAllPage -= currPage; 108 if (tempAllPage < 1) 109 { 110 float movedY = (allRowsNum - m_RowOrLineNum) * (grid.cellHeight); 111 Vector3 toPositionY = new Vector3(m_scrollView.transform.localPosition.x, movedY, m_scrollView.transform.localPosition.z); 112 SpringPanel.Begin(m_scrollView.panel.cachedGameObject, toPositionY, strength); 113 m_scrollView.panel.clipOffset.Set(transform.localPosition.x, -m_scrollView.panel.clipOffset.y); 114 } 115 else 116 { 117 float movedY = currPage * grid.cellHeight * m_RowOrLineNum; 118 Vector3 toPositionY = new Vector3(m_scrollView.transform.localPosition.x, movedY, m_scrollView.transform.localPosition.z); 119 SpringPanel.Begin(m_scrollView.panel.cachedGameObject, toPositionY, strength); 120 m_scrollView.panel.clipOffset.Set(transform.localPosition.x, -m_scrollView.panel.clipOffset.y); 121 } 122 123 break; 124 } 125 126 127 } 128 private float GetCurrPage(bool forward, float positionValue) 129 { 130 float currPageNum = Mathf.Abs(positionValue / pageOffset); 131 132 if (forward) 133 currPageNum += 1; 134 else 135 { 136 currPageNum -= 1; 137 if (currPageNum < 0) 138 currPageNum = 0; 139 } 140 return currPageNum; 141 } 142 private void OnDestroy() 143 { 144 UIManager.RemoveButtonEventHandler(btn_leftOrTop.gameObject, EnumButtonEvent.OnClick); 145 UIManager.RemoveButtonEventHandler(btn_rightOrButtom.gameObject, EnumButtonEvent.OnClick); 146 } 147 }
时间: 2024-10-12 02:47:59