using System.Collections; using System.Collections.Generic; using UnityEngine; using System; namespace BuildSystem { public class CarController : MonoSingleton { [Header("车辆生成位置")] public List birth = new List(); [Header("车辆预制体")] public Car[] carPrefabs; [Header("生成最大车辆数")] public int carNumber; [NonSerialized] public int number = 0; [Header("车辆创建时间")] public Vector2 createTime; [Header("40为标准速度")] public Vector2 speed; void Start() { birth.Add(new Vector2Int(0 , 2)); birth.Add(new Vector2Int(10, 0)); //birth.Add(new Vector2Int(3, 7)); birth.Add(new Vector2Int(13, 5)); //StartCreateCar(); } /// /// 开启创建车辆协程 /// public void StartCreateCar() { StartCoroutine(CreateCar()); } /// /// 创建车辆 /// /// IEnumerator CreateCar() { while (true) { yield return new WaitUntil(() => { return number < carNumber; }); yield return new WaitForSeconds(UnityEngine.Random.Range(createTime.x, createTime.y)); number++; GameObject car = Instantiate(carPrefabs[UnityEngine.Random.Range(0, carPrefabs.Length)].gameObject,transform); car.GetComponent().OpenMove(birth[UnityEngine.Random.Range(0, birth.Count)], UnityEngine.Random.Range(speed.x,speed.y)); } } } }