Custom creation / destruction of bullets

By default, the Danmaku Engine uses the methods Object.Instantiate and Object.Destroy for creation and destruction of bullets and emitters but you can override this behaviour with your own implementation.

If you are familiar with the Boehm Garbage Collector of the current Mono-version which is used by Unity, you might know that frequent allocation and deallocation of many objects can lead to significant performance hits every time the garbage collection kicks in. Therefore, we recommend using an object-pool if you want to increase the performance of your game.

Note, that you don't have to assign a bullet library to your DanmakuContexts if you override the default instantiation/destruction-behaviour. However, you can use the BulletLibrary-object for your own implementation.

Overriding instantiation and destruction of bullets

You can override the default instantiation-behaviour for bullets by assigning a method or delegate to the property BulletCreationHandler of a DanmakuContext with the following interface:

DanmakuBullet (BulletModel bulletModel)
  • Return value DanmakuBullet
    An instance of a new DanmakuBullet
     
  • BulletModel bulletModel
    The data-model of the bullet-definition which requested the instantiation. Use this object to access essential data about the bullet-definition.
    Note, that the class BulletModel is stored in the namespace VirtualDropkick.DanmakuEngine.

For overriding the default behaviour for destroying a bullet, assign a method or delegate to the property BulletDestructionHandler of a DanmakuContext with the following interface:

void (DanmakuBullet bullet)

Example

using UnityEngine;
using VirtualDropkick.DanmakuEngine;
using VirtualDropkick.DanmakuEngine.Unity;

public class MyBulletCreator : MonoBehaviour
{
    private DanmakuContext context;

    void Awake()
    {
        context = DanmakuController.Instance.GetContext("MyContext");

        // register creation/destruction-delegates
        context.BulletCreationHandler = CreateBullet;
        context.BulletDestructionHandler = DestroyBullet;
    }

    private DanmakuBullet CreateBullet(BulletModel bulletModel)
    {
        // put your construction logic here...
        BulletLibrary lib = context.bulletLibrary;
        DanmakuBullet prefab = lib.GetBulletPrefab(bulletModel.Id);
        DanmakuBullet bullet = (Instantiate(prefab) as DanmakuBullet);
        
        return bullet;
    }
    
    private void DestroyBullet(DanmakuBullet bullet)
    {
        // put your destruction logic here...
        Destroy(bullet.gameObject);
    }
}

Overriding instantiation and destruction of emitters

To override the default instantiation-behaviour for emitters, assign a method or delegate to the property EmitterCreationHandler of a DanmakuContext with the following interface:

DanmakuEmitter (EmitterModel emitterModel)
  • Return value DanmakuEmitter
    An instance of a new DanmakuEmitter
     
  • EmitterModel emitterModel
    The data-model of the emitter-definition which requested the instantiation. Use this object to access essential data about the emitter-definition.
    Note, that the class EmitterModel is stored in the namespace VirtualDropkick.DanmakuEngine.

For overriding the default behaviour for destroying an emitter, assign a method or delegate to the property EmitterDestructionHandler of a DanmakuContext with the following interface:

void (DanmakuEmitter emitter)

Example

using UnityEngine;
using VirtualDropkick.DanmakuEngine;
using VirtualDropkick.DanmakuEngine.Unity;

public class MyEmitterCreator : MonoBehaviour
{
    private DanmakuContext context;

    void Awake()
    {
        context = DanmakuController.Instance.GetContext("MyContext");

        // register creation/destruction-delegates
        context.EmitterCreationHandler = CreateEmitter;
        context.EmitterDestructionHandler = DestroyEmitter;
    }

    private DanmakuEmitter CreateEmitter(EmitterModel emitterModel)
    {
        // put your construction logic here...
        BulletLibrary lib = context.bulletLibrary;
        DanmakuEmitter prefab = lib.defaultEmitter.prefab;
        DanmakuEmitter emitter = (Instantiate(prefab) as DanmakuEmitter);

        return emitter;
    }
    
    private void DestroyEmitter(DanmakuEmitter emitter)
    {
        // put your destruction logic here...
        Destroy(emitter.gameObject);
    }
}

 


Buy the Danmaku Engine plugin

Leave a comment