using System; using System.Collections; using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; // 耗材列表 public class MaterialListController : MonoBehaviour,HttpController { public Transform dataView; // 显示对象 private HttpRequestHandler httpRequestHandler; private int index = 0; // 数据下标 private List materialDatas; private List lastDatas; // 上一次的数据源 private string httpUrl=HttpRequestHandler.httpIp+"/system/materialInfo/materialListApi"; private string encryptText; // 加密文本 private bool firstLoad = true; void OnEnable() { firstLoad = true; httpRequestHandler = gameObject.AddComponent(); encryptText = AESHelper.Encrypt("/system/materialInfo/materialListApi"); StartCoroutine(UpdateDataSource()); } public IEnumerator UpdateDataSource() { while (true) { // 发起一个 POST 请求 string jsonData = "{\"encryptText\":\"" + encryptText + "\"}"; StartCoroutine(httpRequestHandler.PostRequest(httpUrl, jsonData, OnPostSuccess, OnError)); yield return new WaitForSeconds(4f); } } // POST 请求成功回调 private void OnPostSuccess(string response) { Debug.Log("POST Success 耗材: " + DateTime.Now); ReturnInfo> returnInfo = JsonUtility.FromJson>>(response); materialDatas = returnInfo.data; if (firstLoad) { firstLoad = false; UpdateDataView(); } } // 请求失败的回调 private void OnError(string error) { Debug.LogError("Request Error: " + error); } // 更新填充数据 private void UpdateDataView() { int count = materialDatas.Count; print("耗材数量:"+count); for (int i=0;i < dataView.childCount; i++) { // (index+i)%userDatas.Count() // 当轮播图轮动多少次时,列表的第一个数据就应该是数据源里的第多少个,用i递增过去,当超出数据源数量时使用%将多余的拿出去以获得正确的下标 // 名称 dataView.GetChild(i).GetChild(0).GetChild(0).GetComponent().text = materialDatas[(index+i)%count].name == "" ? "无":materialDatas[(index+i)%count].name; // 类型 dataView.GetChild(i).GetChild(0).GetChild(1).GetComponent().text = materialDatas[(index+i)%count].type == "" ? "无":materialDatas[(index+i)%count].type; // 实时库存 dataView.GetChild(i).GetChild(0).GetChild(2).GetComponent().text = materialDatas[(index+i)%count].stock.ToString(); // 预警值 dataView.GetChild(i).GetChild(0).GetChild(3).GetComponent().text = materialDatas[(index+i)%count].warningValue.ToString(); /*if (index == materialDatas.Count-1) { return; }*/ } } // 更新下标 public void UpdateIndex() { // 轮播图每次轮动下标增加,下标达到数据源数量最大值时清零 if (materialDatas == null) { return; } if (index