https://github.com/Siccity/SerializableCallback
UnityEvent and System.Func had a child
https://github.com/Siccity/SerializableCallback
callback serializable unity
Last synced: 6 months ago
JSON representation
UnityEvent and System.Func had a child
- Host: GitHub
- URL: https://github.com/Siccity/SerializableCallback
- Owner: Siccity
- License: mit
- Created: 2017-12-22T10:15:22.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-04-03T18:18:19.000Z (over 1 year ago)
- Last Synced: 2024-11-09T18:03:01.608Z (about 1 year ago)
- Topics: callback, serializable, unity
- Language: C#
- Homepage:
- Size: 44.9 KB
- Stars: 362
- Watchers: 11
- Forks: 53
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-unity3d - SerializableCallback - UnityEvent and System.Func had a child (Open Source Repositories / Utilities)
- awesome-unity3d - SerializableCallback - UnityEvent and System.Func had a child (Open Source Repositories / Utilities)
- awesome-opensource-unity - SerializableCallback - UnityEvent and System.Func had a child. (Open Source Packages / Utilities)
README
### SerializableCallback
Lets you drag-and-drop methods with or without return values / parameters in the Unity inspector.
It uses expression trees and reflection to cache a delegate on first execution.
Usage is identical to UnityEvent

```csharp
public class MyClass : MonoBehaviour {
//These fields are shown in the inspector
public SerializableCallback callback; // supports all non-void return types
public Condition condition; // supports bool return types only
public GetProduct getProduct; // supports MyProduct return types only
void Start() {
// Callbacks can be invoked with or without parameters, and with different types
Debug.Log(callback.Invoke()); // returns object
Debug.Log(condition.Invoke()); // returns bool
Debug.Log(getProduct.Invoke(2)); // returns MyProduct
}
// As with UnityEvents, custom callbacks must have a non-generic wrapper class marked as [Serializable] in order to be serialized by Unity
[Serializable]
public class Condition : SerializableCallback {}
// Last generic type parameter is the return type, staying consistent with System.Func
[Serializable]
public class GetProduct : SerializableCallback {}
}
```
| Performance (100000 iterations) | Time |
| -------------------------------------------- | --------- |
| bool Method(float) | 00.00304s |
| SerializedCallback (Persistent) | 00.01026s |
| SerializedCallback (Dynamic) | 00.00797s |
### Installing with Unity Package Manager
To install this project as a dependency using the Unity Package Manager,
add the following line to your project's `manifest.json`:
```
"com.github.siccity.serializablecallback": "git+https://github.com/Siccity/SerializableCallback.git"
```
Join the [Discord](https://discord.gg/qgPrHv4 "Join Discord server") server to leave feedback or get support.