private void InitUI() { Vector2 size = new Vector2(995.0f, 780.0f); Vector2 cellSize = new Vector2(210, 78); float spacing = 30f; float lineHeight = size.y / 6; GameObject scrollView = CreateUI.NewScrollView(size, BarType.Vertical, ContentType.Node); ContentSizeFitter contentSizeFitter = scrollView.GetComponentInChildren <ContentSizeFitter>(); if (contentSizeFitter != null) { contentSizeFitter.enabled = false; } scrollRect = scrollView.GetComponent <ScrollRect>(); scrollRect.verticalNormalizedPosition = 1; Image imgScrollView = scrollView.GetComponentInChildren <Image>(); imgScrollView.color = new Color(0.5f, 0.5f, 0.5f, 0.005f);// 测试时可以把透明度调成0.5可以看见底板大小是否正确 imgScrollView.raycastTarget = false; RectTransform rScrollView = ((RectTransform)scrollView.transform); rScrollView.SetParent(transform, false); rScrollView.anchoredPosition = new Vector2(0, 0); //scrollView.GetComponentInChildren<Mask>().enabled = false;//测试的时候设置不要遮罩方便看 GameObject itemCell = new GameObject("line", new System.Type[] { typeof(RectTransform) });// 创建一行的预制件 //itemCell.AddComponent<Image>().color = new Color(1, 0, 0, 0.5f);//测试的时候加个图片方便看到是否有创建 RectTransform rItemCell = itemCell.GetComponent <RectTransform>(); rItemCell.SetParent(transform, false); rItemCell.anchoredPosition = new Vector2(10000, 10000); // 把预制件设置到看不见的地方 rItemCell.sizeDelta = new Vector2(size.x, lineHeight); GameObject prefab = HomeSystem.instance.listActor; // 这里设置Item预制件 for (int i = 0; i < Main.settings.numberOfColumns; i++) // 初始化预制件 { GameObject go = UnityEngine.Object.Instantiate(prefab); go.transform.SetParent(rItemCell, false); } GridLayoutGroup gridLayoutGroup = itemCell.AddComponent <GridLayoutGroup>(); gridLayoutGroup.cellSize = cellSize; gridLayoutGroup.spacing = new Vector2(spacing, spacing); gridLayoutGroup.padding.left = (int)spacing; gridLayoutGroup.padding.top = (int)spacing; ActorItem actorItem = itemCell.AddComponent <ActorItem>(); bigDataScroll = gameObject.AddComponent <BigDataScroll>(); // 添加大数据滚动组件 bigDataScroll.Init(scrollRect, actorItem, SetCell); //初始化大数据滚动组件 bigDataScroll.cellHeight = lineHeight; //设置每行高度 // 设置滚动条图片 Transform parent = transform.parent; ScrollRect scroll = GetComponent <ScrollRect>();//获取原本的组件 Image otherBar = scroll.verticalScrollbar.GetComponent <Image>(); Image myBar = scrollRect.verticalScrollbar.GetComponent <Image>(); myBar.sprite = otherBar.sprite; myBar.type = Image.Type.Sliced; Image otherHand = scroll.verticalScrollbar.targetGraphic.GetComponent <Image>(); Image myHand = scrollRect.verticalScrollbar.targetGraphic.GetComponent <Image>(); myHand.sprite = otherHand.sprite; myHand.type = Image.Type.Sliced; // Main.Logger.Log("UI改造完毕"); //GuiBaseUI.Main.LogAllChild(transform.parent, true); isInit = true; SetData(); }