public void AddItem() { string name = "Item" + (itemIndexCount++); GameObject item = new GameObject(name); Picker.PickerItem pickerItem = item.AddComponent <Picker.PickerItem>(); RectTransform rect = pickerItem.GetComponent <RectTransform>(); rect.sizeDelta = scrollRect.layout == RectTransform.Axis.Vertical ? verticalItemSize : horizontalItemSize; Text text = item.AddComponent <Text>(); text.font = itemTextFont; text.fontSize = 18; text.resizeTextForBestFit = true; text.color = Color.black; text.fontStyle = FontStyle.Bold; text.alignment = TextAnchor.MiddleCenter; text.text = name; item.transform.SetParent(layoutGroup.gameObject.transform); item.transform.localPosition = Vector3.zero; item.transform.localRotation = Quaternion.identity; item.transform.localScale = Vector3.one; scrollRect.UpdateLayout(); scrollRect.ScrollTo(scrollRect.lastItemPosition, true); }
public void ScrollTo(PickerItem item, bool immdiate = false) { if (item != null) { ScrollTo(item.position, immdiate); } }
public void UnregisterItem(PickerItem item) { if (Application.isPlaying) { m_ChildItemTable.Remove(item.transform); } }
protected void NotifySelectItem(PickerItem item) { if (beforeSelectedItem != item) { beforeSelectedItem = item; GameObject notify = (item != null ? item.gameObject : null); onSelectItem.Invoke(notify); } }
public void ScrollTo() { Transform item = layoutGroup.transform.GetChild(scrollTargetItemIndex); if (item != null) { Picker.PickerItem pickerItem = item.GetComponent <Picker.PickerItem>(); scrollRect.ScrollTo(pickerItem); //or scrollRect.ScrollTo( pickerItem.position ); } }
public void ScrollToNearItem(bool immdiate = false) { if (pickerLayoutGroup == null) { return; } PickerItem item = GetNearItem(); if (item != null) { ScrollTo(item.position, immdiate); } }
public void RegisterItem(PickerItem item) { if (Application.isPlaying) { Transform key = item.transform; if (!m_ChildItemTable.ContainsKey(key)) { m_ChildItemTable[key] = new ItemComponent() { item = item, rectTransform = item.GetComponent <RectTransform>() }; } } }
protected virtual void NotifySelectItem() { if (pickerLayoutGroup == null || content == null) { return; } List <float> itemOffsetList = pickerLayoutGroup.itemOffsetList; if (itemOffsetList.Count <= 0) { return; } PickerItem item = GetNearItem(); NotifySelectItem(item); }
public PickerItem[] GetSelectedItems() { PickerScrollRect[] columns = this.columns; if (columns == null) { return(new PickerItem[0]); } PickerItem[] selected = new PickerItem[columns.Length]; for (int i = 0; i < selected.Length; ++i) { if (columns[i] != null) { selected[i] = columns[i].GetSelectedPickerItem(); } } return(selected); }
static public void AddItem(GameObject parent, bool zoom, bool horizontal, int index) { string name = "Item" + index; GameObject item = new GameObject(name); GameObjectUtility.SetParentAndAlign(item, parent); PickerItem pickerItem = !zoom?item.AddComponent <PickerItem>() : item.AddComponent <ZoomPickerItem>(); RectTransform rect = pickerItem.GetComponent <RectTransform>(); if (!horizontal) { rect.sizeDelta = new Vector2(70, 20); } else { rect.sizeDelta = new Vector2(15, 70); } Text itemText = AddTextComponent(item, item.name); if (zoom) { GameObject zoomItem = new GameObject("ZoomItem"); GameObjectUtility.SetParentAndAlign(zoomItem, item); AddTextComponent(zoomItem, item.name); RectTransform zoomTransform = zoomItem.GetComponent <RectTransform>(); zoomTransform.sizeDelta = rect.sizeDelta; zoomTransform.localScale = Vector3.one * 1.2f; itemText.color = Color.gray; ((ZoomPickerItem)pickerItem).zoomItem = zoomTransform; } }
protected virtual void AutoScroll() { if (m_stopAutoScroll > 0) { m_stopAutoScroll -= Time.deltaTime; return; } if (m_autoScroll) { Vector2 anchoredPosition = content.anchoredPosition; if (anchoredPosition != m_targetPosition) { m_autoScrollTimeCount += Time.deltaTime; if (m_autoScrollTimeCount < autoScrollSeconds) { //Ease Out Quart float t = m_autoScrollTimeCount / autoScrollSeconds - 1; t *= t; t *= t; t = t - 1; content.anchoredPosition = (m_fromPosition - m_targetPosition) * t + m_fromPosition; } else { content.anchoredPosition = m_targetPosition; velocity = Vector2.zero; m_autoScroll = false; } if (wheelEffect || infiniteScroll) { UpdateLayout(); } } else { velocity = Vector2.zero; m_autoScroll = false; } } else if (!m_dragging) { int axisIndex = (int)layout; if (m_slip) { float slipVelocity = GetSlipVelocity(); if (Mathf.Abs(velocity[axisIndex]) <= slipVelocity) { if (pickerLayoutGroup != null) { List <float> itemOffsetList = pickerLayoutGroup.itemOffsetList; if (itemOffsetList != null && itemOffsetList.Count > 0) { Vector2 scrollPosition = content.anchoredPosition; scrollPosition[1 - axisIndex] = 0; float beforePosition = beforeScrollPosition[axisIndex]; float currentPosition = scrollPosition[axisIndex]; int beforeIndex = GetNearItemIndex(-beforePosition); int index = GetNearItemIndex(-currentPosition); if (beforeIndex == index && 0 <= index) { float itemOffset = -itemOffsetList[index]; if ((beforePosition - itemOffset) * (currentPosition - itemOffset) <= 0 || beforePosition == currentPosition) { RectTransform windowRect = content.parent as RectTransform; if (windowRect == null) { windowRect = content; } if (Mathf.Abs(itemOffset - currentPosition) < windowRect.rect.size[axisIndex] * 0.001f) { velocity = Vector2.zero; m_slip = false; content.anchoredPosition = scrollPosition.Assign(itemOffset, axisIndex); } else { ScrollTo(itemOffset); } } else if (velocity[axisIndex] * (currentPosition - itemOffset) < 0 || (index != 0 && index != itemOffsetList.Count - 1)) { velocity = velocity.Assign(slipVelocity * Mathf.Sign(velocity[axisIndex]), axisIndex); } } else { velocity = velocity.Assign(slipVelocity * Mathf.Sign(velocity[axisIndex]), axisIndex); } } } } } else { if (content != null) { PickerItem nearItem = GetNearItem(); float currentPosition = -content.anchoredPosition[axisIndex]; if (nearItem != null) { float itemOffset = nearItem.position; RectTransform windowRect = content.parent as RectTransform; if (windowRect == null) { windowRect = content; } if (Mathf.Abs(currentPosition + itemOffset) > windowRect.rect.size[axisIndex] * 0.001f) { ScrollTo(itemOffset); } } } } } }
protected virtual void SetLayout(int axis) { if (scrollRect == null) { return; } if (axis != GetAxisIndex()) { return; } if (m_LockSetLayout) { m_DirtyLayout = true; return; } Vector2 scrollAreaSize = GetScrollAreaSize(); m_Tracker.Clear(); rectChildren.Clear(); m_itemOffsetList.Clear(); m_pickerItemList.Clear(); m_childTransformList.Clear(); cacheRect.Clear(); bool infinite = m_ScrollRect.infiniteScroll; bool wheelEffect = m_ScrollRect.wheelEffect; WheelEffect3D wheelEffect3D = GetComponentInParent <WheelEffect3D>(); if (wheelEffect3D != null) { if (wheelEffect3D.IsActive() && (int)wheelEffect3D.layout == axis) { wheelEffect = true; } else { wheelEffect3D = null; } } DrivenTransformProperties childDriven = GetChildPropertyDriven(axis, wheelEffect); float position = (!infinite ? scrollAreaSize[axis] * 0.5f : 0); float scrollSize = position; for (int childIndex = 0; childIndex < rectTransform.childCount; childIndex++) { Transform childTransform = rectTransform.GetChild(childIndex); RectTransform childRectTransform; ItemComponent itemComponent; PickerItem item; if (!m_ChildItemTable.TryGetValue(childTransform, out itemComponent)) { childRectTransform = childTransform as RectTransform; item = childRectTransform.GetComponent <PickerItem>(); m_ChildItemTable[childTransform] = new ItemComponent() { item = item, rectTransform = childRectTransform }; } else { childRectTransform = itemComponent.rectTransform; item = itemComponent.item; } if (item == null || !item.enabled || !childRectTransform.gameObject.activeInHierarchy) { continue; } rectChildren.Add(childRectTransform); m_pickerItemList.Add(item); m_itemOffsetList.Add(0); m_childTransformList.Add(childRectTransform); m_Tracker.Add(this, childRectTransform, childDriven); { Vector2 anchorMin = childRectTransform.anchorMin; if (anchorMin[axis] != 0.5f) { anchorMin[axis] = 0.5f; childRectTransform.anchorMin = anchorMin; } Vector2 anchorMax = childRectTransform.anchorMax; if (anchorMax[axis] != 0.5f) { anchorMax[axis] = 0.5f; childRectTransform.anchorMax = anchorMax; } } Rect rect = childRectTransform.rect; cacheRect.Add(rect); float size = rect.size[axis]; scrollSize += spacing + size; } if (m_childTransformList.Count > 0 && !infinite) { float size = m_childTransformList[0].rect.size[axis]; position -= size * 0.5f; scrollSize -= size * 0.5f; size = m_childTransformList[m_childTransformList.Count - 1].rect.size[axis]; scrollSize -= size * 0.5f; scrollSize -= spacing; } if (!infinite) { float flex = Mathf.Min(scrollSize * 0.002f, 0.1f); scrollSize += scrollAreaSize[axis] * 0.5f + flex + flex; position += flex; } SetContentRectSize(axis, scrollSize); position += scrollSize * -0.5f; float pivot = m_ChildPivot - 0.5f; float direction = (axis == 0 ? 1f : -1f); float wheelPosition = scrollRect.content.localPosition[axis] * -direction; float infiniteOffsetFloor = 0; if (infinite) { infiniteOffsetFloor = Mathf.Floor(wheelPosition / scrollSize) * scrollSize; } if (wheelEffect) { float perspective = m_ScrollRect.wheelPerspective; float wheelRadius = wheelEffect3D != null ? wheelEffect3D.radius : scrollAreaSize[axis] * 0.5f; float circumference = (wheelRadius + wheelRadius) * Mathf.PI; float ru = (Mathf.PI + Mathf.PI) / circumference * direction; for (int childIndex = 0; childIndex < m_childTransformList.Count; ++childIndex) { RectTransform childTransform = m_childTransformList[childIndex]; float size = cacheRect[childIndex].size[axis]; float tmp; if (!infinite) { tmp = position + size * 0.5f; } else { tmp = infiniteOffsetFloor + position + size * 0.5f; if (Mathf.Abs(tmp - wheelPosition) > Mathf.Abs(tmp + scrollSize - wheelPosition)) { tmp += scrollSize; } } position += size + spacing; float rad = (tmp - wheelPosition) * ru; float offset = tmp * direction; m_itemOffsetList[childIndex] = offset; PickerItem item = m_pickerItemList[childIndex]; item.position = -offset; if (Mathf.Abs(rad) < Mathf.PI * 0.5f) { if (wheelEffect3D == null) { float scale = Mathf.Cos(rad); Vector3 localScale = childTransform.localScale; localScale[axis] = scale; float scale2 = (1 - perspective) + scale * perspective; localScale[1 - axis] = scale2; childTransform.localScale = localScale; Vector2 center = cacheRect[childIndex].center; Vector3 childPosition = Vector2.zero; childPosition[axis] = Mathf.Sin(rad) * wheelRadius + wheelPosition * direction - center[axis] * scale; float childPivot = childTransform.pivot[1 - axis] - 0.5f; float childAnchorMin = childTransform.anchorMin[1 - axis]; float childAnchorMax = childTransform.anchorMax[1 - axis]; float revision = (childPivot * (childAnchorMax - childAnchorMin) + (childAnchorMax + childAnchorMin - 1) * 0.5f) * scrollAreaSize[1 - axis]; childPosition[1 - axis] = (scrollAreaSize[1 - axis] - cacheRect[childIndex].size[1 - axis] * scale2) * pivot - center[1 - axis] * scale2 - revision; childPosition.z = 0; childTransform.localPosition = childPosition; if (childTransform.localRotation != Quaternion.identity) { childTransform.localRotation = Quaternion.identity; } } else { Vector2 center = cacheRect[childIndex].center; Vector3 childPosition = Vector3.zero; childPosition[axis] = Mathf.Sin(rad) * wheelRadius + wheelPosition * direction - center[axis]; Vector3 eulerAngles = childTransform.localRotation.eulerAngles; eulerAngles[1 - axis] = rad * -Mathf.Rad2Deg * direction; childTransform.localRotation = Quaternion.Euler(eulerAngles); float childPivot = childTransform.pivot[1 - axis] - 0.5f; float childAnchorMin = childTransform.anchorMin[1 - axis]; float childAnchorMax = childTransform.anchorMax[1 - axis]; float revision = (childPivot * (childAnchorMax - childAnchorMin) + (childAnchorMax + childAnchorMin - 1) * 0.5f) * scrollAreaSize[1 - axis]; childPosition[1 - axis] = (scrollAreaSize[1 - axis] - cacheRect[childIndex].size[1 - axis]) * pivot - center[1 - axis] - revision; childPosition.z = wheelRadius - Mathf.Cos(rad) * wheelRadius; if (childTransform.localScale != Vector3.one) { childTransform.localScale = Vector3.one; } childTransform.localPosition = childPosition; } } else { Vector3 scale = childTransform.localScale; if (scale.x != 0 || scale.y != 0) { scale.x = 0; scale.y = 0; childTransform.localScale = scale; } } } } else { for (int childIndex = 0; childIndex < m_childTransformList.Count; ++childIndex) { RectTransform childTransform = m_childTransformList[childIndex]; PickerItem item = m_pickerItemList[childIndex]; Rect rect = childTransform.rect; float size = rect.size[axis]; float tmp; if (!infinite) { tmp = position + size * 0.5f; } else { tmp = infiniteOffsetFloor + position + size * 0.5f; if (Mathf.Abs(tmp - wheelPosition) > Mathf.Abs(tmp + scrollSize - wheelPosition)) { tmp += scrollSize; } } float offset = tmp * direction; m_itemOffsetList[childIndex] = offset; item.position = -offset; position += size + spacing; Vector3 childPosition = childTransform.anchoredPosition; childPosition[axis] = tmp * direction - rect.center[axis]; float childPivot = childTransform.pivot[1 - axis] - 0.5f; float childAnchorMin = childTransform.anchorMin[1 - axis]; float childAnchorMax = childTransform.anchorMax[1 - axis]; float revision = (childPivot * (childAnchorMax - childAnchorMin) + (childAnchorMax + childAnchorMin - 1) * 0.5f) * scrollAreaSize[1 - axis]; childPosition[1 - axis] = (scrollAreaSize[1 - axis] - rect.size[1 - axis]) * pivot - rect.center[1 - axis] - revision; childPosition.z = 0; if (childTransform.localPosition != childPosition) { childTransform.localPosition = childPosition; } if (childTransform.localRotation != Quaternion.identity) { childTransform.localRotation = Quaternion.identity; } if (childTransform.localScale != Vector3.one) { childTransform.localScale = Vector3.one; } } } if (direction < 0) { m_itemOffsetList.Reverse(); m_pickerItemList.Reverse(); } int infiniteScrollOffset = 0; if (infinite) { //sort offset & item int i; int count = m_itemOffsetList.Count; if (count > 1) { for (i = 1; i < count; ++i) { if (m_itemOffsetList[i - 1] >= m_itemOffsetList[i]) { break; } } infiniteScrollOffset = i; if (i < count) { if (swapBufferOffset == null || i > swapBufferOffset.Length) { swapBufferOffset = new float[i]; swapBufferItem = new PickerItem[i]; } m_itemOffsetList.CopyTo(0, swapBufferOffset, 0, i); m_pickerItemList.CopyTo(0, swapBufferItem, 0, i); int j; for (j = 0; j + i < count; ++j) { m_itemOffsetList[j] = m_itemOffsetList[j + i]; m_pickerItemList[j] = m_pickerItemList[j + i]; } for (int k = 0; k + j < count; ++k) { m_itemOffsetList[k + j] = swapBufferOffset[k]; m_pickerItemList[k + j] = swapBufferItem[k]; } } } } if (scrollRect != null && Application.isPlaying) { scrollRect.SetInitialPosition(infiniteScrollOffset); } }