Skip to content

Commit eb830bb

Browse files
Thomas Ichépeeweek
authored andcommitted
Output Event Helpers (#58)
* Base Commit * Moved Files down one folder * Updated Package Configuration, CHANGELOG and documentation * Fixed Behavior of ExposedProperty by implementing a simple Drawer / Debug Behaviour * Small fixes and checks * Updated Documentation / Renamed Attribute handler RigidBody to RigidBodyVeolcity + Safe check * Small Fixes * Added the Execute In Editor Capability + Custom Inspectors in order to handle the capability correctly * Other custom Editors + Helpbox * Added CMCameraShake Editor + Capabilities / Fixed Possible Circular Reference in Prefab Spawn * Removed Rerouting Helpers as CopyValuesFrom is broken at the moment. * Fixed ASMDEFs / class accessibility * Updated Documentation * Fixed Class Accessibility for ExposedPropertyDrawer * Updated Changelog (missing entry for Exposed Proeprty Custom Property Drawer) Co-authored-by: Thomas ICHÉ <peeweek@gmail.com>
1 parent 293b0d4 commit eb830bb

File tree

48 files changed

+1335
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1335
-0
lines changed

com.unity.visualeffectgraph/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
6262
- Compute culling of particle which have their alive attribute set to false in output
6363
- Mesh and lit mesh outputs can now have up to 4 differents meshes that can be set per Particle (Experimental)
6464
- Screen space per particle LOD on mesh and lit mesh outputs (Experimental)
65+
- Compare operator can take int and uint as inputs
66+
- New operator : Sample Signed distance field
67+
- Added Output Event Handler API
68+
- Added Output Event Handler Samples
69+
- Added ExposedProperty custom Property Drawer
70+
6571

6672
### Fixed
6773
- Moved VFX Event Tester Window visibility to Component Play Controls SceneView Window

com.unity.visualeffectgraph/Documentation~/Contexts.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ For a breakdown of context compatibility, see the table below.
3737
| **Event** | **None** | **SpawnEvent** (1+) | **None** |
3838
| **Spawn** | **SpawnEvent** (1+) | **SpawnEvent** (1+) | Has two input flow slots which start and stop the **Spawn** context respectively. |
3939
| **GPU Event** | **None** | **SpawnEvent** | Outputs to **Initialize** Context |
40+
| **Output Event** | **SpawnEvent (1+)** | | Outputs a CPU SpawnEvent back to the Visual Effect component. |
4041
| **Initialize** | **SpawnEvent** (1+) or **GPUSpawnEvent** (1+) | **Particle** (1) | Input types are either **SpawnEvent** or **GPUSpawnEvent**. These input types are mutually exclusive.<br/>Can output to **Particle Update** or **Particle Output**. |
4142
| **Update** | **Particle** (1) | **Particle** (1+) | Can output to a **Particle Update** or **Particle Output**. |
4243
| **Particle Output** | **Particle** (1) | **None** | Can either have input from an **Initialize** or **Update** Context.<br/>No output. |
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.

com.unity.visualeffectgraph/Documentation~/TableOfContents.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* [Using Visual Effects with Timeline](Timeline.md)
2222
* [Property Binders](PropertyBinders.md)
2323
* [Event Binders](EventBinders.md)
24+
* [Output Event Handlers](OutputEventHandlers.md)
2425
* Pipeline Tools
2526
* [ExposedProperty Helper](ExposedPropertyHelper.md)
2627
* [Vector Fields](VectorFields.md)

com.unity.visualeffectgraph/Editor/Utilities/ExposedProperty.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using UnityEditor.UIElements;
2+
using UnityEngine;
3+
using UnityEngine.UIElements;
4+
using UnityEngine.VFX.Utility;
5+
6+
namespace UnityEditor.VFX.Utility
7+
{
8+
[CustomPropertyDrawer(typeof(ExposedProperty))]
9+
class ExposedPropertyDrawer : PropertyDrawer
10+
{
11+
const string k_PropertyName = "m_Name";
12+
13+
public override VisualElement CreatePropertyGUI(SerializedProperty property)
14+
{
15+
return new PropertyField(property.FindPropertyRelative(k_PropertyName), ObjectNames.NicifyVariableName(property.name));
16+
}
17+
18+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
19+
{
20+
EditorGUI.PropertyField(position, property.FindPropertyRelative(k_PropertyName), new GUIContent(ObjectNames.NicifyVariableName(property.name)));
21+
}
22+
}
23+
}

com.unity.visualeffectgraph/Editor/Utilities/ExposedProperty/ExposedPropertyDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.visualeffectgraph/Runtime/Utilities/OutputEventHandlers.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace UnityEngine.VFX.Utility
2+
{
3+
[ExecuteAlways]
4+
[RequireComponent(typeof(VisualEffect))]
5+
public abstract class VFXOutputEventHandler : MonoBehaviour
6+
{
7+
public abstract bool canExecuteInEditor { get; }
8+
public bool executeInEditor = true;
9+
10+
public ExposedProperty outputEvent = "On Received Event";
11+
12+
protected VisualEffect m_VisualEffect { private set; get; }
13+
14+
protected virtual void OnEnable()
15+
{
16+
m_VisualEffect = GetComponent<VisualEffect>();
17+
if (m_VisualEffect != null)
18+
{
19+
m_VisualEffect.outputEventReceived += OnOutputEventRecieved;
20+
}
21+
}
22+
23+
protected virtual void OnDisable()
24+
{
25+
if(m_VisualEffect != null)
26+
m_VisualEffect.outputEventReceived -= OnOutputEventRecieved;
27+
}
28+
29+
30+
void OnOutputEventRecieved(VFXOutputEventArgs args)
31+
{
32+
if (Application.isEditor
33+
&& !Application.isPlaying
34+
&& (!executeInEditor || !canExecuteInEditor))
35+
return;
36+
37+
if (args.nameId == outputEvent)
38+
{
39+
OnVFXOutputEvent(args.eventAttribute);
40+
}
41+
}
42+
43+
public abstract void OnVFXOutputEvent(VFXEventAttribute eventAttribute);
44+
45+
}
46+
}

com.unity.visualeffectgraph/Runtime/Utilities/OutputEventHandlers/VFXOutputEventHandler.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)