using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Linq; public class PowerMessage : MonoBehaviour { public GameObject CarMsgPrefab; public Transform CarMsgParent; public Text NowDis; public Text NowTime; private List> MyCarList = new List>(); private float showTime = 5f; void Start() { } //用来显示车辆信息的方法 public void ShowMessage(List> CarList, string Distance, string time) { showTime = 5f; if (MyCarList != CarList) { MyCarList = CarList; CarMsgShow(CarList); } NowDis.text = Distance; NowTime.text = time; if (!gameObject.activeSelf) gameObject.SetActive(true); } public void CarMsgShow(List> ShowCarList) { foreach (Transform t in CarMsgParent) { Destroy(t.gameObject); } if (ShowCarList.Count > 0) { //var GroupResult = from FireCarEngine in ShowCarList group FireCarEngine by FireCarEngine.TypeName; //foreach (IGrouping item in GroupResult) //{ // GameObject c = Instantiate(CarMsgPrefab, CarMsgParent); // c.transform.Find("CarName").GetComponent().text = item.Key; // c.transform.Find("CarNum").GetComponent().text = item.Count().ToString(); //} foreach (KeyValuePair item in ShowCarList) { GameObject c = Instantiate(CarMsgPrefab, CarMsgParent); c.transform.Find("CarName").GetComponent().text = item.Key.TypeName; c.transform.Find("CarNum").GetComponent().text = item.Value.ToString(); } } } void Update() { if (gameObject.activeSelf) { showTime -= Time.deltaTime; if (showTime <= 0) { gameObject.SetActive(false); showTime = 5f; } } } }