Add tutorial for Conditions (#206)

This commit is contained in:
Miika Lönnqvist 2020-12-21 00:42:03 +01:00 committed by GitHub
parent fcf0b2c5a9
commit cede860ff4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 92 additions and 1 deletions

View File

@ -14,6 +14,7 @@
- [Variable Instancer](./tutorials/variable-instancer.md)
- [Event Instancer](./tutorials/event-instancer.md)
- [Generator](./tutorials/generator.md)
- [Conditions](./tutorials/conditions.md)
- [Advanced example](./tutorials/advanced-example.md)
- [Usage with UniRX](./tutorials/unirx.md)
- API

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

View File

@ -0,0 +1,85 @@
---
id: conditions
title: Conditions
hide_title: true
sidebar_label: Conditions
---
# Conditions
`Conditions` control whether a listener responds to an event or not. `Conditions` are specialized `Functions`.
The following script will add a condition for the type `int` to the Atom creation menu:
```cs
using UnityEngine;
using UnityAtoms;
using UnityAtoms.BaseAtoms;
// Set the icon you will see in the editor
[EditorIcon("atom-icon-sand")]
// Set the path in the asset creation menu
[CreateAssetMenu(menuName = "Unity Atoms/Conditions/Int/MultipleOf", fileName = "MultipleOf")]
public class MultipleOf : IntCondition
{
// Can be set via the Inspector
public int multiple;
public override bool Call(int value)
{
// The condition implementation must return a boolean value
return value % multiple == 0;
}
}
```
> **Note:** Use the generator to generate the corresponding `Condition` base class for any type you might need in your project.
Notice how the script extends the `IntCondition` class and the usual `Call` method of a `Function` returns a `bool`.
It is now possible to create new conditions for the type `int` under the path described in the script:
![create-condition](../assets/conditions/create-condition.png)
Create two conditions and assign a different parameter for each:
![multiple-of-three](../assets/conditions/multiple-of-three.png)
![multiple-of-five](../assets/conditions/multiple-of-five.png)
Now these assets can be used in any `IntListener`. Create a simple logging script to test it out:
```cs
using UnityEngine;
public class LogSomething : MonoBehaviour
{
public void Report(int value)
{
Debug.Log(gameObject.name + " reports: " + value);
}
}
```
To test this, create an `IntEvent` called `SomeIntChanged`. This will be used to raise it from the `Inspector` during testing:
![test-int-event](../assets/conditions/test-int-event.png)
Create a new `GameObject` and rename it to `Unfiltered` for testing purposes. Add an `IntEventReferenceListener` and `LogSomething` components to the `GameObject` and wire the listener to listen for `SomeIntChanged` event. Add a dynamic Unity Event response (the `Report` method of the `LogSomething` component of the same `GameObject`). The end result should look like this:
![unfiltered-gameobject](../assets/conditions/unfiltered-gameobject.png)
Now `Unfiltered` will simply respond to any value passed when `SomeIntChanged` is raised. Enter play mode and test it by clicking on "Raise" from the `Inspector`. The console should log the value in the "Inspector Raise Value" field.
Duplicate `Unfiltered` and rename the duplicate to `Filtered`. Add the previously created condition assets to the `Conditions` array and choose a logical operator to test. `Filtered` now looks like this:
![filtered-gameobject](../assets/conditions/filtered-gameobject.png)
Now enter play mode again and see that the conditions apply. The `Filtered` one only responds when the value fulfills either of the conditions, since the logical operator is `And`:
![conditions-in-use](../assets/conditions/conditions-in-use.png)
That's it! The conditions are taking effect and can be swapped in or out in the `Inspector` without coding.

View File

@ -128,6 +128,10 @@
"title": "Advanced example",
"sidebar_label": "Advanced example"
},
"tutorials/conditions": {
"title": "Conditions",
"sidebar_label": "Conditions"
},
"tutorials/event-instancer": {
"title": "Event Instancer",
"sidebar_label": "Event Instancer"

View File

@ -24,7 +24,8 @@
"ids": [
"tutorials/variable-instancer",
"tutorials/event-instancer",
"tutorials/generator"
"tutorials/generator",
"tutorials/conditions"
]
},
{