Magical Box  1.15
Modular Particle Engine for Unity3D
Creating custom emitter types

Creating your own emitter types involves the following steps:

  1. Create a class MB<YourEmitterTypeName>Emitter under Assets/Plugins/Magical Box/User EmitterTypes/ and set class attributes
  2. Override some methods
  3. Create a GUI handler class MBEditor<YourEmitterTypeName>EmitterHandler under Assets/Editor/Plugins/Magical Box/User Handlers
  4. Enjoy!

Step 1: Creating the custom class and adding attributes

In this example we want to create a simple point emitter that sets the initial heading to a random vector. First we create our class:

[MBEmitterTypeInfo(Menu = "3D/Point w Rnd Hdg")]
public class MBPointRndEmitter : MBEmitterType 
{
}

The MBEmitterTypeInfo attribute informs the editor about our new class and defines its name in the Change Type dropdown list. Let's add some functionality...

Step 2: Adding functionality

When spawning particles, an emitter asks its attached emitter type to return both position and heading of the new particle. As our point emitter needs no offset from the emitter's position, we don't need to overwrite MBEmitterType.GetPosition() to change it. The heading is defined by MBEmitterType.GetHeading() and by default handles the built-in heading types (Center,Fixed,Trail). We want to ignore all this and just return a random heading:

 public override Vector3 GetHeading(MBParticle PT)
{
        return Random.insideUnitSphere;
}

That's it. But usually you create more complex emitter types with initialization and GUI support, so here's a brief overview of methods you want to override in your future custom emitter types:

MethodPurpose
MBEmitterType.GetPosition()Set particle position in particle system's space
MBEmitterType.GetHeading()Set particle heading
MBEmitterType.OnPlay()Initialization, called when emitter starts playing
MBEmitterType.Validate()Validate properties set in the editor
MBEmitterType.DoGizmos()Drawing gizmos

Step 3: Create a GUI handler

While our custom emitter type will work now, we need to provide a GUI handler class to integrate it in the editor. First we create a handler class:

[MBEmitterTypeHandler(typeof(MBPointRndEmitter))]
public class MBEditorPointRndEmitterHandler : MBEditorEmitterTypeHandler
{
        public override void OnGUI()
        {
                // omit default GUI
                // base.OnGUI();
        }
}

The MBEmitterTypeHandler attribute informs the editor about our GUI handler. We override MBEditorEmitterTypeHandler.OnGUI() to place our custom GUI code. In this example we just omit the drawing of the default GUI because our point emitter ignores all these settings.

Note:
When creating your own GUI you should use the methods provided in MBGUI for simplicity and a uniform look&feel.

Step 4: Enjoy!

You're done, congratulation to your first custom emitter type!

 All Classes Files Functions Variables Enumerations Properties Events