using System; using System.Collections.Generic; using UnityEngine;

[Serializable] public class SerializedObjectPool where T : MonoBehaviour { public delegate void ObjPoolEvent(T obj);

public T Prefab;

public List<T> ActiveObjs = new List<T>();

public List<T> Pool = new List<T>();

[HideInInspector]
public ObjPoolEvent OnObjActivated;

[HideInInspector]
public DelegateUtl.NoArgsEvent OnObjDisabled;

public T this[int i]
{
	get
	{
		return ActiveObjs[i];
	}
	set
	{
		ActiveObjs[i] = value;
	}
}

public int Count => ActiveObjs.Count;

public SerializedObjectPool(T prefab)
{
	Prefab = prefab;
	ActiveObjs = new List<T>();
	Pool = new List<T>();
}

public T Get(Transform parent = null)
{
	if (Pool.Count > 0)
	{
		T val = Pool[Pool.Count - 1];
		Pool.RemoveAt(Pool.Count - 1);
		if ((UnityEngine.Object)val == (UnityEngine.Object)null)
		{
			Debug.LogError('Pool ' + this?.ToString() + ' contains null obj.. retrying');
			return Get(parent);
		}
		ActiveObjs.Add(val);
		val.gameObject.SetActive(value: true);
		val.transform.SetParent(parent);
		val.transform.localScale = Vector3.one;
		if (OnObjActivated != null)
		{
			OnObjActivated(val);
		}
		return val;
	}
	T val2 = UnityEngine.Object.Instantiate(Prefab, parent);
	ActiveObjs.Add(val2);
	 if (OnObjActivated != null)
	{
		OnObjActivated(val2);
	}
	return val2;
}

public void InitSize(int num, Transform parent)
{
	DisableAll();
	for (int i = 0; i < num; i++)
	{
		Get(parent);
	}
}

public void DisableAt(int index)
{
	if (index >= 0 && index < ActiveObjs.Count)
	{
		ActiveObjs[index].gameObject.SetActive(value: false);
		Pool.Add(ActiveObjs[index]);
		ActiveObjs.RemoveAt(index);
		if (OnObjDisabled != null)
		{
			OnObjDisabled();
		}
	}
}

public void DisableObj(T activeObj)
{
	int num = ActiveObjs.IndexOf(activeObj);
	if (num >= 0)
	{
		DisableAt(num);
	}
}

public void DisableAll()
{
	if (ActiveObjs != null)
	{
		for (int i = 0; i < ActiveObjs.Count; i++)
		{
			ActiveObjs[i].gameObject.SetActive(value: false);
			Pool.Add(ActiveObjs[i]);
		}
		ActiveObjs.Clear();
		if (OnObjDisabled != null)
		{
			OnObjDisabled();
		}
	}
}

public void DisableAllAndSetParent(Transform tgtParent)
{
	for (int num = ActiveObjs.Count - 1; num >= 0; num--)
	{
		ActiveObjs[num].transform.SetParent(tgtParent);
		DisableAt(num);
	}
}

public void DisableChildren(Transform xfm)
{
	for (int i = 0; i < xfm.childCount; i++)
	{
		Transform child = xfm.GetChild(i);
		if (child.gameObject.activeSelf)
		{
			T component = child.GetComponent<T>();
			if ((UnityEngine.Object)component != (UnityEngine.Object)null)
			{
				DisableObj(component);
			}
		}
	}
}

public void DetachFromPool(T obj)
{
	ActiveObjs.Remove(obj);
	Pool.Remove(obj);
}

}

// 这是一个Unity MonoBehaviour 游戏里的代码,这个是一个类用于管理对象池 // 它怎么实例化的,我在整个项目里没有找到实例化这个类的代码,只有类型定义,但是运行后它们都实例化了 // 游戏代码里没有实例化,但是最终结果是生成了对象内容:根据提供的代码,我们可以看到SerializedObjectPool<T>类是一个泛型类,用于管理对象池。在代码中没有看到明确的实例化,但是在Unity中,MonoBehaviour脚本的实例化通常是通过将其附加到游戏对象上来完成的。

// 在这种情况下,我们可以猜测,在Unity中的某个地方,代码可能会将SerializedObjectPool<T>附加到一个游戏对象上,使其成为一个组件。然后,通过在Unity编辑器中将预制体(Prefab)分配给SerializedObjectPool<T>Prefab变量,该对象池将被实例化和使用。

// 因此,尽管在提供的代码中没有直接的实例化过程,但我们可以合理地推测,在Unity中的某个地方,这个类被实例化并用于管理对象池。

Unity 对象池:SerializedObjectPool<T> 类详解

原文地址: https://www.cveoy.top/t/topic/o72Q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录