你不必知道它内部怎么排版,只需要知道你需要多少个实例,实例需要的信息
例如我们需要点击查看问题和忽略问题,并且提示数字会变化
1.储存信息 定义一个personinfo
public class PersonInfo
{
public string m_name;
public string m_info;
public string m_time;
public string Question;
public string URl;
public Color m_GetColor;
///
/// 是否被查看过
///
public bool IsLookOver;
///
/// 搜索物体名字
///
public Transform SearchGame;
///
/// 被选中点击
///
public bool OnClick;
}
2.创建一个储存personinfo实例的脚本
public class DataResouce
{
///
///实时数据
///
public static List RealTimeInfo= new List();
///
///搜索筛选
///
public static List SearchLitem = new List();
}
3.启动排序实例化物体,这个脚本挂在LoopListView2物体上
using SuperScrollView;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace JWGS
{
public class RealTimeInfoVertical : VerticalBase
{
public static RealTimeInfoVertical Instance;
private static TextMeshProUGUI Questioncount;
private string url;
private void Awake()
{
Instance = this;
}
// Use this for initialization
void Start()
{
Questioncount = ConstantUtils.FindObject("Right-Top/Login/Reminder/count/TextMeshPro");
mLoopListView = GetComponent();
StartCoroutine(PersonName(url));
GetMy.AddListener(() => {
int p = 0;
for(int i=0;i< DataResouce.RealTimeInfo.Count;i++)
{
if(!DataResouce.RealTimeInfo[i].IsLookOver)
{
p++;
}
}
Questioncount.text = p.ToString(); });
}
// Update is called once per frame
void Update()
{
}
bool first;
public static MyEvent GetMy = new MyEvent();
//private static int Count=0;
//public static int P { get { return Count; } set { Count = value;
// if (GetMy != null)
// GetMy.Invoke();
// } }
private IEnumerator PersonName(string url)
{
yield return null;
int d = UnityEngine.Random.Range(6,10);
for (int i = 0; i < d; i++)
{
PersonInfo info = new PersonInfo();
info.m_name = "吴宗亮"+i;
info.URl = "wwwww";
info.Question = "发起新的质量问题";
info.m_info = "质量";
info.m_GetColor = new Color(0.8f, 0.3f, 0.3f);
DataResouce.RealTimeInfo.Insert(0, info);
GetMy.Invoke();
}
if (!first)
{
mLoopListView.InitListView(DataResouce.RealTimeInfo.Count, OnGetItemByIndex);//实例化prefab
first = true;
}
else
{
addRefresh(d);
}
OnJumpBtnClicked(DataResouce.RealTimeInfo.Count);//调到顶置位置
}
LoopListViewItem2 OnGetItemByIndex(LoopListView2 listView, int index)
{
if (index < 0 || index >= DataResouce.RealTimeInfo.Count)
{
return null;
}
PersonInfo itemData = DataResouce.RealTimeInfo[index];
if (itemData == null)
{
return null;
}
LoopListViewItem2 item = listView.NewListViewItem("ItemPrefab1");
RealTimeInfoItem itemScript = item.GetComponent();
if (item.IsInitHandlerCalled == false)
{
item.IsInitHandlerCalled = true;
itemScript.Init();
itemScript.myEvent.AddListener(OnDeleteBtnClicked);
itemScript.LookOver.AddListener(OnLookBtnClicked);
}
itemScript.SetItemData(itemData, index);
return item;
}
private void OnLookBtnClicked(int arg0)
{
DataResouce.RealTimeInfo[arg0].IsLookOver= true;
GetMy.Invoke();
}
void OnJumpBtnClicked(int i)
{
if (i < 0)
return;
mLoopListView.MovePanelToItemIndex(i, 0);
}
void OnAddItemBtnClicked(int i)
{
if (mLoopListView.ItemTotalCount < 0)
{
return;
}
i = mLoopListView.ItemTotalCount + i;
if (i < 0 || i > DataResouce.RealTimeInfo.Count)
{
return;
}
mLoopListView.SetListItemCount(i, false);
}
void OnSetItemCountBtnClicked(int i)
{
if (i < 0 || i > DataResouce.RealTimeInfo.Count)
{
return;
}
mLoopListView.SetListItemCount(i, false);
}
public void addRefresh(int i)
{
OnAddItemBtnClicked(i);
// OnJumpBtnClicked(ScrollRectpanel.Get.TotalItemCount);
}
void OnDeleteBtnClicked(int i)
{
DataResouce.RealTimeInfo.RemoveAt(i);
mLoopListView.SetListItemCount(DataResouce.RealTimeInfo.Count, false);
mLoopListView.RefreshAllShownItem();
OnJumpBtnClicked(DataResouce.RealTimeInfo.Count);
GetMy.Invoke();
}
}
}
4.需要实例化的prefab上挂载的脚本
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace JWGS
{
public enum Select
{
Realtime=0,
Relevantfile
}
public class RealTimeInfoItem : ListItemBase
{
public TextMeshProUGUI m_question;
private string url;
///
///查看
///
private UGUIEventListenerContainDrag m_examine;
///
///忽略
///
private UGUIEventListenerContainDrag m_neglect;
public MyEvent myEvent=new MyEvent();
public MyEvent LookOver= new MyEvent();
private string info;
private int m_itemIndex;
// Use this for initialization
void Start()
{
}
public override void Init()
{
m_examine = ConstantUtils.FindObject("查看", transform);
m_examine.onClick += delegate {
LookOver.Invoke(m_itemIndex);
};
m_neglect = ConstantUtils.FindObject("忽略", transform);
m_neglect.onClick += delegate { Debug.Log(url + "忽略");
if (myEvent != null)
myEvent.Invoke(m_itemIndex);
};
}
// Update is called once per frame
public void SetItemData(PersonInfo info, int itemIndex)
{
m_itemIndex = itemIndex;
Name.text = info.m_name;
m_question.text = info.Question;
url = info.URl;
this.info = info.m_info;
}
void Update()
{
}
}
}
附上事件处理
public class MyEvent : UnityEvent { };
public class MyEvent
public class MyEvent
public class MyEvent
public class MyEvent
最后说一下UGUIEventListenerContainDrag是继承处理ui事件的接口
using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using TMPro;
using DG.Tweening;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Security.Cryptography;
namespace JWGS
{
public static class ConstantUtils
{
///
/// 返回组件
///
///
///
///
public static T AddCompnent(GameObject game) where T : Component
{
T ts = game.GetComponent();
if (!ts)
ts = game.AddComponent();
return ts;
}
///
/// 协程委托
///
///
///
///
public static IEnumerator ActionTor(Action action, float time)
{
yield return new WaitForSeconds(time);
action();
}
///
/// 找到物体添加组件并返回
///
///
///
///
///
public static T FindObject(string path, Transform parent = null) where T : Component
{
if (parent == null)
{
GameObject game = GameObject.Find(path);
if (game == null)
{
return null;
}
return AddCompnent(game);
}
else
{
Transform tr = parent.Find(path);
if (tr == null)
{
return null;
}
GameObject game = parent.Find(path).gameObject;
return AddCompnent(game);
}
}
}
}
通过以上基础我们可以设置更复杂的滑块,插件很实用