using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Button = UnityEngine.UI.Button; using System; public class ScroolTest : MonoBehaviour { public GameObject btnObj; public Transform parentTrans; public GameObject scrollViewObj; private Vector2 scroSize; public static ScroolTest Instance { get; protected set; } public event Action ButtonBeClickEvent; // Start is called before the first frame update void Start() { } void Awake() { if (Instance == null) Instance = this; } // Update is called once per frame void Update() { } /// /// 初始化滑动视图 /// /// 需要展示的Item的名称数组 /// 滚动视图的位置 /// 滚动视图的大小 /// Item的大小 public void InitScrollview(string[] nameArr, Vector3 scrollViewPostion, Vector2 scrollViewSize, Vector2 contentSize) { //获取Scrollview并设置视图大小 scrollViewObj.GetComponent().sizeDelta = scrollViewSize; scrollViewObj.GetComponent().position = scrollViewPostion; GameObject content = GameObject.Find("Content"); content.GetComponent().cellSize = contentSize; //content.GetComponent().localScale = new Vector3(1, 1, 1);//重要的是这一句 for (int i = 0; i < nameArr.Length; i++) { CreatItem(i, nameArr[i]); } } public void CreatItem(int idx, string name) { GameObject obj = Instantiate(btnObj, parentTrans); Button oneBtn = obj.GetComponentInChildren