前言
原理是通过改变Scrollbar的Value和Size数值,刷新指定多少条数据在UGUI上
JJItemView.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class JJItemView : MonoBehaviour
{
public int lstStart = 0;
public int start = 0;
public ScrollRect scRect;
public Scrollbar rectBar;
public float mouseStep = -1f;
public JJItemRow[] JJItemRow;
public static JJItemView _instance;
void Awake()
{
_instance = this;
}
void Update()
{
if (gameObject.activeSelf)
{
updateMouseWheel();
if (lstStart != start)
{
lstStart = start;
updateRows();
}
}
}
#region ----------------SCROLLVIEW改变
public void onRectValueChanged()
{
if (scRect.verticalNormalizedPosition == 1)
{
rectBar.value += mouseStep;
}
else
{
rectBar.value -= mouseStep;
}
}
int TempStart=0;
public void onBarChange()
{
if (UIControl._instance.TempZY.TenderJJ.Count > JJItemRow.Length)
{
if(UIControl._instance.TempZY.TenderJJ.Count%2==0)
{
TempStart = (int)(rectBar.value * (UIControl._instance.TempZY.TenderJJ.Count - JJItemRow.Length));
if(TempStart%2==0)
{
start=TempStart;
}
}
else
{
TempStart = (int)(rectBar.value * (UIControl._instance.TempZY.TenderJJ.Count+1 - JJItemRow.Length));
if(TempStart%2==0)
{
start=TempStart;
}
}
}
else
{
start = 0;
}
}
public void BarSizeChange()
{
if(UIControl._instance.TempZY.TenderJJ.Count%2==0)
{
float barSz = ((float)JJItemRow.Length) / ((float)UIControl._instance.TempZY.TenderJJ.Count);
rectBar.size = barSz;
}
else
{
float barSz = ((float)JJItemRow.Length) / ((float)(UIControl._instance.TempZY.TenderJJ.Count+1));
rectBar.size = barSz;
}
}
public void updateRows()
{
for (int i = 0; i < JJItemRow.Length; ++i)
{
int dataRow = start + i;
if (dataRow < UIControl._instance.TempZY.TenderJJ.Count)
{
JJItemRow[i].gameObject.SetActive(true);
JJItemRow[i].Init(UIControl._instance.TempZY.TenderJJ[dataRow]);
}
else
{
JJItemRow[i].gameObject.SetActive(false);
JJItemRow[i].Init(null);
}
}
}
public void updateData()
{
if(UIControl._instance.TempZY.TenderJJ.Count%2==0)
{
int max = UIControl._instance.TempZY.TenderJJ.Count - JJItemRow.Length;
start = Mathf.Clamp(start, 0, Mathf.Abs(max));
}
else
{
int max = UIControl._instance.TempZY.TenderJJ.Count+1 - JJItemRow.Length;
start = Mathf.Clamp(start, 0, Mathf.Abs(max));
}
for (int i = 0; i < JJItemRow.Length; ++i)
{
int dataRow = start +i;
if (dataRow < UIControl._instance.TempZY.TenderJJ.Count)
{
JJItemRow[i].saveChangeValue(UIControl._instance.TempZY.TenderJJ[dataRow]);
}
}
}
public void updateMouseWheel()
{
float deltaWheel = Input.GetAxis("Mouse ScrollWheel");
if (deltaWheel != 0.0f)
{
rectBar.value += deltaWheel * Time.deltaTime * mouseStep;
}
}
#endregion
}
JJItemRow.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using Google.Protobuf;
using Tenders.Protobuf;
public class JJItemRow : MonoBehaviour
{
public Text JJname;
public InputField Sum;
public InputField Sprice;
public void Init(TenderZY zy)
{
if (zy != null)
{
JJname.text = zy.Leixing.ToString();
Sum.text = zy.Ziyuansum.ToString();
Sprice.text = zy.Ziyuanhuafei.ToString();
}
else
{
JJname.text = "";
Sum.text ="1";
Sprice.text = "0";
}
}
public void saveChangeValue(TenderZY zy)
{
//只改变数量,其他不变
zy.Ziyuansum = int.Parse(Sum.text);
}
//点击左键按钮
public void LeftButton(InputField SumText)
{
int sum = int.Parse(SumText.text);
if (sum > 0)
{
sum--;
SumText.text = sum.ToString();
}
JJItemView._instance.updateData();
}
//点击右键按钮
public void RightButton(InputField SumText)
{
//数量先无限制,如有需要加判断
int sum = int.Parse(SumText.text);
sum++;
SumText.text = sum.ToString();
JJItemView._instance.updateData();
}
}
网友评论