antfarmar / Unity-3D-Asteroids

A simple Asteroids clone. In 3D.
The Unlicense
65 stars 15 forks source link

Asteroid Explosions: Prefab ParticleSystems & SoundClips #21

Closed antfarmar closed 8 years ago

antfarmar commented 8 years ago

Asteroid explosions are currently handled identically by 1 prefab for both big & small types.

Could implement 2 different behaviours:

ghost commented 8 years ago

A third option could be to give asteroids an Exploder (TBD) reference which handles explosion effects. Client code could look like this:

// example declaration
public Exploder exploder;

// example usage:
exploder.SmallExplosion(transform.position);
exploder.BigExplosion(transform.position);
exploder.ClusterExplosion(transform.position);

// or data driven
exploder.Explosion("small", transform.position);

The exploder can maintain a list of explosion prefabs.

It can also be made an asset, through ScriptableObject. The benefit of doing so is that you get a single asset to modify when you want to add new kinds of explosions. If it was a component, and if you had two different prefabs that could spawn explosions, you'd have to modify two prefabs instead of a single asset. Both prefabs reference the same Exploder asset.

antfarmar commented 8 years ago

It can also be made an asset, through ScriptableObject.

Sounds like a good exercise for me to learn using ScriptableObject, of which I have currently only read about. Mostly in the manual, but also people hating on it too. Not sure why, though.

Unity Live Training: INTRODUCTION TO SCRIPTABLE OBJECTS

Scriptable Objects are amazing data containers. They don't need to be attached to a GameObject in a scene. They can be saved as assets in our project. Most often, they are used as assets which are only meant to store data, but can also be used to to help serialize objects and can be instantiated in our scenes. We won't cover serialization in this session, but will touch on how Scriptable Objects can help us with serializing things. We will cover not only what Scriptable Objects are, but show some very simple example uses for Scriptable Objects.

antfarmar commented 8 years ago

It can also be made an asset, through ScriptableObject.

Took this route, found in commit 0297875.

This current implementation may need to change due to pool usage though:

A ScriptableObject asset might not be the right use case when used along with Object Pools instead of one-time Instantiations.

antfarmar commented 8 years ago

Reverted the implementation then recommitted in commit daf2886, due to a bug.

The Exploder ScriptableObject asset created and was holding old references to old explosion clones (760 of them!) that were (wrongly) being created OnEnable when building their pools during development & testing.

Note also that for ScriptableObject assets, since they are primarily designed as serialized data containers, both OnEnable & OnDisable messages are called in the Editor, along with during play mode.