87 lines
3.4 KiB
C#
87 lines
3.4 KiB
C#
// using System.Collections;
|
|
// using System.Collections.Generic;
|
|
// using UnityEngine;
|
|
|
|
// public class DynamicParallaxManager : MonoBehaviour
|
|
// {
|
|
// public ParallaxObjectConfig[] objectConfigs; // 配置数组
|
|
// private Dictionary<ParallaxObjectConfig, List<GameObject>> objectPools = new Dictionary<ParallaxObjectConfig, List<GameObject>>();
|
|
|
|
// void Start()
|
|
// {
|
|
// // 初始化对象池并激活第一个物体
|
|
// foreach (var config in objectConfigs)
|
|
// {
|
|
// var pool = new List<GameObject>();
|
|
// // 创建对象池
|
|
// for (int i = 0; i < config.poolSize; i++)
|
|
// {
|
|
// GameObject obj = Instantiate(config.prefab, transform.position, Quaternion.identity);
|
|
// obj.SetActive(false); // 初始状态为禁用
|
|
// pool.Add(obj);
|
|
// }
|
|
|
|
// // 激活第一个物体并开始移动
|
|
// GameObject firstObject = pool[0]; // 获取池中的第一个物体
|
|
// firstObject.transform.position = config.prefab.transform.position; // 使用编辑器中设置的位置
|
|
// firstObject.SetActive(true); // 激活第一个物体
|
|
// StartCoroutine(MoveObject(firstObject, config, config.maxSpeed)); // 启动移动协程
|
|
|
|
// objectPools[config] = pool;
|
|
|
|
// // 启动生成其他物体的协程
|
|
// StartCoroutine(ManageObjectGeneration(config));
|
|
// }
|
|
// }
|
|
|
|
// private IEnumerator ManageObjectGeneration(ParallaxObjectConfig config)
|
|
// {
|
|
// // 获取池中的对象
|
|
// var pool = objectPools[config];
|
|
// GameObject lastObject = pool[0]; // 最后一个激活的物体
|
|
|
|
// // 初始位置检查,第一个物体直接开始
|
|
// while (true)
|
|
// {
|
|
// // 检查最后一个激活的物体的 x 坐标
|
|
// if (lastObject.transform.position.x < -config.xGenerationThreshold)
|
|
// {
|
|
// // 找到一个未激活的物体并生成
|
|
// GameObject obj = pool.Find(o => !o.activeInHierarchy);
|
|
// if (obj != null)
|
|
// {
|
|
// obj.SetActive(true); // 激活对象
|
|
// float xStart = config.xSpawnRange;
|
|
// float yStart = config.prefab.transform.position.y;
|
|
// obj.transform.position = new Vector3(xStart, yStart, obj.transform.position.z);
|
|
|
|
// // 设置物体的移动速度
|
|
// float speed = Random.Range(config.minSpeed, config.maxSpeed);
|
|
// StartCoroutine(MoveObject(obj, config, speed)); // 启动移动协程
|
|
|
|
// lastObject = obj; // 更新最后一个激活物体为新生成的物体
|
|
// }
|
|
// }
|
|
|
|
// yield return null; // 等待下一帧
|
|
// }
|
|
// }
|
|
|
|
// private IEnumerator MoveObject(GameObject obj, ParallaxObjectConfig config, float speed)
|
|
// {
|
|
// while (obj.activeInHierarchy)
|
|
// {
|
|
// // 物体沿X轴向左移动
|
|
// obj.transform.Translate(Vector3.left * speed * Time.deltaTime);
|
|
|
|
// // 如果对象超出屏幕范围则禁用
|
|
// if (obj.transform.position.x < -config.xSpawnRange)
|
|
// {
|
|
// obj.SetActive(false); // 禁用对象
|
|
// }
|
|
|
|
// yield return null;
|
|
// }
|
|
// }
|
|
// }
|