Page 1 of 1

Using Chipmunk2d for Unity objects with a pool manager

PostPosted: Wed Aug 14, 2013 12:51 am
by Steve Oldmeadow
It is very common in Unity development to use a pool manager to avoid garbage collection spikes. Has any thought been given to using C2D4U with pool managers? For example, I'd love to see the CrayonBall demo rewritten to work with pooled objects rather than instantiate and destroy.

Re: Using Chipmunk2d for Unity objects with a pool manager

PostPosted: Wed Aug 14, 2013 10:47 am
by slembcke
RANT WARNING: So my opinion of pooling as a optimization technique is pretty low. I do use it internally in Chipmunk in a few places not to avoid memory management costs, but to make things more cache friendly. It's one of the very last optimizations I usually try because it's generally minimally effective and maximally difficult. It's very easy to make really bizarre bugs due to the object lifecycle complications.

In Unity it's even more problematic since components cannot exist in a valid state in memory if they aren't in existence in a scene, and Unity's object lifecycle for components is already overly complicated. Since you can't move components around once they are created, you need to pool things at the GameObject level which makes it even a little more complicated when multiple MonoBehaviours are involved. You should be able to deactivate/activate entire GameObjects okay, but I still wouldn't. ;)

GC is an inevitable cost of using Unity in my mind. I feel like it's less work to use a different engine/framework that doesn't have that problem than to deal with that in Unity.

Re: Using Chipmunk2d for Unity objects with a pool manager

PostPosted: Wed Aug 14, 2013 10:11 pm
by Steve Oldmeadow
@Scott - I understand your opinion but when Unity themselves tell people to use object pooling http://docs.unity3d.com/Documentation/M ... ement.html (near the bottom) then you have to expect that some customers will want to do it. In that document they mention using pooling for projectiles and that is one of my use cases, I'd also like to be able to use Chipmunk for some particle effects too.

There are also several 3rd party pooling libraries and PoolManager is currently number 1 in the scripting charts and the second most popular asset in the current "Madness" sale.

AFAIK all the pool managers work by having a pool manager object in the scene graph that instantiates prefabs and keeps inactive objects as children i.e. they are simply activating and deactivating game objects as you suggested. Chipmunk2D seems to work in this context but I haven't tested it extensively.

It is something I want to do so I'll plug away and see how I go. If anybody else is interested in getting Chipmunk2D to work with a pool manager please speak up.

Re: Using Chipmunk2d for Unity objects with a pool manager

PostPosted: Thu Aug 15, 2013 12:25 am
by Steve Oldmeadow
I've got the CrayonBall demo working with PoolManager. I've attached a Unity package with the modified code, it doesn't include PoolManager or Chipmunk2D (for obvious reasons) so you need those two packages installed for it to work.

The main changes were to move stuff from Awake into an Initialise method so that it can be called when an object is spawned and calling Chipmunk.UpdatedTransform when the object is spawned as per the code below.

Code: Select all
public void OnSpawned() {
   this.Initialise();
   despawned = false;   
   ChipmunkBody body = GetComponent<ChipmunkBody>();
   if (body != null) {
      Chipmunk.UpdatedTransform(this.gameObject);
   }
}