129 lines
4.5 KiB
C#
129 lines
4.5 KiB
C#
using Assets.Scenes.Ride.Scripts.Model;
|
|
using Assets.Scripts;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Assets.Scenes.Ride.Scripts
|
|
{
|
|
public class NearByFactory : MonoBehaviour
|
|
{
|
|
PlayerController playerController;
|
|
private GameObject nearByItem;
|
|
private GameObject nearByMajorItem;
|
|
private GameObject scroll;
|
|
GameObject body;
|
|
|
|
private int bufferSize = 0;
|
|
private int pageIndex = 1;
|
|
private int pageSize =7;
|
|
|
|
private int preNum = 0;//前面的数量
|
|
private int offset = 8;//显示区域的数量
|
|
|
|
|
|
private void Start()
|
|
{
|
|
playerController = FindObjectOfType<PlayerController>();
|
|
nearByItem = Resources.Load<GameObject>("UI/Prefab/Ride/NearbyItem");
|
|
nearByMajorItem = Resources.Load<GameObject>("UI/Prefab/Ride/NearbyMajorItem");
|
|
scroll = transform.parent.parent.gameObject;
|
|
if (scroll != null)
|
|
{
|
|
UIManager.AddEvent(scroll, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
|
|
}
|
|
}
|
|
float t =1f;
|
|
private void Update()
|
|
{
|
|
t -= Time.deltaTime;
|
|
while (t < 0)
|
|
{
|
|
Utils.DestroyChildren(transform);
|
|
var nearList = MapUDPService.GetNearRiderData(pageIndex, pageSize, new double[] { playerController.Currentlatlong.x, playerController.Currentlatlong.y });
|
|
|
|
for (int i = 0; i < nearList.Count(); i++)
|
|
{
|
|
Create(nearList[i]);
|
|
}
|
|
t = 1;
|
|
}
|
|
}
|
|
private void OnEndDrag(BaseEventData arg0)
|
|
{
|
|
var scrollrect = scroll.GetComponent<ScrollRect>();
|
|
if (scrollrect.verticalNormalizedPosition <= 0)
|
|
{
|
|
Debug.Log(scrollrect.verticalNormalizedPosition);
|
|
pageIndex++;
|
|
//RefreshList();
|
|
|
|
}
|
|
if (scrollrect.verticalNormalizedPosition >= 1)
|
|
{
|
|
if(pageIndex > 0)
|
|
pageIndex--;
|
|
//RefreshList();
|
|
}
|
|
}
|
|
|
|
private void RefreshList()
|
|
{
|
|
var nearList = MapUDPService.GetNearRiderData(pageIndex, pageSize, new double[] { playerController.Currentlatlong.x, playerController.Currentlatlong.y });
|
|
|
|
for (int i = 0; i < nearList.Count(); i++)
|
|
{
|
|
if (bufferSize < pageSize)
|
|
{
|
|
Create(nearList[i]);
|
|
}
|
|
else
|
|
{
|
|
UpdateItem(nearList[i],i);
|
|
}
|
|
}
|
|
}
|
|
private void Create(NearRiderModel item)
|
|
{
|
|
//创建
|
|
GameObject prefabItem = item.IsSelf ? nearByMajorItem : nearByItem;
|
|
GameObject newObj = (GameObject)Instantiate(prefabItem, transform);
|
|
var nearByItemscript = newObj.GetComponent<NearByItemScript>();
|
|
nearByItemscript.setUserId(item.Id);
|
|
nearByItemscript.setName(item.Name);
|
|
nearByItemscript.setRatio(item.KGWeight+"W/KG");
|
|
nearByItemscript.setSpeed(item.Speed.ToString() + "KM/H");
|
|
nearByItemscript.setHead(item.Headimage);
|
|
nearByItemscript.setCountry(item.CountryImg);
|
|
nearByItemscript.setDistance(item.Near.ToString() + "KM");
|
|
bufferSize++;
|
|
}
|
|
|
|
private void UpdateItem(NearRiderModel item,int index)
|
|
{
|
|
for (int i = 0; i < transform.childCount; i++)
|
|
{
|
|
var child = transform.GetChild(i);
|
|
if (child != null)
|
|
{
|
|
NearByItemScript nearByItemscript = child.GetComponent<NearByItemScript>();
|
|
if (nearByItemscript.UserId == item.Id)
|
|
{
|
|
nearByItemscript.setName(item.Name);
|
|
nearByItemscript.setRatio(item.KGWeight + "W/KG");
|
|
nearByItemscript.setSpeed(item.Speed.ToString() + "KM/H");
|
|
//nearByItemscript.setHead(item.Headimage);
|
|
//nearByItemscript.setCountry(item.CountryImg);
|
|
nearByItemscript.setDistance(item.Near.ToString() + "KM");
|
|
if (index != i)//重排序
|
|
{
|
|
child.SetSiblingIndex(index);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|