unity2d Ugui--Image城市道路汽车行驶

目录

1.车辆生成与回收

2.路径点控制

3.车辆控制


1.车辆生成与回收
cs 复制代码
using System.Collections.Generic;
using UnityEngine;

public class RoadContr : MonoBehaviour
{
    public WayPoint[] wayPoints; //出生点
    public Transform pare;
    [SerializeField]private Car[] fabCar;

    public Dictionary<int, Queue<Car>> dic_car = new();
    private int carNum = 0;

    private void OnEnable()
    {
        InvokeRepeating("CarCreate", 1, 1);
    }

    private void CarCreate()
    {
        if (fabCar.Length<1) { return; }
        if (carNum < 30)
        {
            carNum++;
            Car car = Instantiate(fabCar[Random.Range(0, fabCar.Length)], pare);
            WayPoint way = wayPoints[Random.Range(0, wayPoints.Length)];
            car.rect.anchoredPosition = way.rect.anchoredPosition;
            car.Init(way,this);
        }
        else
        {
            CancelInvoke("CarCreate");
        }
    }

    public void CarRecycle(int index , Car car) {
        if (!dic_car.ContainsKey(index))
        {
            dic_car[index] = new Queue<Car>();
        }
        
        if (dic_car[index].Count > 10)
        {
            Destroy(car.gameObject);
        }
        else
        {
            car.gameObject.SetActive(false);
            dic_car[index].Enqueue(car);
        }
        
        CreateOneCar();
    }

    private void CreateOneCar()
    {
        Car car = null;
        int ran = Random.Range(0, fabCar.Length);
        if (!dic_car.ContainsKey(ran))
        {
            dic_car[ran] = new Queue<Car>();
        }
        if (dic_car[ran].Count > 0)
        {
            car = dic_car[ran].Dequeue();
        }
        else { 
            car = Instantiate(fabCar[ran], pare);
        }
        WayPoint way = wayPoints[Random.Range(0, wayPoints.Length)];
        car.rect.anchoredPosition = way.rect.anchoredPosition;
        car.gameObject.SetActive(true);
        car.Init(way, this);
    }
}
2.路径点控制
cs 复制代码
using System.Collections.Generic;
using UnityEngine;

public class WayPoint : MonoBehaviour
{
    public bool isDestroy;
    public RectTransform rect;
    [Header("0上1下2左3右")]
    public int curDirect = -1;
    [Header("下一路径点")]
    public List<WayPoint> nextWayPoints_1;


    /// <summary>
    /// 获取下一路径点
    /// </summary>
    public WayPoint GetNextWayPoint()
    {
        if (nextWayPoints_1.Count == 1)
        {
            return nextWayPoints_1[0];
        }
        else
        {
            return nextWayPoints_1[Random.value < 0.5 ? 0 : 1];
        }
    }

}
3.车辆控制
cs 复制代码
using UnityEngine;
using UnityEngine.UI;

public class Car : MonoBehaviour
{
    [SerializeField] private Image carImg;
    //0:scaleX 上:1 右:-1 、1: scaleX 下1 左-1
    [SerializeField] private Sprite[] carSprites;
    [SerializeField] private int carIndex;
    public RectTransform rect;
    [SerializeField] private WayPoint curWay;       // 当前路径点
    [SerializeField] private WayPoint nextWay;       // 下一路径点
    [SerializeField] private float speed;
    [SerializeField]private int direction = -1;
    private RoadContr road;

    public void Init(WayPoint cur_way,RoadContr _road)
    {
        curWay = cur_way;
        road = _road;
        direction = curWay.curDirect;
        nextWay = curWay.GetNextWayPoint();
        UpdateCarImage();
    }

    private void Update()
    {
        if (!gameObject.activeInHierarchy) { return; }

        if (Vector2.Distance(rect.anchoredPosition, nextWay.rect.anchoredPosition) <= 0.1f)
        {
            // 到达
            if (nextWay.isDestroy)
            {
                //回收
                road.CarRecycle(carIndex, this);
                return;
            }
            else
            {
                //获取新的下一个路径点
                Init(nextWay, road);
            }
        }
        else { 
            Vector2 newPosition = Vector2.MoveTowards(rect.anchoredPosition, nextWay.rect.anchoredPosition, speed * Time.deltaTime);
            rect.anchoredPosition = newPosition;
        }
    }

    private Vector3 dir_scale = new(-1, 1, 1);
    private void UpdateCarImage()
    {
        if (direction == 0 || direction == 1)
        {
            carImg.rectTransform.localScale = dir_scale;
        }
        else {
            carImg.rectTransform.localScale = Vector3.one;
        }

        if (direction == 0 || direction == 2)
        {
            carImg.sprite = carSprites[0];
        }
        else
        {
            carImg.sprite = carSprites[1];
        }
    }
}