public void Select(WheelItem item) { //update the current selection with the new middle _curSelection = item.ItemIndex; //call OnValueChange OnValueChange(item.ItemIndex, item.ItemText); }
/// <summary> /// Init the wheel. Instantiates itemCount wheelItemPrefabs and puts them in the right position. /// Then populates the visible wheelItems by calling SetData. /// </summary> /// <param name="choices">A list of all the possible values (strings) on the wheel</param> public void Init(params string[] choices) { if (_items == null) { _items = new List <WheelItem>(); } if (_sr == null) { _sr = GetComponent <ScrollRect>(); } prefabHeight = (wheelItemPrefab.transform as RectTransform).sizeDelta.y; itemData = choices; //create itemCount wheelItemPrefabs and put them in the right positions. int start = -(itemCount - 1) / 2; int end = (itemCount + 1) / 2; for (int i = start; i < end; i++) { WheelItem wi = AddWheelItem(i); _items.Add(wi); } SetData(choices); }
/// <summary> /// Instatiate a WheelItem (based on wheelItemPrefab) and adds it to content.transform /// </summary> /// <param name="offset">Offset the item height in offset * prefabHeight from the center.</param> /// <returns>The newly initiated WheelItem</returns> protected WheelItem AddWheelItem(int offset) { wheelItemPrefab.SetActive(true); GameObject go = (GameObject)Instantiate(wheelItemPrefab); go.name = offset.ToString(); RectTransform r = go.GetComponent <RectTransform>(); r.SetParent(content.transform, false); go.transform.localPosition = new Vector2(0, -(offset) * prefabHeight); WheelItem wi = go.GetComponent <WheelItem>(); wheelItemPrefab.SetActive(false); return(wi); }