根据项目要求自己写了个滚动列表,以方便自己以后随时使用
/*************************************************************************
* Copyright © 2019 LiuKe. All rights reserved.
*------------------------------------------------------------------------
* File : ScrollerItems.cs
* Description : Null.
*------------------------------------------------------------------------
* Author : LiuKe
* Version : 0.1.0
* Date : 2019/12/4
* Description : Initial development version.
*************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System;
namespace LiuKe.Script
{
//[AddComponentMenu("")]
//[RequireComponent(typeof())]
public class ScrollerItems : MonoBehaviour
{
#region Field and Property
public GameObject clone;
ScrollRect scrollRect;
GridLayoutGroup gridLayoutGroup;
[Header("Y方向间隔")] public float YSpace = 0;
[Header("X方向间隔")] public float XSpace = 0;
public GridLayoutGroup.Constraint constraint;
public int constraintCount = 1;
private List<Dictionary<string, object>> m_ItemsDatas;
private ContentSizeFitter contentSizeFitter;
#endregion
#region Private Method
// Use this for initialization.
void Start()
{
}
public void Init(GameObject cloneobj = null)
{
if (GetComponent<ScrollRect>() == null)
{
Debug.LogError("此物体上没有<--ScrollRect-->组件");
return;
}
if (clone == null)
{
if (cloneobj == null)
{
Debug.LogError("预制体为空");
}
else
{
clone = cloneobj;
}
}
scrollRect = GetComponent<ScrollRect>();
if (scrollRect.content.GetComponent<GridLayoutGroup>() == null)
{
scrollRect.content.gameObject.AddComponent<GridLayoutGroup>();
}
gridLayoutGroup = scrollRect.content.GetComponent<GridLayoutGroup>();
gridLayoutGroup.cellSize = clone.GetComponent<RectTransform>().sizeDelta;
gridLayoutGroup.spacing = new Vector2(XSpace, YSpace);
gridLayoutGroup.constraint = constraint;
gridLayoutGroup.constraintCount = constraintCount;
if (scrollRect.content.GetComponent<ContentSizeFitter>() == null)
{
scrollRect.content.gameObject.AddComponent<ContentSizeFitter>();
}
contentSizeFitter = scrollRect.content.GetComponent<ContentSizeFitter>();
contentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
contentSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
m_ItemsDatas = new List<Dictionary<string, object>>();
scrollRect.onValueChanged.AddListener(onValueChanged);
Invoke("RepedEvent",0.5f);
}
private void onValueChanged(Vector2 arg0)
{
// print(scrollRect.content.localPosition);
}
#endregion
void RepedEvent()
{
Bounds viewBounds = GetBounds(scrollRect.GetComponent<RectTransform>());
print(scrollRect.viewport.sizeDelta);
print(scrollRect.viewport.position);
foreach (RectTransform VARIABLE in scrollRect.content)
{
if (GetBounds(VARIABLE).Intersects(viewBounds))
{
//print(VARIABLE.GetComponent<ScroltemBase>().index);
}
}
}
#region Public Method
public void SetData(List<Dictionary<string, object>> list)
{
// Clear();
//清空子物体
for (int i = 0; i < scrollRect.content.childCount; i++)
{
Destroy(scrollRect.content.GetChild(i).gameObject);
}
scrollRect.content.DetachChildren();
if (list.Count <= 0) return;
for (int i = 0; i < list.Count; i++)
{
GameObject _clone = Instantiate(this.clone);
_clone.GetComponent<ScroltemBase>().SetContent(i, list[i]);
_clone.transform.SetParent(scrollRect.content.transform);
_clone.gameObject.SetActive(true);
_clone.GetComponent<RectTransform>().localScale = Vector3.one;
}
foreach (var VARIABLE in list)
{
if (!m_ItemsDatas.Contains(VARIABLE))
m_ItemsDatas.Add(VARIABLE);
}
}
public void AddOneItem(Dictionary<string, object> data)
{
m_ItemsDatas.Add(data);
GameObject _clone = Instantiate(this.clone);
_clone.GetComponent<ScroltemBase>().SetContent(m_ItemsDatas.Count - 1, data);
_clone.transform.SetParent(scrollRect.content.transform);
_clone.gameObject.SetActive(true);
_clone.GetComponent<RectTransform>().localScale = Vector3.one;
//int allcell = scrollRect.content.childCount;
//int row = 0;
//row = allcell / constraintCount;
//if (allcell % constraintCount >= 0)
//{
// row++;
//}
//scrollRect.content.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, row * yItem);
}
public GameObject cloneimg;
public void DeleteOneItem(int index)
{
for (int i = 0; i < scrollRect.content.childCount; i++)
{
if (index == scrollRect.content.GetChild(i).GetComponent<ScroltemBase>().index)
{
m_ItemsDatas.RemoveAt(index);
}
}
SetData(m_ItemsDatas);
}
public void Clear()
{
//清空子物体
for (int i = 0; i < scrollRect.content.childCount; i++)
{
Destroy(scrollRect.content.GetChild(i).gameObject);
}
scrollRect.content.DetachChildren();
m_ItemsDatas.Clear();
}
private BoxCollider2D boxCollider2D;
Bounds GetBounds(RectTransform rect)
{
Bounds bounds=new Bounds();
bounds.center = rect.position;
bounds.size = rect.sizeDelta;
return bounds;
}
#endregion
}
}
/*************************************************************************
* Copyright © 2019 LiuKe. All rights reserved.
*------------------------------------------------------------------------
* File : Scroltem.cs
* Description : Null.
*------------------------------------------------------------------------
* Author : LiuKe
* Version : 0.1.0
* Date : 2019/12/4
* Description : Initial development version.
*************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System;
namespace LiuKe.Script
{
//[AddComponentMenu("")]
//[RequireComponent(typeof())]
public enum ScroltemEvent
{
Slected
}
public class Scroltem : ScroltemBase
{
#region Field and Property
// [Tooltip("")]
// [SerializeField]
#endregion
#region Private Method
// Use this for initialization.
void Start()
{
}
// Update is called once per frame.
void Update()
{
}
#endregion
#region Public Method
private string content;
public override void SetContent(int indexitem, Dictionary<string, object> data)
{
base.SetContent(indexitem, data);
SetText("Text", (string)data["name"]);
}
private void ClickEvent()
{
}
#endregion
}
}
/*************************************************************************
* Copyright © 2019 LiuKe. All rights reserved.
*------------------------------------------------------------------------
* File : ScroltemBase.cs
* Description : Null.
*------------------------------------------------------------------------
* Author : LiuKe
* Version : 0.1.0
* Date : 2019/12/4
* Description : Initial development version.
*************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace LiuKe.Script
{
//[AddComponentMenu("")]
//[RequireComponent(typeof())]
public class ScroltemBase : MonoBehaviour
{
#region Field and Property
public int index;
public Dictionary<string, object> itemdata;
public List<GameObject> listObjs = new List<GameObject>();
#endregion
#region Private Method
// Use this for initialization.
void Start()
{
}
// Update is called once per frame.
void Update()
{
}
#endregion
#region Public Method
public virtual void SetContent(int indexitem, Dictionary<string, object> data)
{
index = indexitem;
itemdata = data;
}
public void SetText(string objName, string content)
{
for (int i = 0; i < listObjs.Count; i++)
{
if (objName == listObjs[i].name)
{
if (listObjs[i].GetComponent<Text>())
{
listObjs[i].GetComponent<Text>().text = content;
}
else
{
Debug.LogError("没有对应组件,检查吧!!!!");
}
}
}
}
public GameObject GetGameObject(string objName)
{
for (int i = 0; i < listObjs.Count; i++)
{
if (objName == listObjs[i].name)
{
return listObjs[i];
}
}
Debug.LogError("没有对应组件,检查吧!!!!");
return null;
}
public Image GetImage(string objName)
{
for (int i = 0; i < listObjs.Count; i++)
{
if (objName == listObjs[i].name)
{
return listObjs[i].GetComponent<Image>();
}
}
Debug.LogError("没有对应组件,检查吧!!!!");
return null;
}
public Text GetText(string objName)
{
for (int i = 0; i < listObjs.Count; i++)
{
if (objName == listObjs[i].name)
{
return listObjs[i].GetComponent<Text>();
}
}
Debug.LogError("没有对应组件,检查吧!!!!");
return null;
}
#endregion
}
}
/*************************************************************************
* Copyright © #COPYRIGHTYEAR# LiuKe. All rights reserved.
*------------------------------------------------------------------------
* File : InitData.cs
* Description : Null.
*------------------------------------------------------------------------
* Author : LiuKe
* Version : 0.1.0
* Date : #CREATEDATE#
* Description : Initial development version.
*************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace LiuKe.Script
{
//[AddComponentMenu("")]
//[RequireComponent(typeof())]
public class InitData : MonoBehaviour
{
#region Field and Property
// [Tooltip("")]
// [SerializeField]
public ScrollerItems ScrollerItems;
#endregion
#region Private Method
// Use this for initialization.
void Start()
{
ScrollerItems.Init();
ScrollerItems.SetData(GetData());
}
// Update is called once per frame.
void Update()
{
}
#endregion
#region Public Method
public void AddOne()
{
Dictionary<string,object> temp=new Dictionary<string, object>();
temp["name"] = "旺财";
ScrollerItems.AddOneItem(temp);
}
public int Index = 0;
public void DeletOne()
{
ScrollerItems.DeleteOneItem(Index);
}
List<Dictionary<string, object>> GetData()
{
List<Dictionary<string, object>> data=new List<Dictionary<string, object>>();
for (int i = 0; i < 150; i++)
{
Dictionary<string,object> temp=new Dictionary<string, object>();
temp["name"] = i.ToString();
data.Add(temp);
}
return data;
}
#endregion
}
}