https://github.com/favoyang/unity-action-sender
A type-safe replacement to SendMessage.
https://github.com/favoyang/unity-action-sender
messaging openupm unity upm
Last synced: 9 months ago
JSON representation
A type-safe replacement to SendMessage.
- Host: GitHub
- URL: https://github.com/favoyang/unity-action-sender
- Owner: favoyang
- License: mit
- Created: 2020-04-19T03:58:03.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-09-10T17:08:29.000Z (almost 4 years ago)
- Last Synced: 2025-04-10T22:34:59.180Z (about 1 year ago)
- Topics: messaging, openupm, unity, upm
- Language: C#
- Homepage:
- Size: 185 KB
- Stars: 16
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
Unity Action Sender
[](https://openupm.com/packages/com.littlebigfun.action-sender/)
A type-safe replacement to SendMessage.
SendMessage is a known trap for hard to maintain. An alternative way is leveraging interfaces with `GetComponents`. This package consists of a group of extension API to simplify the approach.
Notice that [Unity EventSystems](https://docs.unity3d.com/2019.1/Documentation/ScriptReference/EventSystems.ExecuteEvents.Execute.html) uses the same approach to support custom message. The major difference is that Unity EventSystems is designed for the UI system, therefore it takes extra efforts to support consume, use, or reset an event (like clicks). While this package API is more accessible and similar to the SendMessage calls. See more discussion in [#1](https://github.com/favoyang/unity-action-sender/issues/1).
## How to Use
Define an interface for your callbacks.
```
public interface IDamageable
{
void Damage(float amount);
bool IsAlive();
}
```
Implements the interface on your component.
```
public class Unit : MonoBehaviour, IDamageable
{
public void Damage(float amount)
{
// take damage.
}
public bool IsAlive()
{
// return is alive?
}
}
```
Import the namespace to send actions.
```
using LittleBigFun.ActionSender;
```
Replace SendMessage with:
```
gameObject.SendAction(t => t.Damage(1.0f));
```
Replace BroadcastMessage with:
```
gameObject.BroadcastAction(t => t.Damage(1.0f));
```
Replace SendMessageUpwards with:
```
gameObject.SendActionUpwards(t => t.Damage(1.0f));
```
To get result value of actions:
```
var results = gameObject.SendAction(t => t.IsAlive());
var results = gameObject.BroadcastAction(t => t.IsAlive());
var results = gameObject.SendActionUpwards(t => t.IsAlive());
```
## Install Package
The package is available on the [openupm registry](https://openupm.com). It's recommended to install it via [openupm-cli](https://github.com/openupm/openupm-cli).
```
openupm add com.littlebigfun.action-sender
```
Or you can install via [Git URL](https://docs.unity3d.com/Manual/upm-ui-giturl.html).
## Media
Icons made by [Freepik](https://www.flaticon.com/authors/freepik) from [flaticon.com](http://www.flaticon.com)
----
License: see LICENSE.md