public void SetItem(handleSetData func, GameObject prefab) { if (prefab.GetComponent <ItemCell>() == null) { // Main.Logger.Log(prefab.ToString() + " don't find ItemCell"); return; } prefab.SetActive(false); if (!pool.ContainsKey(func)) { var v = new List <GameObject>(); v.Add(prefab); pool.Add(func, v); } else { var v = pool[func]; for (int i = 1; i < v.Count; i++) { GameObject.Destroy(v[(int)i]); } v.Clear(); v.Add(prefab); } }
public GameObject GetItem(handleSetData func, Transform parent) { if (pool.ContainsKey(func)) { var v = pool[func]; if (v != null && v.Count > 0) { GameObject go = null; if (v.Count > 1) { go = v[1]; v.RemoveAt(1); } else { go = GameObject.Instantiate <GameObject>(v[0]); } go.SetActive(true); Transform tf = go.transform; tf.SetParent(parent, false); return(go); } } // Main.Logger.Log("not set item"); return(null); }
public void PutItem(handleSetData func, GameObject item) { if (pool.ContainsKey(func)) { var v = pool[func]; if (v != null && v.Count > 0) { item.transform.SetParent(poolRoot, false); item.SetActive(false); v.Add(item); return; } } GameObject.Destroy(item); }
/// <summary> /// 初始化滚动 /// </summary> /// <param name="_scrollRect">ScrollRect组件</param> /// <param name="_itemcell">格子预制件的ItemCell组件</param> /// <param name="_handleSetData">委托回调方法 设置格子数据</param> public void Init(ScrollRect _scrollRect, ItemCell _itemcell, handleSetData _handleSetData, float wHeight = 0) { scrollRect = _scrollRect; m_funcSetData = _handleSetData; contentSizeFitter = scrollRect.content.GetComponent <ContentSizeFitter>(); verticalLayoutGroup = scrollRect.content.GetComponent <VerticalLayoutGroup>(); ItemPool.instance.SetItem(funcSetData, _itemcell.gameObject); gameObject = scrollRect.gameObject; transform = scrollRect.GetComponent <RectTransform>(); content = scrollRect.content; view = scrollRect.viewport; scrollRect.onValueChanged.AddListener(OnValueChanged); top = new GameObject("top", new Type[] { typeof(RectTransform) }).GetComponent <RectTransform>(); btm = new GameObject("button", new Type[] { typeof(RectTransform) }).GetComponent <RectTransform>(); top.SetParent(content, false); btm.SetParent(content, false); top.sizeDelta = default(Vector2); btm.sizeDelta = default(Vector2); }