/*private void WrapContent () * { * Vector3[] conner_Local = new Vector3[4]; * for (int i = 0; i < 4; i++) { * conner_Local [i] = transform.InverseTransformPoint (conners [i]); * Debug.Log ("conner_Local[i]: " + conner_Local [i]); * } * Vector3 lastItemPos = _listItem.Count > 0 ? _listItem[_listItem.Count - 1].transform.localPosition : Vector3.zero; * Debug.Log("lastItemPos.y <= conner_Local[0].y : " + lastItemPos.y + "conner_Local[0].y" + conner_Local[0].y); * Debug.Log("itemTypeStart: " + itemTypeStart); * if (lastItemPos.y <= conner_Local[0].y || _listItem.Count == 0) * { * ChatContentIndexCallback temp = CreateItem(itemTypeStart); * temp.transform.SetAsFirstSibling(); * itemTypeStart--; * _listItem.Add(temp); * } * }*/ // Update is called once per frame void Update() { //if (_listItem.Count > 0) { //GameObject obj = _listItem[] //Debug.Log(_scrollRect.content.sizeDelta.y); if (_chatStateType == ChatStateType.initItem) { if (itemTypeStart > 0) { Vector3[] conner_Local = new Vector3[4]; for (int i = 0; i < 4; i++) { conner_Local[i] = itemParent.InverseTransformPoint(conners[i]); //Debug.Log ("conner_Local[i]: " + conner_Local [i]); } //Vector3 center = (conner_Local [0] + conner_Local [3]) / 2;//计算此时中心点的坐标 Vector3 lastItemPos = _listItem.Count > 0 ? _listItem [_listItem.Count - 1].transform.localPosition : Vector3.zero; //Debug.Log("lastItemPos.y <= conner_Local[0].y : " + lastItemPos.y + "conner_Local[0].y" + conner_Local[0].y); //Debug.Log("itemTypeStart: "+itemTypeStart); if (lastItemPos.y <= conner_Local[0].y || _listItem.Count == 0) { ChatContentIndexCallback temp = CreateItem(itemTypeStart); temp.transform.SetAsFirstSibling(); itemTypeStart--; _listItem.Add(temp); } else { startPostion = _scrollRect.content.position; _chatStateType = ChatStateType.empty; } } else { _chatStateType = ChatStateType.empty; } } else if (_chatStateType == ChatStateType.BotttomToTop) { //Debug.Log("itemTypeEnd: " + itemTypeEnd + "totalCount: " + totalCount); MoveBotttomToTop(); } else if (_chatStateType == ChatStateType.TopToBottom) { //Debug.Log("itemTypeStart: "+itemTypeStart); MoveTopToBottom(); } //startPostion = _scrollRect.content.anchoredPosition; }
//向上移动判断 private void MoveBotttomToTop() { if (itemTypeEnd < totalCount) { Vector3[] conner_Local = new Vector3[4]; for (int i = 0; i < 4; i++) { conner_Local[i] = itemParent.InverseTransformPoint(conners[i]); //Debug.Log("conner_Local[i]: " + conner_Local[i]); } Vector3 lastItemPos = _listItem.Count > 1 ? _listItem [_listItem.Count - 2].transform.localPosition : Vector3.zero; //移除上边不在范围的 if (lastItemPos.y > conner_Local[0].y) { if (_listItem.Count > 0) { ChatContentIndexCallback startItem = _listItem [_listItem.Count - 1]; startItem.gameObject.SetActive(false); _listItem.Remove(startItem); _pool.Add(startItem); itemTypeStart++; //Debug.Log ("delete"); } } //Debug.Log("BotttomToTop"); //增加新的 Vector3 endItemPos = _listItem.Count > 0 ? _listItem [0].transform.localPosition : Vector3.zero; //Debug.Log(endItemPos - _scrollRect.transform.position); //if (itemStartY2 <= itemEndTempY || _listItem.Count == 0) { if (endItemPos.y > conner_Local[3].y || _listItem.Count == 0) { //减少新的item的height //Debug.Log("_verLayoutGroup.padding.bottom: "+_verLayoutGroup.padding.bottom); itemTypeEnd++; //Debug.Log ("add"+itemTypeEnd); ChatContentIndexCallback temp = CreateItem(itemTypeEnd); float newItemHeight = itemHeightDic.ContainsKey(itemTypeEnd) ? itemHeightDic[itemTypeEnd] : 0; _verLayoutGroup.padding.bottom = _verLayoutGroup.padding.bottom - (int)newItemHeight > 0 ? _verLayoutGroup.padding.bottom - (int)newItemHeight : 0;//104;//Mathf.FloorToInt (_listItem [_listItem.Count - 1].GetComponent<RectTransform> ().sizeDelta.y); temp.transform.SetAsLastSibling(); //_listItem.Add (temp); _listItem.Insert(0, temp); //totalCount--; } } }
private void MoveTopToBottom() { if (itemTypeStart > 0) { Vector3[] conner_Local = new Vector3[4]; for (int i = 0; i < 4; i++) { conner_Local[i] = itemParent.InverseTransformPoint(conners[i]); //Debug.Log("conner_Local[i]: " + conner_Local[i]); } //float itemY = _listItem.Count > 1 ? _listItem [1].rect.sizeDelta.y/2 : 0; Vector3 lastItemPos = _listItem.Count > 1 ? _listItem [1].transform.localPosition : Vector3.zero; //移除下边不在范围的 //Debug.Log(lastItemPos - _scrollRect.transform.position); if (lastItemPos.y < conner_Local[3].y) { if (_listItem.Count > 0) { //_scrollRect.content.anchoredPosition = new Vector2(_scrollRect.content.anchoredPosition.x,-600); _verLayoutGroup.padding.bottom = _verLayoutGroup.padding.bottom + Mathf.FloorToInt(_listItem [0].GetComponent <RectTransform> ().sizeDelta.y); //移除要保存被移除的item的height //Debug.Log("_listItem [0]: "+_listItem [0].name); ChatContentIndexCallback lastItem = _listItem [0]; lastItem.gameObject.SetActive(false); _listItem.Remove(lastItem); if (!itemHeightDic.ContainsKey(itemTypeEnd)) { itemHeightDic.Add(itemTypeEnd, lastItem.GetComponent <RectTransform>().sizeDelta.y); } else { itemHeightDic[itemTypeEnd] = lastItem.GetComponent <RectTransform>().sizeDelta.y; } //_scrollRect.content.anchoredPosition.y+_listItem [0].GetComponent<RectTransform> ().sizeDelta.y); _pool.Add(lastItem); itemTypeEnd--; } } //Debug.Log("TopToBottom");0 //增加新的 Vector3 StartItemPos = _listItem.Count > 0 ? _listItem [_listItem.Count - 1].transform.localPosition : Vector3.zero; // float itemStartY2 = Vector3.Distance (_scrollRect.transform.position, StartItemPos); if (_listItem.Count > 0) { //Debug.Log ("lastItemPos: " + StartItemPos + "_listItem [_listItem.Count - 1].transform.localPosition:" + _listItem [_listItem.Count - 1].transform.localPosition); //Debug.Log(_listItem [_listItem.Count - 1].name); } if (StartItemPos.y < conner_Local[0].y || _listItem.Count == 0) { ChatContentIndexCallback temp = CreateItem(itemTypeStart); temp.transform.SetAsFirstSibling(); itemTypeStart--; _listItem.Add(temp); //totalCount--; } else { //_chatStateType = ChatStateType.empty; } } }