109 lines
4.5 KiB
C#
109 lines
4.5 KiB
C#
using Assets.Scenes.Ride.Scripts.Model;
|
||
using System;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using UnityEngine.UI;
|
||
|
||
namespace Assets.Scenes.Ride.Scripts
|
||
{
|
||
public class NearByFactory : BaseListFactory
|
||
{
|
||
private Text Title { get; set; }
|
||
private Text OnlineUserNum { get; set; }
|
||
protected override void Awake()
|
||
{
|
||
scroll = transform.Find("NearByScrollView").gameObject;
|
||
if (scroll != null)
|
||
{
|
||
UIManager.AddEvent(scroll, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
|
||
}
|
||
parent = scroll.transform.Find("Viewport/Content");
|
||
cyclingController = FindObjectOfType<CyclingController>();
|
||
playerController = FindObjectOfType<PlayerController>();
|
||
#if UNITY_IOS || UNITY_ANDROID
|
||
nearByItem = Resources.Load<GameObject>("UI/Prefab/Ride/Mobile/NearbyItem");
|
||
nearByMajorItem = Resources.Load<GameObject>("UI/Prefab/Ride/Mobile/NearbyMajorItem");
|
||
#else
|
||
nearByItem = Resources.Load<GameObject>("UI/Prefab/Ride/NearbyItem");
|
||
nearByMajorItem = Resources.Load<GameObject>("UI/Prefab/Ride/NearbyMajorItem");
|
||
#endif
|
||
Title = transform.Find("Title").GetComponent<Text>();
|
||
OnlineUserNum = transform.Find("NearByView/OnlineUserNum").GetComponent<Text>();
|
||
pageSize = 7;//开启无线下拉
|
||
}
|
||
protected override void CreateList()
|
||
{
|
||
OnlineUserNum.text = " "+ MapUDPService.GetNearRiderCount().ToString();
|
||
LayoutRebuilder.ForceRebuildLayoutImmediate(OnlineUserNum.rectTransform);
|
||
var nearList = MapUDPService.GetNearRiderData(pageIndex, pageSize, new double[] { playerController.Currentlatlong.x, playerController.Currentlatlong.y });
|
||
if (nearList == null)
|
||
return;
|
||
var mod = MapUDPService.GetNearRiderCount() % pageSize;
|
||
var pages = MapUDPService.GetNearRiderCount() / pageSize;
|
||
totalPages = mod > 0 ? pages + 1 : pages;
|
||
//删除
|
||
var currentNearList = FindObjectsOfType<NearByItemScript>();
|
||
foreach (var item in currentNearList)
|
||
{
|
||
var e = nearList.Where(c => c.Id == item.UserId).FirstOrDefault();
|
||
if (e == null)
|
||
{
|
||
//item.transform.gameObject.SetActive(false);
|
||
item.Dispose();
|
||
bufferSize--;
|
||
}
|
||
}
|
||
|
||
for (int i = 0; i < nearList.Count(); i++)
|
||
{
|
||
Create(nearList[i], currentNearList);
|
||
}
|
||
}
|
||
|
||
|
||
private void Create(NearRiderModel item, NearByItemScript[] rankingList)
|
||
{
|
||
//var rankingList = FindObjectsOfType<NearByItemScript>();
|
||
var nearByItemscript = rankingList.Where(c => c.UserId == item.Id).FirstOrDefault();
|
||
//如果当前有这个用户则更新没有则创建并setslibingindex
|
||
if (nearByItemscript == null)
|
||
{
|
||
//创建
|
||
if (bufferSize < pageSize)
|
||
{
|
||
//情况一:还有足够的buffer,直接创建
|
||
GameObject prefabItem = item.IsSelf ? nearByMajorItem : nearByItem;
|
||
GameObject newObj = (GameObject)Instantiate(prefabItem, parent);
|
||
nearByItemscript = newObj.GetComponent<NearByItemScript>();
|
||
bufferSize++;
|
||
}
|
||
else
|
||
{
|
||
//情况二:没有足够的buffer,找到要被淘汰的buffer()最后一名
|
||
nearByItemscript = rankingList.OrderByDescending(c => c.distance).FirstOrDefault();
|
||
}
|
||
}
|
||
|
||
nearByItemscript.setUserId(item.Id);
|
||
nearByItemscript.setName(item.Name);
|
||
nearByItemscript.setRatio(item.KGWeight + "W/KG");
|
||
nearByItemscript.setSpeed(item.Speed.ToString() + "KM/H");
|
||
if (cyclingController.currentPlayer.UserId == item.Id)
|
||
{
|
||
nearByItemscript.setHead(item.Headimage);
|
||
nearByItemscript.transform.SetAsFirstSibling();
|
||
}
|
||
else
|
||
{
|
||
nearByItemscript.transform.SetSiblingIndex((int)Math.Ceiling(item.Near));
|
||
}
|
||
#if !(UNITY_IOS || UNITY_ANDROID)
|
||
nearByItemscript.setCountry(cyclingController.GetCountryImageByName(item.Country));
|
||
#endif
|
||
nearByItemscript.setDistance(item.Near.ToString() + "KM");
|
||
|
||
}
|
||
}
|
||
}
|