浏览代码

Removed DontDestroyOnLoad

Removed DDOL
Added SetParent method that allows pool managers to tie the pool lifetime to their own
/main
Dave Rodriguez 4 年前
当前提交
5430f525
共有 2 个文件被更改,包括 63 次插入13 次删除
  1. 58
      UOP1_Project/Assets/Scripts/Pool/ComponentPoolSO.cs
  2. 18
      UOP1_Project/Assets/Scripts/Pool/Example/LocalPoolTester.cs

58
UOP1_Project/Assets/Scripts/Pool/ComponentPoolSO.cs


/// <typeparam name="T">Specifies the component to pool.</typeparam>
public abstract class ComponentPoolSO<T> : PoolSO<T> where T : Component
{
private GameObject _poolRootObject;
private GameObject PoolRootObject
private Transform _poolRoot;
private Transform PoolRoot
if (!Application.isPlaying)
if (_poolRoot == null)
return null;
_poolRoot = new GameObject(name).transform;
if (_poolRootObject == null)
return _poolRoot;
}
}
private Transform _parent;
private Transform Parent
{
get
{
if (_parent == null)
_poolRootObject = new GameObject(name);
DontDestroyOnLoad(_poolRootObject);
_parent = PoolRoot;
return _poolRootObject;
return _parent;
}
}
public void SetParent(Transform t)
{
Transform newParent = t != null ? t : PoolRoot;
if (_parent != null && _parent != newParent)
{
foreach (T member in Available)
{
member.transform.SetParent(newParent);
}
CheckCleanPoolRoot();
}
_parent = newParent;
}
void CheckCleanPoolRoot()
{
if (_poolRoot != null && _poolRoot != _parent && _poolRoot.childCount == 0)
{
Destroy(_poolRoot.gameObject);
}
}

public override void Return(T member)
{
member.transform.SetParent(PoolRootObject.transform);
member.transform.SetParent(Parent.transform);
CheckCleanPoolRoot();
base.Return(member);
}

newMember.transform.SetParent(PoolRootObject.transform);
newMember.transform.SetParent(Parent.transform);
newMember.gameObject.SetActive(false);
return newMember;
}

base.OnDisable();
if (_poolRoot != null)
{
DestroyImmediate(PoolRootObject);
DestroyImmediate(_poolRoot.gameObject);
Destroy(PoolRootObject);
Destroy(_poolRoot.gameObject);
}
}
}
}

18
UOP1_Project/Assets/Scripts/Pool/Example/LocalPoolTester.cs


private ParticlePoolSO _pool;
private ParticleFactorySO _factory;
private void Start()
private void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
private IEnumerator Start()
// Set the pool parent to this object. The pool's lifetime is now tied to this object. This method can be called at any time,
// But if you do not set the parent before the first request/return is made, the pool members will be parented to a generated object.
//_pool.SetParent(this.transform);
_pool.Prewarm(_initialPoolSize);
List<ParticleSystem> particles = _pool.Request(2) as List<ParticleSystem>;
foreach (ParticleSystem particle in particles)

yield return new WaitForSeconds(2);
// You can set the parent to null at any time and the pool members will be re-parented to a generated object.
// Objects that are currently in use will now target the generated object for parenting when Returned.
_pool.SetParent(null);
yield return new WaitForSeconds(2);
// And back to this object.
_pool.SetParent(this.transform);
}
private IEnumerator DoParticleBehaviour(ParticleSystem particle)

正在加载...
取消
保存