void updateFunc(ScrollItem item, SObject data) { item.gameObject.SetActive(true); DefaultScrollItemData sd = (DefaultScrollItemData)data; item.transform.Find("Text").GetComponent <Text>().text = string.Format("{0}_{1}", sd.name, sd.id); }
/// <summary> /// 初始化ScrollView列表 /// </summary> public virtual void InitScrollView(List <T> scrollContent)// where T : ScrollItemInfo { ClearList(); if (scrollrect.verticalScrollbar != null) { scrollrect.verticalScrollbar.gameObject.SetActive(!autoHideScrollbar); } for (int i = 0; i < scrollContent.Count; i++) { RectTransform scrollitem = Instantiate(scrollItemPrefab) as RectTransform; scrollitem.SetParent(scrollrect.content); scrollitem.localPosition = Vector3.zero; scrollitem.localRotation = Quaternion.Euler(Vector3.zero); scrollitem.localScale = Vector3.one; ScrollItem <T> type = scrollitem.gameObject.GetComponent <ScrollItem <T> >(); if (type == null) { type = scrollitem.gameObject.AddComponent <ScrollItem <T> >(); } T info = scrollContent[i]; type.SetItemInfo(info, OnScrollEventActive); scrollitem.gameObject.SetActive(true); allScrollItemList.Add(scrollitem); } Value = -1; DisplayScrollView(true); }
public void DelSelfItem(int curIndex) { if (curIndex < _index || curIndex >= _index + itemShowCount) { Debug.LogError("ERROR"); return; } int maxValue = -1; for (int i = _itemList.Count; i > 0; --i) { ScrollItem item = _itemList[i - 1]; if (item.Index == curIndex) { GameObject.Destroy(item.gameObject); _itemList.Remove(item); } if (item.Index > maxValue) { maxValue = item.Index; } if (item.Index > curIndex) { item.Index -= 1; } } if (maxValue < ItemTotalCount - 1) { CreateOneItem(maxValue); } ItemTotalCount -= 1; }
// Token: 0x06002A40 RID: 10816 RVA: 0x00469F0C File Offset: 0x0046810C public void ButtonOnClick(ScrollItem sender) { if (sender && this.handler != null) { this.handler.ButtonOnClick(sender.gameObject, sender.m_BtnID1); } }
void updateFunc_3(ScrollItem item, SObject data) { item.gameObject.SetActive(true); DefaultScrollItemData sd = (DefaultScrollItemData)data; item.GetComponent <Text>().text = GetLongTextByData(sd); }
public void setContents(string[] contents) { RectTransform[] a = gameObject.GetComponentsInChildren <RectTransform> (); for (int b = 1; b < a.Length; b++) { Destroy(a [b].gameObject); } ((RectTransform)transform).sizeDelta = new Vector2(((RectTransform)transform).sizeDelta.x, height * contents.Length); for (int n = 0; n < contents.Length; n++) { ScrollItem item = GameObject.Instantiate(prefab); item.name += contents [n]; item.sc = this; item.n = n; item.transform.SetParent(transform, false); ((RectTransform)item.transform).anchoredPosition = new Vector2(((RectTransform)item.transform).anchoredPosition.x, -height * n); Text text = item.GetComponentInChildren <Text> (); if (text != null) { text.text = contents [n]; } } }
public void RemoveSelectedItem() { if (_selectedItem != null) { _selectedItem.SetSelected(false); _selectedItem = null; } }
/// <summary> /// Add a new it to scrollMenu /// </summary> /// <param name="scrollItem">the new item</param> public void AddItem(ScrollItem scrollItem) { if (scrollItem.item) { View item = scrollItem.item; item.PositionUsesPivotPoint = true; item.ParentOrigin = Tizen.NUI.ParentOrigin.Center; item.Size2D = _itemDimensions; if (scrollItem.title) { item.Add(scrollItem.title); } // If we have a shadow or reflection, we need an extra container to provide the correct draw order. if (scrollItem.shadow || scrollItem.reflection) { View itemContainer = new View(); itemContainer.PositionUsesPivotPoint = true; itemContainer.PivotPoint = Tizen.NUI.PivotPoint.Center; itemContainer.ParentOrigin = Tizen.NUI.ParentOrigin.Center; itemContainer.Size2D = _itemDimensions; itemContainer.Focusable = true; itemContainer.Position = GetItemPosition(_items.Count, _currentScrollPosition, false); if (scrollItem.reflection) { scrollItem.reflection.PositionUsesPivotPoint = true; scrollItem.reflection.Size2D = _itemDimensions; scrollItem.reflection.PivotPoint = Tizen.NUI.PivotPoint.BottomCenter; scrollItem.reflection.ParentOrigin = Tizen.NUI.ParentOrigin.BottomCenter; scrollItem.reflection.Position = new Position(0.0f, 40.0f, 0.0f); scrollItem.reflection.Scale = new Vector3(1.0f, -1.0f, 1.0f); scrollItem.reflection.SetProperty(scrollItem.reflection.GetPropertyIndex("ColorAlpha"), new PropertyValue(0.3f)); itemContainer.Add(scrollItem.reflection); } if (scrollItem.shadow) { itemContainer.Add(scrollItem.shadow); } itemContainer.Add(item); Add(itemContainer); // Replace the root item with the new container. scrollItem.item = itemContainer; } else { // Item only, set it as focusable and position it directly. item.Focusable = true; item.Position = GetItemPosition(_items.Count, _currentScrollPosition, false); Add(item); } _items.Add(scrollItem); } }
public void Insert(int index, object userData) { ScrollItem si = new ScrollItem() { Index = index, UserData = userData }; _Items.Insert(index, si); _Changed = true; }
public void ItemBtnExeFalse(ScrollItem inItem, string parStr) { falseEvent.Invoke(parStr); if (btnCantCtrlInEnd) { lastSelectItem.GetComponent <Image>().raycastTarget = true; } // Debug.Log("false" + inItem.name + "----" + parStr); }
public void Add(object userData) { ScrollItem si = new ScrollItem() { Index = _Items.Count, UserData = userData }; _Items.Add(si); _Changed = true; }
private void DisableItem(ScrollItem data) { if (data.Object != null) { if (data.Object.gameObject.activeSelf) { data.Object.gameObject.SetActive(false); } data.Object.transform.SetParent(null); } }
void VerticalInitItem() { for (int i = 0; i < row; i++) { obj = Instantiate(prefab); obj.transform.SetParent(grid.transform, false); obj.transform.localPosition = new Vector3(sv_width / 2, -(middle / 2 + item_height / 2 + (middle + item_height) * i), 0); ScrollItem item = new ScrollItem(); item.BindGameObject(obj); item.SetData(i + 1); itemList.AddLast(item); } }
void fillScroll() { scrollableArea.VisibleAreaLength = scrollableAreaDelegate.getVisibleAreaLength(scrollableArea.transform); int index = 0; float currentOffset = scrollableAreaDelegate.getScrollStartOffset(); while (!scrollableAreaDelegate.getIsScrollContentFull()) { var itemTemplate = scrollableAreaDelegate.getScrollableAreaItemTemplate(index); index++; itemTemplate.item.transform.parent = scrollableArea.contentContainer.transform; if (scrollableArea.scrollAxes == tk2dUIScrollableArea.Axes.XAxis) { itemTemplate.item.transform.localPosition = new Vector3(currentOffset, 0, 0); } else { itemTemplate.item.transform.localPosition = new Vector3(0, -currentOffset, 0); } itemTemplate.item.SetActive(false); ScrollItem scrollItem = new ScrollItem(); scrollItem.itemTemplate = itemTemplate; scrollItem.itemScrollPosition = currentOffset; m_cachedContentItems.Add(scrollItem); currentOffset += itemTemplate.itemStride + scrollableAreaDelegate.getScrollBetweenObjectsOffset(); } currentOffset -= scrollableAreaDelegate.getScrollBetweenObjectsOffset(); float newContentLength = currentOffset + scrollableAreaDelegate.getScrollEndOffset(); float topOffset = scrollableArea.Value * (scrollableArea.ContentLength - scrollableArea.VisibleAreaLength); if (!Mathf.Approximately(newContentLength, scrollableArea.ContentLength)) { // If all items are visible, we simply populate as needed if (newContentLength < scrollableArea.VisibleAreaLength) { scrollableArea.Value = 0; // no more scrolling } // The total size required to display all elements scrollableArea.ContentLength = newContentLength; // Rescale the previousOffset so it remains constant if (scrollableArea.ContentLength > 0) { scrollableArea.Value = topOffset / (scrollableArea.ContentLength - scrollableArea.VisibleAreaLength); } } UpdateListGraphics(); m_isInited = true; }
public void RemoveAt(int index) { if (index >= 0 && index < _Items.Count) { ScrollItem item = _Items[index]; if (item.Object != null) { item.Object.transform.SetParent(null); Destroy(item.Object); } _Items.RemoveAt(index); _Changed = true; } }
private void UpdateItemPosition(ScrollItem data) { RectTransform rt = data.Object.GetComponent <RectTransform>(); if (rt == null) { Debug.LogError("RectTransform not found"); return; } float itemW = _ScrollRect.content.rect.width; float itemH = itemW * ItemHeightFactor; UpdateItemPosition(rt, data.Index, itemH); }
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; } }
//创建item public void CreateItems() { //初始化方向和大小 int tempcount = 0; Vector3 dir = Vector2.left; float itemlen = item.rect.width; float rectlen = scrollRect.rect.width; //如果是竖直方向 if (scrollDirection == ScrollDirection.vertical) { dir = Vector2.up; itemlen = item.rect.height; rectlen = scrollRect.rect.height; } //计算实际需要的item大小 这里设定为覆盖满后+2个 tempcount = Mathf.CeilToInt(rectlen / itemlen) + 2; //初始化所有item的数组 items = new ScrollItem[tempcount]; //循环创建item for (int i = 0; i < tempcount; i++) { ScrollItem si = new ScrollItem(); GameObject go = Instantiate(item.gameObject) as GameObject; go.transform.SetParent(scrollRect); go.transform.localScale = Vector3.one; si.rt = go.GetComponent <RectTransform>(); si.index = i >= itemValues.Length ? i - itemValues.Length : i; si.rt.anchoredPosition = dir * (rectlen * 0.5f - (i + 0.5f) * itemlen); go.name = "item" + i.ToString(); if (i < itemNames.Length) { go.GetComponentInChildren <Text>().text = itemNames[i]; } if (i < itemImage.Length) { go.GetComponentInChildren <Image>().sprite = itemImage[i]; } go.SetActive(true); items[i] = si; } }
private void GpsItem(ScrollItem item) { //重新定位选择物体 mScrollRect.velocity = Vector3.zero; m_GPSCenterPosion = mScrollRect.transform.TransformPoint(m_CenterPosion); m_GPSCenterPosion = mContent.transform.InverseTransformPoint(m_GPSCenterPosion); if (item == null) { float dis = Mathf.Infinity; for (int i = 0; i < mItems.Count; i++) { float tempDis = 0; var it = mItems[i]; if (m_IsVertical) { tempDis = Mathf.Abs(m_GPSCenterPosion.y - it.gameObject.transform.localPosition.y + m_CenterOffset); } else { tempDis = Mathf.Abs(m_GPSCenterPosion.x - it.gameObject.transform.localPosition.x + m_CenterOffset); } if (m_CenterItem != null && (m_CenterItem == it || m_CenterItem.GroupIndex == it.GroupIndex)) { tempDis += m_SpaceDis / 2.0f; } if (tempDis < dis) { item = it; dis = tempDis; } } } if (m_IsVertical) { m_GPSCenterPosion.y = mContent.transform.localPosition.y + m_GPSCenterPosion.y - item.gameObject.transform.localPosition.y + m_CenterOffset; } else { m_GPSCenterPosion.x = mContent.transform.localPosition.x + m_GPSCenterPosion.x - item.gameObject.transform.localPosition.x + m_CenterOffset; } m_CenterItem = item; m_TempCenterItem = item; }
public void OnValueChanged(Vector2 pos) { int index = GetCurIndex(); if (index != _index && index > -1) { _index = index; for (int i = _itemList.Count; i > 0; --i) { ScrollItem item = _itemList[i - 1]; if (item.Index < index || item.Index >= index + itemShowCount) { _itemList.Remove(item); _unuseList.Enqueue(item); } } for (int i = index; i < index + itemShowCount; ++i) { if (i < 0) { continue; } if (i > ItemTotalCount - 1) { continue; } bool hasItem = false; foreach (ScrollItem item in _itemList) { if (item.Index == i) { hasItem = true; } } if (false == hasItem) { CreateOneItem(i); } } } }
protected override void FillData(ScrollItem item, int index) { if (!cacheDict.TryGetValue(item, out var elements)) { elements = item.GetComponent <UIElements>(); cacheDict.Add(item, elements); } var image = elements.GetObject <Image>("Icon"); var itemName = elements.GetObject <Text>("Name"); var desc = elements.GetObject <Text>("Desc"); var desc2 = elements.GetObject <Text>("Desc2"); if (image != null) { image.sprite = GetSprite(); } itemName.text = index.ToString(); }
public void ItemBtnExeTrue(ScrollItem inItem, string parStr) { currentSelectItem = inItem; if (lastSelectItem != null && lastSelectItem != currentSelectItem) { lastSelectItem.imageButton.CleanState(); } lastSelectItem = currentSelectItem; trueEvent.Invoke(parStr); if (btnCantCtrlInEnd) { currentSelectItem.GetComponent <Image>().raycastTarget = false; } // Debug.Log("true"+ inItem.name+"----"+parStr); }
private void EnableItem(ScrollItem data) { if (data.Object == null) { data.Object = Instantiate(ItemPrefab); data.Object.hideFlags = this.HideFlags; if (Initialize != null) { Initialize(data.Object, data.Index); } } if (data.Object.transform.parent == null) { data.Object.transform.SetParent(_ScrollRect.content, false); UpdateItemPosition(data); } if (!data.Object.gameObject.activeSelf) { data.Object.gameObject.SetActive(true); } }
private void UpdateItemPositions() { float itemW = _ScrollRect.content.rect.width; float itemH = itemW * ItemHeightFactor; for (int i = 0; i < _Items.Count; i++) { ScrollItem item = _Items[i]; item.Index = i; if (item.Object != null) { RectTransform rt = item.Object.GetComponent <RectTransform>(); if (rt == null) { Debug.LogError("RectTransform not found"); return; } UpdateItemPosition(rt, item.Index, itemH); } } }
private void UpdateCenter() { if (mIsUseSelect) { float sp = 0; if (m_IsVertical) { sp = Mathf.Abs(mScrollRect.velocity.y); } else { sp = Mathf.Abs(mScrollRect.velocity.x); } if (sp < 100) { if (m_TempCenterItem == null) { GpsItem(null); } else { mContent.transform.localPosition = Vector3.SmoothDamp(mContent.transform.localPosition, m_GPSCenterPosion, ref mVelocityPos, 0.15f); if (Vector3.Distance(mContent.transform.localPosition, m_GPSCenterPosion) < 5) { mIsUseSelect = false; m_TempCenterItem = null; mScrollRect.velocity = Vector3.zero; mContent.transform.localPosition = m_GPSCenterPosion; if (m_LastCenterGroup != m_CenterItem.GroupIndex) { m_LastCenterGroup = m_CenterItem.GroupIndex; CenterEvent(m_CenterItem); } } } } } }
// Token: 0x06002A2E RID: 10798 RVA: 0x00468F88 File Offset: 0x00467188 private ItemObject AddContentObj(Vector2 pos, int btnID, GameObject item = null) { ItemObject itemObject = new ItemObject(); GameObject gameObject = UnityEngine.Object.Instantiate(this.customItem) as GameObject; ScrollItem component = gameObject.GetComponent <ScrollItem>(); component.m_BtnID1 = btnID; component.m_Handler = this; RectTransform component2 = gameObject.GetComponent <RectTransform>(); if (component2) { this.tempV2.x = this.btnSzieX; this.tempV2.y = this.btnSzieY; this.SetPos(component2, this.tempV2, pos); gameObject.transform.SetParent(this.Content.transform, false); } itemObject.rectTransform = component2; itemObject.scrollItem = component; itemObject.gameObject = gameObject; return(itemObject); }
private void UpdateAsy(float floatIndex) { int desIndex = (int)(floatIndex - 0.5f) + m_CacheLine; if (desIndex > m_LastIndex) { desIndex = Mathf.Clamp(desIndex, 0, m_MaxLine); int count = Mathf.Clamp(m_LastIndex, 0, m_MaxLine); int cid = 0; for (int page = count; page < desIndex + 1; page++) { for (int n = 0; n < m_GroupNum; n++) { cid = page * m_GroupNum + n; if (m_MaxNum > cid) { ScrollItem com = null; if (cid < mItems.Count) { com = mItems[cid]; } else { mGroups.Add(1); com = new ScrollItem(mGroups.Last.gameObject); mItems.Add(com); } m_GLayPosition.HandTool(cid, com.gameObject.transform); com.SetIndexAndGroup(cid, page); UpdateEvent(com); } } } m_LastIndex = desIndex; } }
public void Set(int idx) { if (items == null || items.Length == 0) { return; } if (idx < 0) { idx = itemValues.Length - 1; } if (idx > itemValues.Length - 1) { idx = 0; } if (mid != null && left != null && right != null) { if (mid.index == idx) { return; } int p = idx - mid.index; int q = 0; //先找到当前item的位置 item的位置是不变的,只是left的 int start = 0; ScrollItem si = scrollDirection == ScrollDirection.horizontal ? left : right; for (int i = 0; i < scrollRect.childCount; i++) { if (items[i] == si) { start = i; break; } } // p = idx; q = si.index + p; if (q >= itemValues.Length) { // q = itemValues.Length - q; q = q - itemValues.Length; } if (q < 0) { q = itemValues.Length + q; } // for(int i = 0 ; i < items.Length;i++) // { // Debug.Log(start +" "+ items[start].index); // start++; // if (start >= items.Length) // start = 0; // } for (int i = 0; i < items.Length; i++) { items[start].index = q; if (q < itemNames.Length) { items[start].rt.GetComponentInChildren <Text>().text = itemNames[q]; } if (q < itemImage.Length) { items[start].rt.GetComponentInChildren <Image>().sprite = itemImage[q]; } q++; if (q >= itemValues.Length) { q = 0; } start++; if (start >= items.Length) { start = 0; } } Scrolling(); } }
public void Scrolling() { //初始化方向和大小 int tempcount = 0; Vector2 dir = Vector2.left; float itemlen = item.rect.width; float rectlen = scrollRect.rect.width; //判断是垂直的列表还是横向的列表 if (scrollDirection == ScrollDirection.horizontal) { delta.y = 0; } else { delta.x = 0; dir = Vector2.down; itemlen = item.rect.height; rectlen = scrollRect.rect.height; } if (items.Length > 0) { float scale = 1; left = items[0]; right = items[0]; mid = items[0]; // float horv = scrollDirection == ScrollDirection.horizontal ? it.rt.anchoredPosition.x : it.rt.anchoredPosition.y; foreach (ScrollItem it in items) { //滚动 it.rt.anchoredPosition += delta; //当前和中心点的阈值 Text tex = it.rt.GetChild(0).GetComponent <Text>(); Image img = null; float tango = it.rt.anchoredPosition.x; if (scrollDirection == ScrollDirection.vertical) { tango = it.rt.anchoredPosition.y; } scale = (tango) / rectlen * 0.8f; if (it.rt.childCount > 1) { img = it.rt.GetChild(1).GetComponent <Image>(); } if (Animation_Scale) { if (tex != null) { tex.fontSize = (int)(TexSize * (1 - Mathf.Abs(scale))); } if (img != null) { img.rectTransform.localScale = (1 - Mathf.Abs(tango / rectlen)) * ImgScale; } } if (Animation_Color) { if (tex != null) { tex.color = new Color(TexColor.r, TexColor.g, TexColor.b, (1 - Mathf.Abs(scale) * 2)); } if (img != null) { img.color = new Color(ImgColor.r, ImgColor.g, ImgColor.b, (1 - Mathf.Abs(scale) * 2)); } } if (Animation_Rotate) { if (tex != null) { if (scrollDirection == ScrollDirection.vertical) { tex.rectTransform.eulerAngles = new Vector3(90 * (scale), 0, 0); } else { tex.rectTransform.eulerAngles = new Vector3(0, 90 * (scale), 0); } } if (img != null) { if (scrollDirection == ScrollDirection.vertical) { img.rectTransform.eulerAngles = new Vector3(90 * (scale), 0, 0); } else { img.rectTransform.eulerAngles = new Vector3(0, 90 * (scale), 0); } } } if (Animation_Scale) { //获取文字信息 Color newcol = TexColor * (1 - Mathf.Abs(scale) * 2); tex.color = newcol; // for(int i = 0 ; i < it.rt.childCount;i++) // { // tex.fontSize = (int)(100 * (1 - Mathf.Abs(scale))); // } } // scale = (it.rt.anchoredPosition.x)/rectlen*0.5f; // //获取文字信息 // Text tex = it.rt.GetChild(0).GetComponent<Text>(); // //旋转角度 // tex.rectTransform.eulerAngles = new Vector3(0, 270*((it.rt.anchoredPosition.x)/rectlen*0.5f), 0); // //设置字体颜色 // Color newcol = TexColor*(1- Mathf.Abs(scale)); //// newcol.a = 1; // tex.color = newcol; //字体大小 // tex.fontSize = (int)(100 * (1 - Mathf.Abs(scale))); if (scrollDirection == ScrollDirection.horizontal) { //判定最左和最右的item if (it.rt.anchoredPosition.x < left.rt.anchoredPosition.x) { left = it; } if (it.rt.anchoredPosition.x > right.rt.anchoredPosition.x) { right = it; } if (Mathf.Abs(mid.rt.anchoredPosition.x) > Mathf.Abs(it.rt.anchoredPosition.x)) { mid = it; } } else { //判定最上和最下的item if (it.rt.anchoredPosition.y < left.rt.anchoredPosition.y) { left = it; } if (it.rt.anchoredPosition.y > right.rt.anchoredPosition.y) { right = it; } if (Mathf.Abs(mid.rt.anchoredPosition.y) > Mathf.Abs(it.rt.anchoredPosition.y)) { mid = it; } } } if (scrollDirection == ScrollDirection.horizontal) { //右边滑动的情况下 if (delta.x > 0) { if (right.rt.anchoredPosition.x > (rectlen + itemlen) * 0.5f) { right.rt.anchoredPosition = left.rt.anchoredPosition + dir * itemlen; right.index = left.index - 1; if (right.index < 0) { if (itemNames.Length > 0) { right.index = itemNames.Length - 1; } else if (itemImage.Length > 0) { right.index = itemImage.Length - 1; } else { right.index = 0; } } //修改item的值 if (right.index < itemNames.Length) { right.rt.GetComponentInChildren <Text>().text = itemNames[right.index]; } if (right.index < itemImage.Length) { right.rt.GetComponentInChildren <Image>().sprite = itemImage[right.index]; } } } //左边滑动的情况下 if (delta.x < 0) { if (left.rt.anchoredPosition.x < -(rectlen + itemlen) * 0.5f) { left.rt.anchoredPosition = right.rt.anchoredPosition - dir * itemlen; left.index = right.index + 1; if (left.index >= itemValues.Length) { left.index = 0; } //修改item的值 if (left.index < itemNames.Length) { left.rt.GetComponentInChildren <Text>().text = itemNames[left.index]; } if (left.index < itemImage.Length) { left.rt.GetComponentInChildren <Image>().sprite = itemImage[left.index]; } } } } else { //上边滑动的情况下 if (delta.y > 0) { if (right.rt.anchoredPosition.y > (rectlen + itemlen) * 0.5f) { right.rt.anchoredPosition = left.rt.anchoredPosition + dir * itemlen; right.index = left.index + 1; if (right.index >= itemNames.Length && right.index >= itemImage.Length) { right.index = 0; } //修改item的值 if (right.index < itemNames.Length) { right.rt.GetComponentInChildren <Text>().text = itemNames[right.index]; } if (right.index < itemImage.Length) { right.rt.GetComponentInChildren <Image>().sprite = itemImage[right.index]; } } } //下边滑动的情况下 if (delta.y < 0) { if (left.rt.anchoredPosition.y < -(rectlen + itemlen) * 0.5f) { left.rt.anchoredPosition = right.rt.anchoredPosition - dir * itemlen; left.index = right.index - 1; if (left.index < 0) { if (itemNames.Length > 0) { left.index = itemNames.Length - 1; } else if (itemImage.Length > 0) { left.index = itemImage.Length - 1; } else { left.index = 0; } } //修改item的值 if (left.index < itemNames.Length) { left.rt.GetComponentInChildren <Text>().text = itemNames[left.index]; } if (left.index < itemImage.Length) { left.rt.GetComponentInChildren <Image>().sprite = itemImage[left.index]; } } } } } }
public void CreateItemGroup(string[] btnGroupDisplayName, string[] btnGroupParameter) { nonStandFloor = new List <ScrollItem>(); allScrollItem = new List <ScrollItem>(); if (btnGroupParameter == null) { btnGroupParameter = btnGroupDisplayName; } ScrollItem[] lastScrollItem = scrollRect.content.GetComponentsInChildren <ScrollItem>(); // Debug.Log(lastScrollItem.Length); for (int i = 0; i < lastScrollItem.Length; i++) { if (lastScrollItem[i] != null) { DestroyObject(lastScrollItem[i].gameObject); } } Vector2 startPos = itemSpace; for (int i = 0; i < btnGroupDisplayName.Length; i++) { ScrollItem sItem = Instantiate(itemPrefab, new Vector3(0, 0, 0), new Quaternion(), scrollRect.content); Vector2 itemSize = sItem.SetupItem(btnGroupDisplayName[i], scroolItemHeight, startPos, scrollItemMinWidth, fontSize, paddingFactor); Vector2 iSpace = Vector2.zero; // sItem.imageButton.btnNameForRemote = "B" + i.ToString() + "_" + this.gameObject.name + btnGroupParameter[i]; sItem.imageButton.btnNameForRemote = Regex.Replace(sItem.imageButton.btnNameForRemote, @"[\u4e00-\u9fa5]", ""); RemoteGather.AddImageToGroup(sItem.imageButton, true); sItem.imageButton.RecordOrginState(); if (itemDirection == ItemDirection.水平) { itemSize = new Vector2(itemSize.x, 0); iSpace = new Vector2(itemSpace.x, 0); } else { itemSize = new Vector2(0, -itemSize.y); iSpace = new Vector2(0, itemSpace.y); } // if(i!= btnGroupDisplayName.Length-1) startPos += itemSize + iSpace; //设置item的按钮事件 BaseEventDelegate itemTrue = new BaseEventDelegate(); BaseEventDelegate itemfalse = new BaseEventDelegate(); itemTrue.parameterTargetSlot = new int[] { 0, 3 }; itemfalse.parameterTargetSlot = new int[] { 0, 3 }; itemTrue.parameterList = new EventParametar[2]; itemfalse.parameterList = new EventParametar[2]; itemTrue.parameterList[0].pObject = sItem; itemTrue.parameterList[1].pString = btnGroupParameter[i]; itemfalse.parameterList[0].pObject = sItem; itemfalse.parameterList[1].pString = btnGroupParameter[i]; itemTrue.excuteMethodName = "ItemBtnExeTrue"; itemfalse.excuteMethodName = "ItemBtnExeFalse"; itemTrue.targetMono = this; itemfalse.targetMono = this; itemTrue.currentEditorChooseFunName = "ScrollMenu/ItemBtnExeTrue"; itemTrue.lastEditorChooseFunName = "ScrollMenu/ItemBtnExeTrue"; itemfalse.currentEditorChooseFunName = "ScrollMenu/ItemBtnExeTrue"; itemfalse.lastEditorChooseFunName = "ScrollMenu/ItemBtnExeTrue"; sItem.imageButton.trueEventList.Add(itemTrue); sItem.imageButton.falseEventList.Add(itemfalse); allScrollItem.Add(sItem); //判断参数是否可以转成有效的数字,如果可以就是楼层 int temp; if (!int.TryParse(btnGroupParameter[i].Replace("F", ""), out temp)) { nonStandFloor.Add(sItem); } } if (itemDirection == ItemDirection.水平) { scrollRect.content.sizeDelta = new Vector2(startPos.x, scrollRect.GetComponent <RectTransform>().sizeDelta.y); //设置居中 if (startPos.x < (scrollRect.GetComponent <RectTransform>().sizeDelta.x - 200)) { Vector2 offset = new Vector2((scrollRect.GetComponent <RectTransform>().sizeDelta.x - 200 - startPos.x) * 0.5f, 0f); // Debug.Log(offset); foreach (ScrollItem s in scrollRect.content.GetComponentsInChildren <ScrollItem>()) { s.GetComponent <RectTransform>().anchoredPosition += offset; s.imageButton.RecordOrginState(); } } } else { scrollRect.content.sizeDelta = new Vector2(scrollRect.GetComponent <RectTransform>().sizeDelta.x, -startPos.y); } }