public void Awake() { scroRect = transform.gameObject.AddComponent <MyScrollRect>(); scroRect.horizontal = false; scroRect.OnScrollDraged = OnScrollDraged; RectTransform baseRect = scroRect.GetComponent <RectTransform>(); GameObject viewport = new GameObject("viewport"); RectTransform viewtrans = viewport.AddComponent <RectTransform>(); viewtrans.SetParent(transform, false); viewtrans.pivot = Vector2.up; viewtrans.anchorMin = Vector2.zero; viewtrans.anchorMax = Vector2.one; viewtrans.anchoredPosition3D = Vector3.zero; viewtrans.sizeDelta = Vector2.zero; viewport.AddComponent <CanvasRenderer>(); viewport.AddComponent <Image>().raycastTarget = false; viewport.AddComponent <Mask>().showMaskGraphic = false; contenttrans = new GameObject("content").AddComponent <RectTransform>(); contenttrans.SetParent(viewtrans, false); contenttrans.pivot = Vector2.up; contenttrans.anchorMin = Vector2.up; contenttrans.anchorMax = Vector2.up; contenttrans.anchoredPosition3D = Vector3.zero; contenttrans.sizeDelta = Vector2.zero; content = contenttrans.gameObject.AddComponent <SchemeImageContent>(); content.scrollRect = scroRect; contentRect = content.contentTrans; scroRect.content = contentRect; // 设置布局 SetData(420, 672, 30, 70, 6, 2, 20, refreshDis, content.itemPrefab); // 测试 数据 ItemData[] data = new ItemData[20]; Display(data); }
void OnDestroy() { myContent = null; }