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(); #if UNITY_IOS || UNITY_ANDROID watcher = Resources.Load("UI/Prefab/Match/Mobile/Watcher"); #else watcher = Resources.Load("UI/Prefab/Match/Watcher"); #endif } private float t = 1f; private void Update() { t -= Time.deltaTime; while (t<=0) { CreateList(); t = 1; } } private const int MAX_COUNT = 7; private void CreateList() { var watchList = cyclingController.GetWatcherList(); var count = watchList.Count(); head.SetActive(count>= MAX_COUNT); watchNum.text = count.ToString();//绑定观看人数 var list = watchList.Take(MAX_COUNT); Utils.DestroyChildren(content); foreach (var item in list) { var head = Instantiate(watcher, content); var headiamge = head.GetComponent(); Utils.DisplayImage(headiamge, item.HeadImage, true); } } } }