using Assets.Scenes.Ride.Scripts.Model; using Assets.Scenes.Ride.Scripts.Model.CyclingModels; using Assets.Scripts; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; namespace Assets.Scenes.Ride.Scripts { public class WatcherFactory : MonoBehaviour { private Transform content { get; set; } private CyclingController cyclingController { get;set;} private GameObject watcher { get; set; } private GameObject head { get; set; } private Text watchNum { get; set; } private void Awake() { cyclingController = FindObjectOfType(); content = transform.Find("List/Viewport/Content"); head = transform.Find("Head").gameObject; watchNum = transform.Find("Head/OnlineUserNum").GetComponent(); watcher = Resources.Load("UI/Prefab/Match/Watcher"); } private float t = 1f; private void Update() { t -= Time.deltaTime; while (t<=0) { CreateList(); t = 1; } } private void CreateList() { var watchList = cyclingController.GetWatcherList(); var count = watchList.Count(); head.SetActive(count >= 5); watchNum.text = count.ToString();//绑定观看人数 var list = watchList.Take(5); Utils.DestroyChildren(content); foreach (var item in list) { var head = Instantiate(watcher, content); var headiamge = head.GetComponent(); Utils.DisplayImage(headiamge, item.HeadImage, true); } } } }