|
| 1 | +# Output Event Handlers |
| 2 | + |
| 3 | +A VFXOutputEventHandler is an API helper that hooks into an [Output Event](Contexts.md#output-events) to allow you to execute scripts based on the event. |
| 4 | + |
| 5 | +The Visual Effect Graph includes a set of example scripts as a sample. For information on how to install the sample, see [Installing sample scripts](#installing-sample-scripts). |
| 6 | + |
| 7 | +## Output Event Handler API |
| 8 | + |
| 9 | +To create your own output event handler, write a script that extends the `UnityEngine.VFX.Utility.VFXOutputEventHandler` class. |
| 10 | + |
| 11 | +When you write a MonoBehaviour that extends this class, it reduces the amount of code required to perform a hook. This is because the base class does the job of filtering the event and calls the following method : |
| 12 | + |
| 13 | +`override void OnVFXOutputEvent(VFXEventAttribute eventAttribute)` |
| 14 | + |
| 15 | +When you implement this method, Unity calls it every time the event triggers and passes in the event's attributes. |
| 16 | + |
| 17 | +### Example |
| 18 | + |
| 19 | +The following example teleports a Game Object to the Given position when it receives an event. |
| 20 | + |
| 21 | +```c# |
| 22 | +[RequireComponent(typeof(VisualEffect))] |
| 23 | +public class VFXOutputEventTeleportObject : VFXOutputEventHandler |
| 24 | +{ |
| 25 | + public Transform target; |
| 26 | + |
| 27 | + static readonly int kPosition = Shader.PropertyToID("position"); |
| 28 | + |
| 29 | + public override void OnVFXOutputEvent(VFXEventAttribute eventAttribute) |
| 30 | + { |
| 31 | + if(target != null) |
| 32 | + target.position = eventAttribute.GetVector3(kPosition); |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +## Installing Sample Scripts |
| 38 | + |
| 39 | +To help you create your own VFXOutputEventHandler, the Visual Effect Graph package includes a set of example scripts that you can install via the Package Manager. To do this, select the Visual Effect Graph package then, next to **OutputEvent Helpers Sample**, click the **Import** button. |
| 40 | + |
| 41 | +### Using Output Event Helpers |
| 42 | + |
| 43 | +The OutputEvent Helper scripts are deployed into the project as a Sample contained in the folder : `Assets/Samples/Visual Effect Graph/(version)/OutputEvent Helpers/`. |
| 44 | + |
| 45 | +These helpers are MonoBehaviour Scripts that you can add to Game Objects that hold a VisualEffect Component. These scripts will listen for OutputEvents of a given name, and will react to these events by performing various actions. |
| 46 | + |
| 47 | +Some of these scripts can be safely previewed in editor, while other aren't. The Inspector shall display a **Execute in Editor** toggle if the script is able to be previewed. Otherwise, you can press the Play button to experience the behavior in play mode. |
| 48 | + |
| 49 | +<u>The Sample Output Event Helpers are the following:</u> |
| 50 | + |
| 51 | +* **VFXOutputEventCMCameraShake** : Upon receiving a given OutputEvent, Triggers a Camera Shake through the Cinemachine Impulse Sources system. |
| 52 | +* **VFXOutputEventPlayAudio** : Upon receiving a given OutputEvent, Plays a sound from an AudioSource |
| 53 | +* **VFXOutputEventPrefabSpawn** : Upon receiving a given OutputEvent, Spawns an invisible prefab game object from a pool of prefabs, at a given position, rotation and angle, and manages its life based on the Event lifetime attribute. Spawned Prefabs can also be customized upon spawn with the help of **VFXOutputEventPrefabAttributeHandler** scripts in order to configure child elements of the prefab (see below). |
| 54 | +* **VFXOutputEventRigidBody** : Upon receiving a given OutputEvent, applies a force to a RigidBody. |
| 55 | +* **VFXOutputEventRigidBody** : Upon receiving a given OutputEvent, triggers a UnityEvent |
| 56 | + |
| 57 | +### Using Output Event Prefab Spawn |
| 58 | + |
| 59 | +The **VFXOutputEventPrefabSpawn** script handles the spawn of hidden prefabs from a pool. These prefabs are instantiated as invisible, and disabled upon enabling this monobehaviour, and destroyed upon disabling the monobehaviour. |
| 60 | + |
| 61 | +Upon receiving a given OutputEvent, the system will look for a free (disabled) prefab, and if any available will : |
| 62 | + |
| 63 | +* enable it |
| 64 | + |
| 65 | +* set its position from the position attribute, if enabled in the inspector |
| 66 | + |
| 67 | +* set its rotation from the angle attribute, if enabled in the inspector |
| 68 | + |
| 69 | +* set its scale from the scale attribute, if enabled in the inspector |
| 70 | + |
| 71 | +* start a coroutine with a delay (based on the lifetime attribute), that will disable (free) the prefab once elapsed, thus making it available for spawn upon a future OutputEvent |
| 72 | + |
| 73 | +* search for any **VFXOutputEventPrefabAttributeHandler** scripts in the prefab instance in order to perform attribute binding. |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +**VFXOutputEventPrefabAttributeHandler** scripts are used to configure parts of the prefab, based on the event that spawned the prefab. Here are two examples bundled with the samples: |
| 78 | + |
| 79 | +* **VFXOutputEventPrefabAttributeHandler_Light** : this script, upon prefab spawn, will set the color and the brightness of the light, based on the OutputEvent color attribute and a brightnessScale property. |
| 80 | +* **VFXOutputEventPrefabAttributeHandler_RigidBodyVelocity** : this script, upon prefab spawn, will set the current rigid body velocity, based on the OutputEvent velocity attribute. |
0 commit comments