Search Unity

PoolManager [By Path-o-logical-Games]

Discussion in 'Assets and Asset Store' started by Rafes, Sep 13, 2011.

  1. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,126
    I just wanted to let all PoolManager users know that the latest Behavior Designer release includes support for PoolManager. I've created a set of tasks which allow you to control PoolManager within a behavior tree. These tasks are very similar to the PlayMaker actions. In addition, there is a small sample project available which should help you get started using these tasks.
     
  2. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Thanks Opsive!!

    When we get our new website up we'll add a section for 3rd-party supporting packages!
     
  3. broesby

    broesby

    Joined:
    Oct 14, 2012
    Posts:
    118
    Hi all,..

    Is it possible to pool only colliders?

    The reason I ask is, that I want to make dense forests in large terrains and I need the trees to have colliders. That is a lot of colliders! Until Unity 5 there is a set limit of +65000 colliders per scene. And no matter what so many colliders is a big performance hit, even when U5 arrives.

    So Advanced Foliage Shader creator Lars Bertram suggested to make an array with maybe 100 pooled colliders and only activate the closest 100 to the player at all times. Would something like that be possible with Pool Manager. I also considered using Target Pro to find the closest ones...

    Two potential issues:

    1) Terrain trees are not normal gameobjects, so I need to figure out how to place the colliders precisely

    2) I read someplace that even deactivated colliders draw performance so it would't matter to deactivate them in the first place - but perhaps that was in an earlier version of Unity...
     
  4. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    PoolManager currently only Pools Transforms (but also caches GameObjects internally for efficiency). We may add a generics-based Pool at some point, but we haven't started on development yet.

    One trick or pattern PoolManager could help with is if you pool interactive and passive trees prefabs and as the player gets near, swap out the passive ones with interactive ones (maybe even higher resolution too). That way you have the simplest tree representation possible except for the ones right near the player. That should drastically limit how many physical-trees you need loaded.

    To attempt to touch on your other points more directly....
    If you mean physical colliders (as opposed to triggers) you should be able to make a child GameObject with rigidbody and collider and deactivate the game object rather than the component. That should stop the rigidbody from processing interactions. However, I would expect a sleeping rigidbody to be extremely cheap, even if not deactivated. I just haven't tried to push the limits of the system to this extent, so you may need to run some tests and contact Unity directly for details.

    TargetPRO (soon to be renamed to TriggerEventPRO) would make area-detection easy, so trees near your player can be detected and handled. However, this requires the trees to have trigger colliders, but perhaps you can just handle largeer groups (areas) of trees as triggers and then have a group-manager component actually process the trees inside it when needed. That should create a sort of level-of-detail approach where you handle larger data sets only when the player is near them, if that makes sense.

    I'm not sure what you mean by your first point. If they aren't GameObjects (have Transforms) then you'll need a point-position array/list. Kind of like a particle system, right?

    I hope this helps!
     
  5. mimminito

    mimminito

    Joined:
    Feb 10, 2010
    Posts:
    780
    Is there a way I can spawn a random object from the SpawnPool?

    EDIT:
    No worries, found you had a forum and there was an answer on there.
     
  6. daudau505

    daudau505

    Joined:
    Jul 19, 2014
    Posts:
    27
    Hi, I'm very interested in this and in need of a pool manager. My main concern is that, will it spawn and de spawn prefab child object/prefab?

    Thanks
     
  7. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    You should only spawn/despawn/pool whole prefabs (from the top transform in the hierarchy of the prefab.) Does this answer your question?
     
  8. daudau505

    daudau505

    Joined:
    Jul 19, 2014
    Posts:
    27
    Thanks for the reapond, I'm trying to make a infinite game with lots of coin, which I have 16 coins organized into an arrow in under a prefab. Is there a better solution to spawn and despawn 16 coins at a time?
     
  9. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    It really depends on the game specifics. You could put a script on your track feature (arrow) which has a drag-and-drop reference to the coin prefab (or just use the PoolManager API to get it). Then when the arrow spawns, have the script spawn and place all the coins. The coins are separate prefabs, and pooled separately, which makes managing the art asset easier too....keep your track and coins seperate.
     
  10. daudau505

    daudau505

    Joined:
    Jul 19, 2014
    Posts:
    27
    Ok, I will get this, and try hopefully I get it to work. Thanks again.
     
  11. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    Hi! Thanks for this great package, I got going with it very fast...although I have a problem understanding what is the proper way of pooling sounds. I have managed to work out pooling of a projectile and visual effect when it's colliding with something, but when trying to pool audio and play it in the same way as visual effect when projectile hit something plays but immediately stops.

    Here is the setup:
    AudioEffectPrefab (pooled object)
    - AudioSource -> audioClip

    here is how to prefab pool is created:

    Code (CSharp):
    1.  var audioEffectPrefabPool = new PrefabPool(w.Projectile.CollisionAudioEffect.transform) {preloadAmount = 30};
    2.     Game.Instance.Controller.ObjectPools[PoolID.AUDIO].CreatePrefabPool(audioEffectPrefabPool);
    here is how I try to spawn it:

    Code (CSharp):
    1.  if (CollisionAudioEffect != null)
    2.     Game.Instance.Controller.ObjectPools[PoolID.AUDIO].Spawn(CollisionAudioEffect, this.transform);
    I guess that there should be some controller that will despawn the audio object when clip ended playing..

    Thanks again, great and very useful package!
     
  12. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Hi,

    I could use a little clarification. It sounds like the audio does spawn and is played but it just stops early? This would indicate that the code to spawn it is working. I would expect it to auto-despawn when done playing too, since you are using that feature of PoolManager by spawning using the AudioSource rather than the Transform. Is your audio a completely separate prefab with the audio at the top of the prefab hierarchy? Just as a sanity-check, is your audio set to not auto-play on start?
     
  13. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    You were right, if I use Spawn with AudioSource than it needs to be separated prefab..also I made a mistake and made spawned audio child of projectile so it it was destroy along with it...

    Thanks, everything works very good!
     
  14. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    If you purchased the "Pool, Target, Constrain Bundle", it is now called "Pool, Trigger, Constrain Bundle" and has also been submitted to the AssetStore and includes the latest PoolManager and UnityConstraints packages as well TriggerEventPRO (TargetPRO 6.0, renamed)!!

    TriggerEventPRO is a MAJOR version. PLEASE READ THE UPGRADE DOCS BEFORE INSTALLING!

    The new version of PoolManager (standalone) is safe to upgrade to as normal.

    See triggerEventPro.path-o-logical.com for more information and the new docs.

    Videos and some final doc clean-up are in progress.

    See all release notes at support.path-o-logical.com/forums
     
  15. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    Hi!

    Can you help me with figuring out the way to properly remove the SpawnPool from the scene.
    At level selection i determine what objects are needed for particular level and create all spawn pools. When level is over
    and i get to the level selection i want to remove all spawn pools in order to create new ones for different level.

    I wanted to loop through all pools, but i am not sure how to remove both pools and instances.
    here is what i have for each level

    Code (CSharp):
    1. public Dictionary<PoolID, SpawnPool> ObjectPools = new Dictionary<PoolID, SpawnPool>();
    so if I loop like this:
    Code (CSharp):
    1. protected void ResetObjectPools()
    2.     {
    3.         foreach (KeyValuePair<PoolID, SpawnPool> kvp in ObjectPools)
    4.         {
    5.            
    6.         }
    7.         ArePoolsInitialized = false;
    8.     }
    what exactly is proper way of removing all instances of pooled objects as well as spawn pool that contain them? Thanks!
     
  16. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    While I am working on this I have another question...There are situations where I want to add object in the pool when it is already added...when that happens it gives me error, but is it possible to give me a warning or is there a easy way to check if object is already added to the pool?

    Thanks man!
     
  17. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Destroy the SpawnPool in the scene and it will clean itself up
     
  18. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Use the Contains() method on the appropriate list using the object's Transform: http://msdn.microsoft.com/en-us/library/bhkz42b3(v=vs.110).aspx

    I assume you want to know if a particular instance is managed by a SpawnPool. For a prefab, checkout the docs for the SpawnPool prefabs dict...

    If you want to know if the object is currently in the pool and spawned, use the SpawnPool itself like any other list: http://docs.poolmanager.path-o-logical.com/example-patterns/using-a-pool-as-a-list

    If you want to know if the object is in the pool even if it isn't currently spawned, You can go through the prefab pools (or get one using the prefab) and test both the spawned and respawned lists: http://docs.poolmanager.path-o-logical.com/code-reference/prefabpool . However, it is probably faster to just wrap your current call in a try statement and handle the failure cases by letting them through. It depends on your use case.
     
  19. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    Hi!

    I ran into problem when trying to move from spawning projectiles as simple spheres (use for testing) to prefabs that have particle systems on them (it looks so much better).
    with spheres i used spawn/despawn overloads that take transform and it worked great. now with particle systems it is different since i should not use despawn any more. this gets me into trouble on several levels :) here is the list:

    - it makes sense to use particle system with looping (i have some simple but nice looking particle systems that look good used as projectiles but they use looping), duration doesn't count in this case so particles are not despawned.
    - also i would like to despawn particle on collision or when it goes out of the screen, but since i shouldn't use despawn i then move the prefab to far away position and let them die (when using particle duration). the problem with this approach is that when prefab is moved to far away place it collides with other particles and that creates more problems (i guess this can be solved with checking layers or tags in OnCollisionEnter)

    so what is the proper pattern in when using prefabs with particle system as projectiles that need to be despawned on collison or similiar. I browsed through forums but couldn't find something similiar. I am doing something completely uncommon or this is so trivial that everybody gets it but me :)

    btw, i am making all prefab pools by code, no inspector setup anywhere (everything worked very good with simple spheres projectiles so i guess that part is fine.

    Thanks!

    (I also posted this in the path-o-logical support forums, i don't know what you read first http://forums.support.path-o-logical.com/viewtopic.php?f=3&t=2267 )
     
  20. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    I replied on our forums.
     
  21. wheelbarrow

    wheelbarrow

    Joined:
    Sep 12, 2011
    Posts:
    177
    Hi guys, I'm finally using PM after purchasing it almost a year ago, and never having used a pooling system before, I have what I hope isn't a stupid question: I set up a pool in a scene as per the docs, and everything works fine, my question relates to how it actually works, i.e. I have a blood effect that spawns from the pool on an npc whenever it is hit by a bullet.
    My health script on the npc destroys the npc when its' health reaches zero. The blood effect only lasts a second or 2, and is not attached to the npc in any way, it just spawns at a position on the npc, and does its' thing, and disappears. Because there is no need for me to despawn the blood effect, is it always available in the pool after it has been used once, or do I need to load up a bunch of them for the various npc's that can get shot in the scene, or if multiple npc's get shot at the same time? For example is one scene I have 23 npc's, who can all get shot at by automated security guns, do I need to load at least 23 blood effect prefabs, or will the pool just keep re-using the same one, seeing as how I never actually have to de-spawn them. Cheers.
     
  22. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Let me see if I can help in list form. Please ask for clarity on anything that needs it
    • You DO need to despawn your effect when it is done to use it again. Be sure it isn't being destroyed.
      • Assuming the blood effect is a ParticleSystem, You can use the convenience overload for Spawn() which takes a ParticleSystem to have it automatically listen for the effect to be done and the auto-despawn.
    • PoolManager only creates new instances in the pool if your game asks for one and one isn't available in the despawned pool to be spawned again. If your effect lasts for 1 second and you fire 20 bullets in that time, you will have 20 effects visible and spawned at the same time.
      • If you want to limit the amount which can be in use at any time, use the First In First Out (FIFO) limit option. If the limit is 10 and a new one is needed, the oldest will be forced to despawn so it can then be respawned. The regular limit option will simply spawn nothing until one becomes available naturally.
    • Preloading is always a good idea. One way to get a good idea of the number you need is to start with 1, play the game for a bit, and then look at how many have been spawned. Then set the preload amount to this amount, or a little less if you feel it was a rare peak amount.
     
  23. wheelbarrow

    wheelbarrow

    Joined:
    Sep 12, 2011
    Posts:
    177
    Ok, thanks for the clarification, I appreciate it, and I'll give the overload option a shot.
    Cheers.
     
  24. OceanBlue

    OceanBlue

    Joined:
    May 2, 2013
    Posts:
    251
    Hi @Rafes
    In my game I have two scenes - the first scene (Home) and a scene for my level that you enter via Home.
    I have my PoolManager in the Home scene that contains all of my game piece elements. I have "Don't destroy on load" selected to true. I did this so that all the game objects are loaded at game start up.

    However, when I'm in the game, if I go to my pause menu and press the Home button to return home, I receive the following log error:
    "A pool with the name 'Game' already exists. This should only happen if a SpawnPool with this name is added to a scene twice."

    Any solution for this?

    Thanks
     
  25. OceanBlue

    OceanBlue

    Joined:
    May 2, 2013
    Posts:
    251
    Can anyone help me at all (re above)? I still can't find a solution @Rafes
     
  26. OnePxl

    OnePxl

    Joined:
    Aug 6, 2012
    Posts:
    307
    I'll take a stab: when you exit the pause menu, does that trigger a load of the home scene? And thus loading a second instance of the pool manager?
     
  27. OceanBlue

    OceanBlue

    Joined:
    May 2, 2013
    Posts:
    251
    Well, yes it seems to be doing that... but i thought that was the idea of Poolmanager? That i can load my pools in at game startup, and don't destroy through all the scenes to help cut down on scene load times?

    But Poolmanager is a game object in the scene - I'm not loading it from script in the home menu.
     
  28. OnePxl

    OnePxl

    Joined:
    Aug 6, 2012
    Posts:
    307
    Since you're not loading from a script, how about an empty scene with just the Poolmanager (with Don't destroy on load set) and load in the Home scene without the Poolmanager first thing. Then reloading the Home scene from the menu won't load an additional Poolmanager. Might that work?
     
  29. eridani

    eridani

    Joined:
    Aug 30, 2012
    Posts:
    655
    I'm gettings a couple of issues in the PoolManager example scenes with Unity 4.6RC1:

    1. In the example scene "Example Scene," after about 10 seconds I get the error message: "SpawnPool Particles: Particles002 has already been despawned. You cannot despawn something more than once!"

    2. In the example scene "OnCreatedDelegateExample," the method RunMe in the script OnCreatedDelegateExample never seems to gets called.

    I know these are just example scenes but it always makes me a tad nervous when example scenes don't work as expected... Please advise. Thanks!
     
  30. eridani

    eridani

    Joined:
    Aug 30, 2012
    Posts:
    655
    Also, is there a reason why PoolManager spawns/despawns Transforms instead of GameObjects? I'm sure there's a good reason, probably memory related right? Thank you
     
  31. kurayami88

    kurayami88

    Joined:
    Aug 30, 2013
    Posts:
    493
    Hi,

    let me answer this question for you...
    transform and gameObject is interchangable... it highly depends on which one you are working on more... in this case, he is working on the transform more since transform holds values for the scale, rotation, position etc of which the gameObject type does not...

    you can always switch between the two by calling gameObject.transform vs transform.gameObject....
    in my case, i did find it a tad annoying at first when i use PoolManager because I too was working with GameObjects... but i found it ok once i knew i could easily make the switch...

    ----

    so for example you have a gameObject you want to pool... just call ".Spawn(yourObject.transform)"
    and if you want it to be back in the gameObject mode, just add this simple extra word at the back
    ".Spawn(yourObject.transform).gameObject"

    same goes for the opposite...
    just call ".Despawn(yourObject.transform)"
     
  32. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    The latest version takes GameObjects too as a convenience.
     
    kurayami88 likes this.
  33. silentneedle

    silentneedle

    Joined:
    Mar 14, 2013
    Posts:
    280
    Importing the current Poolmanager version from the AssetStore results in a "Error while importing package: Package has unknown format" @ Unity 4.6
     
  34. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    I had someone else report they got the Unity 5 version from Unity 4. That shouldn't be possible. If you can confirm this please contact the Unity Store and CC (or follow up with) support@path-o-logical.com.
     
  35. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,683
    I think something like the above happened with Skyshop recently, where if a person had a lower version of U5 than the asset, it would try to import download / import the 4.6 version.

    So if PM was sent out under 5.0.1 and the user is running 5.0.0, then it would try to load in the 4.6 version. Or at least I think that was the problem. (?)
     
  36. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    I contacted the AssetStore. At least they restored the 4.0.1+ version. I reported again that someone in Unity 4 received the Unity 5 version. They haven't heard of this happening before so please report it if you have had this happen to you.

    If anyone needs it, contact support@path-o-logical.com if you need this older version. There are no recent updates. We are only developing for Unity 5 now (though of course we still support Unity 4 users!)
     
  37. comixplay

    comixplay

    Joined:
    Sep 27, 2013
    Posts:
    104
    Hi,
    I'm trying to spawn a rectTransform and getting this warning at runtime (Unity 4.6.4p3):

    Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, which can prevent common UI scaling issues.
    UnityEngine.Transform:set_parent(Transform)
    PathologicalGames.PrefabPool:SpawnNew(Vector3, Quaternion) (at Assets/Plugins/PathologicalGames/PoolManager/SpawnPool.cs:1706)
    PathologicalGames.PrefabPool:SpawnInstance(Vector3, Quaternion) (at Assets/Plugins/PathologicalGames/PoolManager/SpawnPool.cs:1625)
    PathologicalGames.SpawnPool:Spawn(Transform, Vector3, Quaternion, Transform) (at Assets/Plugins/PathologicalGames/PoolManager/SpawnPool.cs:437)
    PathologicalGames.SpawnPool:Spawn(Transform, Vector3, Quaternion) (at Assets/Plugins/PathologicalGames/PoolManager/SpawnPool.cs:511)
    GUIControlScript:AttachCreatureToGUI(GenericCreatureControlScript) (at Assets/Scripts/GUIControlScript.cs:834)
     
  38. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Hi, I'm not in front of my computer right now, but I know we support this in Unity 5. I'll have to check if we back ported this feature (new GUi handling).
     
  39. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,315
    problem is not related to Poolmanager, do not parent it with poolmanager, then unity have a built in function to parent UGUI stuff, use it just after spawning it and add a False boolean parameter.
     
  40. CoCoNutti

    CoCoNutti

    Joined:
    Nov 30, 2009
    Posts:
    513
    Hi
    I'm having trouble scaling spawned objects. I realise that it may be due to the fact it's parented to the pool manger GO, but is there a way around it? I don't want to unparent the objects.

    I've tried unsuccessfully (spawned objects always have scale of (1,1,1) )
    Code (csharp):
    1.  
    2. Transform myObj2 = PoolManager.Pools["obj2"].Spawn(spawned);
    3. //Scale
    4. float randomScale = Random.Range(0.8f, 1.3f);
    5. myObj2.localScale = new Vector3(randomScale, randomScale, 1f);
    6.  
    Thanks for the help
     
  41. DickFeynman

    DickFeynman

    Joined:
    Jul 17, 2015
    Posts:
    16
    Does your "myObj2" GameObject have a MonoBehaviour attached to itself? If yes, you can try to use the OnSpawned event (http://docs.poolmanager.path-o-logical.com/code-reference/events), thus changing the scale into that function's body.
     
  42. CoCoNutti

    CoCoNutti

    Joined:
    Nov 30, 2009
    Posts:
    513
  43. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    PoolManager doesn't stop you from manipulating your objects. The parent only matters because you are doing a localscale but if the parent scale is 1, 1, 1 then it is the same as changing the global scale (the parent has no effect).
     
  44. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    is it possible to preload more objects if given object is already in pool? so if initially is preloaded certain number of objects in the pool and later if more is need to preload more? I have objects that use pooled objects but i don't know in compile time their number
     
  45. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Thanks for posting!

    I don't see how parenting to the pool transform makes any difference. It isn't anything special, just optional organizational help. It definitely parents on instantiation. PoolManager doesn't have the low level control that would be needed to do anything more than instance then parent..
     
  46. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Instances are pooled. Prefabs are used to make instances. You wouldn't make more instances from an instance (unless you really know what you are doing). You can make more instances just by spawning more of a prefab than you have available. If you really need to preload you will need to make a guess at how many you need up front though.
     
  47. Green-Jungle

    Green-Jungle

    Joined:
    Jun 10, 2014
    Posts:
    79
    Hello.I'm having a problems.I received an errors: KeyNotFoundException: A Pool with the name '' not found. I create a gameboject after attack Spawn Pool for it. I push name "Bullet" and in code of other class I call: PoolManager.Pools["Bullet"].Spawn(rocket.transform, bullet.transform.position, Quaternion.identity); Finally I received a errror as above. Please help me! Thank you so much.
     
  48. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    I'm not sure, based on this description, what your issue is. Could you please email support@path-o-logical.com with screengrabs of your SpawnPool component and anything else you think will help?
     
  49. Ghopper21

    Ghopper21

    Joined:
    Aug 24, 2012
    Posts:
    170
    Hi Rafes, just got PoolManager from the store and am looking forward to playing with it. One question for now. Let's say I spawn a prefab with a child game object. Later I despawn just the child. Does PoolManager handle this case as expected, i.e. re-uses the child when another instance of the prefab is spawned?
     
  50. Ghopper21

    Ghopper21

    Joined:
    Aug 24, 2012
    Posts:
    170
    A follow-up question -- if I spawn a prefab with a child, thus creating/getting a parent game object with a child, then despawn the parent object, is the child object automatically despawned also, i.e. just like destroying a parent object will automatically destroy the child object?