这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

74 行
1.7 KiB

using UnityEngine;
namespace UOP1.Pool
{
/// <summary>
/// Implements a Pool for Component types.
/// </summary>
/// <typeparam name="T">Specifies the component to pool.</typeparam>
public abstract class ComponentPoolSO<T> : PoolSO<T> where T : Component
{
private Transform _poolRoot;
private Transform PoolRoot
{
get
{
if (_poolRoot == null)
{
_poolRoot = new GameObject(name).transform;
_poolRoot.SetParent(_parent);
}
return _poolRoot;
}
}
private Transform _parent;
/// <summary>
/// Parents the pool root transform to <paramref name="t"/>.
/// </summary>
/// <param name="t">The Transform to which this pool should become a child.</param>
/// <remarks>NOTE: Setting the parent to an object marked DontDestroyOnLoad will effectively make this pool DontDestroyOnLoad.<br/>
/// This can only be circumvented by manually destroying the object or its parent or by setting the parent to an object not marked DontDestroyOnLoad.</remarks>
public void SetParent(Transform t)
{
_parent = t;
PoolRoot.SetParent(_parent);
}
public override T Request()
{
T member = base.Request();
member.gameObject.SetActive(true);
return member;
}
public override void Return(T member)
{
member.transform.SetParent(PoolRoot.transform);
member.gameObject.SetActive(false);
base.Return(member);
}
protected override T Create()
{
T newMember = base.Create();
newMember.transform.SetParent(PoolRoot.transform);
newMember.gameObject.SetActive(false);
return newMember;
}
public override void OnDisable()
{
base.OnDisable();
if (_poolRoot != null)
{
#if UNITY_EDITOR
DestroyImmediate(_poolRoot.gameObject);
#else
Destroy(_poolRoot.gameObject);
#endif
}
}
}
}