146 lines
4.4 KiB
C#
146 lines
4.4 KiB
C#
using Assets.Scripts;
|
|
using DG.Tweening;
|
|
using System.Collections;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
public class PoolTool
|
|
{
|
|
|
|
|
|
// 从池中获取对象
|
|
public static Transform GetFromPool(ConcurrentQueue<Transform> pool, List<Transform> activeBulletPool, List<Transform> activeImpactPool)
|
|
{
|
|
//lock (pool) // 线程安全锁
|
|
//{
|
|
//while (pool.Count > 0)
|
|
//{
|
|
if (pool.TryDequeue(out Transform obj))
|
|
{
|
|
if (obj != null)
|
|
{
|
|
//pool.Remove(obj);
|
|
ResetTransform(obj); // 获取时重置状态
|
|
if (obj.GetComponent<Bullet>() != null)
|
|
{
|
|
activeBulletPool.Add(obj);
|
|
}
|
|
else if (obj.GetComponent<RecycleImpact>() != null)
|
|
{
|
|
activeImpactPool.Add(obj);
|
|
}
|
|
//var particles = obj.GetComponentsInChildren<ParticleSystem>();
|
|
//foreach (var particle in particles)
|
|
//{
|
|
// if (particle != null)
|
|
// {
|
|
// particle.time = 0;
|
|
// particle.Play();
|
|
// }
|
|
//}
|
|
return obj;
|
|
}
|
|
|
|
}//.Where(o => !o.gameObject.activeSelf).FirstOrDefault();
|
|
//}
|
|
//}
|
|
return null;
|
|
}
|
|
|
|
// 回收对象到池
|
|
public static void RecycleToPool(Transform obj, ConcurrentQueue<Transform> pool)
|
|
{
|
|
if (obj == null) return;
|
|
// 避免重复入队
|
|
if (!pool.Contains(obj))
|
|
{
|
|
// 回收时彻底重置
|
|
ResetTransform(obj);
|
|
pool.Enqueue(obj);
|
|
}
|
|
}
|
|
|
|
|
|
// 重置Transform状态
|
|
public static void ResetTransform(Transform trans)
|
|
{
|
|
if (trans == null) return;
|
|
|
|
trans.DOKill(false); // 强制杀死所有Tween
|
|
trans.SetParent(null); // 解除父对象
|
|
//trans.localPosition = Vector3.zero;
|
|
//trans.localRotation = Quaternion.identity;
|
|
//trans.localScale = Vector3.one;
|
|
|
|
trans.gameObject.SetActive(false);
|
|
}
|
|
|
|
// 命中目标后的逻辑(可自定义爆炸、伤害等)
|
|
public static void RecycleTarget(Transform bulletObj, Transform muzzleObj,
|
|
ConcurrentQueue<Transform> bulletPool, List<Transform> activeBulletPool, ConcurrentQueue<Transform> muzzlePool, List<Transform> activeImpactPool)
|
|
{
|
|
if (bulletObj != null)
|
|
{
|
|
activeBulletPool.Remove(bulletObj);
|
|
var bulletComp = bulletObj.GetComponent<Bullet>();
|
|
// 重置子弹组件状态
|
|
if (bulletComp != null)
|
|
{
|
|
bulletComp.ResetBullet();
|
|
}
|
|
if (bulletComp.IsNeedDestroyInRecycle)
|
|
{
|
|
//Debug.Log("Destroy Bullet Object 1");//Debug
|
|
Object.Destroy(bulletObj.gameObject);
|
|
}
|
|
else
|
|
{
|
|
RecycleBulletToPool(bulletObj, bulletPool, activeBulletPool);
|
|
}
|
|
}
|
|
// 回收对象
|
|
RecycleMuzzleToPool(muzzleObj, muzzlePool, activeImpactPool);
|
|
}
|
|
|
|
public static void RecycleBulletToPool(Transform bulletObj, ConcurrentQueue<Transform> bulletPool, List<Transform> activeBulletPool)
|
|
{
|
|
if (bulletObj != null)
|
|
{
|
|
activeBulletPool.Remove(bulletObj);
|
|
var bulletComp = bulletObj.GetComponent<Bullet>();
|
|
// 重置子弹组件状态
|
|
if (bulletComp != null)
|
|
{
|
|
bulletComp.ResetBullet();
|
|
//有切弹,这里必须做处理
|
|
if (bulletComp.IsNeedDestroyInRecycle)
|
|
{
|
|
//Debug.Log("Destroy Bullet Object 2");//Debug
|
|
Object.Destroy(bulletObj.gameObject);
|
|
}
|
|
else
|
|
{
|
|
// 回收对象
|
|
RecycleToPool(bulletObj, bulletPool);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void RecycleMuzzleToPool(Transform muzzleObj, ConcurrentQueue<Transform> muzzlePool, List<Transform> activeImpactPool)
|
|
{
|
|
activeImpactPool.Remove(muzzleObj);
|
|
// 回收对象
|
|
RecycleToPool(muzzleObj, muzzlePool);
|
|
}
|
|
|
|
public static void RecycleImpactToPool(Transform impactObj, ConcurrentQueue<Transform> impactPool)
|
|
{
|
|
// 回收对象
|
|
RecycleToPool(impactObj, impactPool);
|
|
}
|
|
}
|