Skip to content

Commit c80e1c0

Browse files
authored
Merge pull request #1 from Thundernerd/develop
Initial release
2 parents c9aabb7 + 741d2c4 commit c80e1c0

Some content is hidden

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

43 files changed

+1206
-0
lines changed

.codacy.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exclude_paths:
2+
- '*.md'
3+
- '*.json'

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
## [1.0.0] - 2020-07-31
4+
5+
### Added
6+
- Initial version

CHANGELOG.md.meta

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

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Christiaan Bloemendaal
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

LICENSE.md.meta

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

README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Safe Event
2+
3+
<p align="center">
4+
<img alt="GitHub package.json version" src ="https://img.shields.io/github/package-json/v/Thundernerd/Unity3D-SafeEvent" />
5+
<a href="https://github.com/Thundernerd/Unity3D-SafeEvent/issues">
6+
<img alt="GitHub issues" src ="https://img.shields.io/github/issues/Thundernerd/Unity3D-SafeEvent" />
7+
</a>
8+
<a href="https://github.com/Thundernerd/Unity3D-SafeEvent/pulls">
9+
<img alt="GitHub pull requests" src ="https://img.shields.io/github/issues-pr/Thundernerd/Unity3D-SafeEvent" />
10+
</a>
11+
<a href="https://github.com/Thundernerd/Unity3D-SafeEvent/blob/master/LICENSE.md">
12+
<img alt="GitHub license" src ="https://img.shields.io/github/license/Thundernerd/Unity3D-SafeEvent" />
13+
</a>
14+
<img alt="GitHub last commit" src ="https://img.shields.io/github/last-commit/Thundernerd/Unity3D-SafeEvent" />
15+
</p>
16+
17+
An event class that has extra checks to help prevent mistakes.
18+
19+
It will notify you when you've subscribed more than once with the same callback. In case your callback lies within a MonoBehaviour it will also detect if that behaviour and/or the GameObject has been destroyed and will let you know if you forgot to unsubscribe from the event.
20+
21+
## Installation
22+
1. The package is available on the [openupm registry](https://openupm.com). You can install it via [openupm-cli](https://github.com/openupm/openupm-cli).
23+
```
24+
openupm add net.tnrd.safeevent
25+
```
26+
2. You can also install via git url by adding these entries in your **manifest.json**
27+
```json
28+
"net.tnrd.safeevent": "https://github.com/Thundernerd/Unity3D-SafeEvent.git",
29+
```
30+
31+
## Usage
32+
33+
There are three versions of Safe Event that you can use. One without any parameters, one with a single parameter, and one with two parameters.
34+
35+
### Definition
36+
37+
The definition is the same as you would define an Action
38+
39+
```c#
40+
private SafeEvent onFooEvent;
41+
```
42+
43+
```c#
44+
private SafeEvent<int> onFooEvent;
45+
```
46+
47+
```c#
48+
private SafeEvent<int, string> onFooEvent;
49+
```
50+
51+
### Subscriptions
52+
53+
Subscribing and unsubscribing can be done in two ways.
54+
55+
One way is through the Subscribe and Unsubscribe methods
56+
57+
```c#
58+
private void SubscribeToEvent()
59+
{
60+
onFooEvent.Subscribe(ExecuteOnEvent);
61+
}
62+
63+
private void ExecuteOnEvent()
64+
{
65+
[...]
66+
}
67+
68+
private void UnsubscribeFromEvent()
69+
{
70+
onFooEvent.Unsubscribe(ExecuteOnEvent);
71+
}
72+
```
73+
74+
And the other is through the use of operators
75+
76+
```c#
77+
private void SubscribeToEvent()
78+
{
79+
onFooEvent += ExecuteOnEvent;
80+
}
81+
82+
private void ExecuteOnEvent()
83+
{
84+
[...]
85+
}
86+
87+
private void UnsubscribeFromEvent()
88+
{
89+
onFooEvent -= ExecuteOnEvent;
90+
}
91+
```
92+
93+
### Invocation
94+
95+
Invoking the event is the same as you would invoke a regular event
96+
97+
```c#
98+
private SafeEvent onFooEvent;
99+
100+
[...]
101+
102+
private void InvokeEvent()
103+
{
104+
onFooEvent.Invoke();
105+
}
106+
```
107+
108+
```c#
109+
private SafeEvent<int> onFooEvent;
110+
111+
[...]
112+
113+
private void InvokeEvent()
114+
{
115+
onFooEvent.Invoke(123);
116+
}
117+
```
118+
119+
```c#
120+
private SafeEvent<int, string> onFooEvent;
121+
122+
[...]
123+
124+
private void InvokeEvent()
125+
{
126+
onFooEvent.Invoke(123, "abcde");
127+
}
128+
```
129+
130+
131+
## Support
132+
**Safe Event** is a small and open-source utility that I hope helps other people. It is by no means necessary but if you feel generous you can support me by donating.
133+
134+
[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/J3J11GEYY)
135+
136+
## Contributions
137+
Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.
138+

README.md.meta

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

Runtime.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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
3+
namespace TNRD.Events
4+
{
5+
public class DuplicateSubscriptionException : Exception
6+
{
7+
}
8+
}

Runtime/DuplicateSubscriptionException.cs.meta

Lines changed: 3 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)