private void OnClick(object data, ScrollItem item) { if (_onClickHandler != null) { _onClickHandler(data); } if (ShowSelected) { if (_selectedItem != null) { _selectedItem.SetSelected(false); } item.SetSelected(true); _selectedItem = item; } }
public void SetData(ArrayList list) { if (!_isInit) { Init(); } _dataAmount = list.Count; _currentIndex = _gridAmount; if (list.Count == 0) { for (int i = 0; i < _itemList.Count; i++) { for (int j = 0; j < _itemList[i].Count; j++) { _itemList[i][j].gameObject.SetActive(false); } } return; } else { for (int i = 0; i < _itemList.Count; i++) { for (int j = 0; j < _itemList[i].Count; j++) { _itemList[i][j].gameObject.SetActive(true); } } } _dataList.Clear(); int count = 0; int dataGroupAmount = (int)Mathf.Ceil((float)list.Count / (float)_gridElementAmount); for (int i = 0; i < dataGroupAmount; i++) { ArrayList arrayList = new ArrayList(); _dataList.Add(arrayList); for (int j = 0; j < _gridElementAmount; j++) { if (count < list.Count) { _dataList[i].Add(list[count]); count++; } } } bool isShort; //內容物比ScrollView還小 if (Direction == Type.Horizontal) { isShort = _itemLength * _dataList.Count + Spacing.x * (_dataList.Count - 1) < Mask.rectTransform.rect.width; ScrollView.enabled = !isShort; if (ScrollView.horizontalScrollbar != null) { ScrollView.horizontalScrollbar.gameObject.SetActive(!isShort); } if (isShort) { Content.rectTransform.sizeDelta = new Vector2(_itemLength * (_gridAmount - PreparationAmount) + Spacing.x * ((_gridAmount - PreparationAmount) - 1), _itemSubLength * _gridElementAmount + Spacing.y * (_gridElementAmount - 1)); } else { Content.rectTransform.sizeDelta = new Vector2(_itemLength * _dataList.Count + Spacing.x * (_dataList.Count - 1), _itemSubLength * _gridElementAmount + Spacing.y * (_gridElementAmount - 1)); } Content.transform.position = new Vector3(Mask.transform.position.x - Mask.rectTransform.rect.width / 2 * _canvasScale + Content.rectTransform.sizeDelta.x / 2 * _canvasScale, Content.transform.position.y); for (int i = 0; i < _gridAmount; i++) { if (isShort) { _gridList[i].transform.localPosition = new Vector3((((_gridAmount - PreparationAmount) / 2f) - i - 0.5f) * (_itemLength + Spacing.x) * -1f, (_gridElementAmount / 2 - _isEven * 0.5f) * (_itemSubLength + Spacing.y)); } else { _gridList[i].transform.localPosition = new Vector3(((_dataList.Count / 2f) - i - 0.5f) * (_itemLength + Spacing.x) * -1f, (_gridElementAmount / 2 - _isEven * 0.5f) * (_itemSubLength + Spacing.y)); } for (int j = 0; j < _gridElementAmount; j++) { if (i < _dataList.Count && j < _dataList[i].Count) { _itemList[i][j].SetData(_dataList[i][j]); _itemList[i][j].transform.SetParent(_gridList[i].transform); } else { _itemList[i][j].gameObject.SetActive(false); } } } _lowerBound = _gridList[0].transform.position.x - _itemLength / 2; } else { isShort = _itemLength * _dataList.Count + Spacing.y * (_dataList.Count - 1) < Mask.rectTransform.rect.height; ScrollView.enabled = !isShort; if (ScrollView.verticalScrollbar != null) { ScrollView.verticalScrollbar.gameObject.SetActive(!isShort); } if (isShort) { Content.rectTransform.sizeDelta = new Vector2(_itemSubLength * _gridElementAmount + Spacing.x * (_gridElementAmount - 1), _itemLength * (_gridAmount - PreparationAmount) + Spacing.y * (_gridAmount - PreparationAmount - 1)); } else { Content.rectTransform.sizeDelta = new Vector2(_itemSubLength * _gridElementAmount + Spacing.x * (_gridElementAmount - 1), _itemLength * _dataList.Count + Spacing.y * (_dataList.Count - 1)); } Content.transform.position = new Vector3(Content.transform.position.x, Mask.transform.position.y + Mask.rectTransform.rect.height / 2 * _canvasScale - Content.rectTransform.sizeDelta.y / 2 * _canvasScale); for (int i = 0; i < _gridAmount; i++) { if (isShort) { _gridList[i].transform.localPosition = new Vector3(-1 * (_gridElementAmount / 2 - _isEven * 0.5f) * (_itemSubLength + Spacing.x), (((_gridAmount - PreparationAmount) / 2f) - i - 0.5f) * (_itemLength + Spacing.y)); } else { _gridList[i].transform.localPosition = new Vector3(-1 * (_gridElementAmount / 2 - _isEven * 0.5f) * (_itemSubLength + Spacing.x), ((_dataList.Count / 2f) - i - 0.5f) * (_itemLength + Spacing.y)); } for (int j = 0; j < _gridElementAmount; j++) { if (i < _dataList.Count && j < _dataList[i].Count) { _itemList[i][j].SetData(_dataList[i][j]); _itemList[i][j].transform.SetParent(_gridList[i].transform); } else { _itemList[i][j].gameObject.SetActive(false); } } } _upperBound = _gridList[0].transform.position.y + _itemLength / 2; } if (_selectedItem != null) { _selectedItem.SetSelected(false); } _selectedItem = null; //_onClickHandler = null; //_onDownHandler = null; //_onPressHandler = null; //_onUpHandler = null; }