public void TestFirstSpawn() { Transform inst; // 根节点未创建 IsNull(GameObject.Find(PoolManager.RootName)); // Spawn 一个实例 inst = PoolManager.Spawn(poolName, prefabName); // 根节点(ObjectPools)已创建 IsNotNull(GameObject.Find(PoolManager.RootName)); // 以 poolName 为名的子节点也被创建 Transform group = PoolManager.GetGroup(poolName); IsNotNull(group); // 子节点的名称与 poolName 相同 AreEqual(group.name, poolName); // 创建的实例上被添加了 PoolEntity 组件 PoolEntity comp = inst.GetComponent <PoolEntity>(); IsNotNull(comp); }
public void TestSpawnPrefabNamePosRot() { Vector3 pos = new Vector3(1, 2, 3); Quaternion rot = new Quaternion(1, 2, 3, 4); Transform inst; inst = PoolManager.Spawn(poolName, prefabName, pos, rot); PoolEntity comp = inst.GetComponent <PoolEntity>(); Transform group = PoolManager.GetGroup(poolName); // 实例被创建 IsNotNull(inst); // 实例被挂接在 poolName 节点下 AreSame(inst.parent, group); // 已设置正确的位置和旋转 AreEqual(inst.position, pos); AreEqual(inst.rotation, rot); // PoolEntity 的参数都正确 AreEqual(comp.PoolName, poolName); AreSame(comp.Group, group); AreSame(comp.Prefab, MMGame.Prefab.Load(prefabName)); }
/// <summary> /// 创建一个新的物体并挂接一个 PoolEntity 组件。 /// </summary> /// <param name="prefabPool">Prefab 对象池。</param> /// <param name="transformPool">Transform 对象池。</param> /// <param name="enable">是否激活。</param> /// <param name="position">创建后的初始位置。</param> /// <param name="rotation">创建后的初始方向。</param> /// <returns>创建出来的实例。</returns> private static Transform Instantiate(PrefabPool prefabPool, TransformPool transformPool, bool enable, out PoolEntity comp, Vector3 position = default(Vector3), Quaternion rotation = default(Quaternion)) { if (count >= Instance.maxCount) { throw new ApplicationException( "POOL MANAGER: \nThe number of game objects has reached its limit! Try to increase the Max Count of PoolManager."); } // 创建新的实例 Transform xform; xform = Instantiate(prefabPool.Prefab, position, rotation) as Transform; prefabPool.Count += 1; xform.name += prefabPool.Count.ToString("#000"); // 添加 PoolEntity/PoolParticle 组件 ParticleSystem ps = xform.GetComponent <ParticleSystem>(); comp = ps ? xform.gameObject.AddComponent <PoolParticle>() : xform.gameObject.AddComponent <PoolEntity>(); comp.PrefabPool = prefabPool; comp.Prefab = prefabPool.Prefab; comp.Index = count; comp.OriginalScale = xform.localScale; // 添加到大数组 allTransforms[count] = xform; allPoolEntities[count] = comp; count += 1; // 添加到 prefab 对象池 if (enable) { comp.IsDespawned = false; } else { prefabPool.DisabledPool.Push(comp.Index); } // 添加到 Transform 对象池 // need comp.Index AddToTransformPool(xform, comp, transformPool, enable); return(xform); }
/// <summary> /// 回收一个实例。 /// </summary> /// <param name="xform">实例。</param> /// <param name="reparent">是否重新挂接到所属的对象池节点。</param> /// <exception cref="ApplicationException">在给出的实例上没有找到 PoolEntity 组件。</exception> public static void Despawn(Transform xform, bool reparent = true) { PoolEntity comp = xform.GetComponent <PoolEntity>(); if (!comp) { throw new ApplicationException(string.Format( "Trying to despawn a game object that was not spawned by pool manager: {0}", xform.name)); } Despawn(comp.Index, reparent); }
// ------------------------------------------------------ // Spawn // ------------------------------------------------------ /// <summary> /// 将一个物体加入对象池。 /// </summary> /// <param name="xform">物体实例。</param> /// <param name="comp">物体实例上的 PoolEntity 组件。</param> /// <param name="pool">对象池 Transform。</param> /// <param name="enable">物体是否活跃。</param> private static void AddToTransformPool(Transform xform, PoolEntity comp, TransformPool pool, bool enable) { if (enable) { pool.EnabledPool.Add(comp.Index); } else { pool.DisabledPool.Add(comp.Index); } comp.TransformPool = pool; comp.Group = pool.Group; comp.PoolName = pool.PoolName; xform.SetParent(pool.Group); }
/// <summary> /// 回收一个实例。 /// </summary> /// <param name="index">实例在大数组中的索引。</param> /// <param name="reparent">是否重新挂接到所属的对象池节点。</param> /// <exception cref="ApplicationException">实例早已被回收。</exception> public static void Despawn(int index, bool reparent = true) { PoolEntity comp = allPoolEntities[index]; if (!comp) { #if !UNITY_EDITOR Debug.Log("It seems that the whole object pool has been destroyed."); #endif return; } TransformPool transformPool = transformPoolDic[comp.PoolName]; PrefabPool prefabPool = prefabPoolDic[comp.Prefab]; if (comp.IsDespawned) { throw new ApplicationException(string.Format( "Game object seems to be despawned already: {0}", GetByIndex(index).name)); } prefabPool.DisabledPool.Push(index); transformPool.EnabledPool.Remove(index); transformPool.DisabledPool.Add(index); // 1. 调用所有 PoolBehaviour 组件的 OnDespawn() 方法 // 2. 特定类型的 PoolEntity 有时候需要在 Despawn 时执行一些特定的工作,如 PoolParticle 清除粒子等 comp.OnDespawn(); comp.gameObject.SetActive(false); if (reparent) { allTransforms[index].SetParent(comp.Group); } if (comp.OnDespawnedCallback != null) { comp.OnDespawnedCallback(); comp.OnDespawnedCallback = null; } comp.IsDespawned = true; }
public void TestSpawnPrefabName() { Transform inst; inst = PoolManager.Spawn(poolName, prefabName); PoolEntity comp = inst.GetComponent <PoolEntity>(); Transform group = PoolManager.GetGroup(poolName); // 实例被创建 IsNotNull(inst); // 实例被挂接在 poolName 节点下 AreSame(inst.parent, group); // PoolEntity 的参数都正确 AreEqual(comp.PoolName, poolName); AreSame(comp.Group, group); AreSame(comp.Prefab, Prefab.Load(prefabName)); // position && rotation(默认,未测) }