Add resource pool manager
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
using XGame;
|
||||
|
||||
public sealed class ResourcePoolManagerTests
|
||||
{
|
||||
private const string EffectPath = "Assets/Game/Art/Effect/Prefab/test.prefab";
|
||||
private GameObject _mainBefore;
|
||||
private GameObject _cacheBefore;
|
||||
private GameObject _parent;
|
||||
private GameObject _instance;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_mainBefore = GameObject.Find("Main");
|
||||
_cacheBefore = _mainBefore != null
|
||||
? _mainBefore.transform.Find("Cache")?.gameObject
|
||||
: null;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
ResourcePoolManager.ClearAll();
|
||||
|
||||
if (_parent != null)
|
||||
{
|
||||
Object.DestroyImmediate(_parent);
|
||||
_parent = null;
|
||||
}
|
||||
|
||||
if (_instance != null)
|
||||
{
|
||||
Object.DestroyImmediate(_instance);
|
||||
_instance = null;
|
||||
}
|
||||
|
||||
GameObject main = GameObject.Find("Main");
|
||||
if (_mainBefore == null && main != null)
|
||||
{
|
||||
Object.DestroyImmediate(main);
|
||||
}
|
||||
else if (_mainBefore != null && _cacheBefore == null)
|
||||
{
|
||||
Transform createdCache = _mainBefore.transform.Find("Cache");
|
||||
if (createdCache != null)
|
||||
{
|
||||
Object.DestroyImmediate(createdCache.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
_mainBefore = null;
|
||||
_cacheBefore = null;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReleaseAndTryGet_ReusesInactiveInstanceForSamePath()
|
||||
{
|
||||
_parent = new GameObject("parent");
|
||||
_instance = new GameObject("pooled-effect");
|
||||
_instance.transform.SetParent(_parent.transform);
|
||||
_instance.transform.localPosition = new Vector3(1f, 2f, 3f);
|
||||
_instance.SetActive(true);
|
||||
|
||||
ResourcePoolManager.Release(EffectPath, _instance);
|
||||
|
||||
Assert.IsFalse(_instance.activeSelf);
|
||||
Assert.IsNotNull(_instance.transform.parent);
|
||||
Assert.That(_instance.transform.parent.name, Is.EqualTo("Cache"));
|
||||
Assert.IsNotNull(_instance.transform.parent.parent);
|
||||
Assert.That(_instance.transform.parent.parent.name, Is.EqualTo("Main"));
|
||||
Assert.That(ResourcePoolManager.GetPooledCount(EffectPath), Is.EqualTo(1));
|
||||
|
||||
GameObject reused = ResourcePoolManager.TryGet(EffectPath, _parent.transform, Vector3.one, Quaternion.identity);
|
||||
|
||||
Assert.AreSame(_instance, reused);
|
||||
Assert.IsTrue(reused.activeSelf);
|
||||
Assert.AreSame(_parent.transform, reused.transform.parent);
|
||||
Assert.That(reused.transform.localPosition, Is.EqualTo(Vector3.one));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2c82760d7f3940d48af3d0a2b49c68a1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user