This is a scriptable object architecture followed by Ryan Hipple Talk.
This package is made from Unity 2020.3.0f1
To create scriptable object right click in Project Window => Create => SO-Architecture
Add a script named as GameEventListener.cs
// Event to Listen
public GameEvent Event;
// public Unity Event, which to be happen when event is raised
public UnityEvent<object[]> Response;
Go to the Menu Item => SO-Architecture => New Variable Type
Now you will see a window open
In this picture you see, assign a new variable type that can be variable type i.e. float
, bool
, int
, LayerMask
or any custom struct or enum.
These given type are already created using this window
- Bool
- Int
- Float
- Vector2
- Vector3
- GameObject
- Transform
- Quaternion
In your class simply implement the interface IGenericCallback.cs
, and write you functionality in the generated function.
You can parse variable from object to any type. If you don't know about parsing data follow this link.
In this example you can see i am handling the callback of integer, and parsing the variable.
using UnityEngine;
public class IntegerCallback : MonoBehaviour, IGenericCallback
{
/// <summary>
/// Event Callback
/// </summary>
/// <param name="param">parameter fetch</param>
public void OnEventRaisedCallback(params object[] param)
{
int value = (int) param[0];
Debug.Log(value);
}
}
In your class implement the interface IRaise.cs
, and give the type of event you are going to raise.
In this example you can see i am raising the integer event. You can implement multiple interface for multiple type raising, or you can raise event manually.
using UnityEngine;
public class RaiseInteger : MonoBehaviour, IRaise<int>
{
/// <summary>
/// Event to raise
/// </summary>
public GameEvent _gameEvent;
/// <summary>
/// Raise integer event base on given value
/// </summary>
/// <param name="value">value to pass</param>
public void Raise(int value)
{
_gameEvent.Raise(value);
}
}
In your class create a variable type of Generic Reference, and pass the type of variable you want to create. You can see the variable in the inspector.
using UnityEngine;
public class FetchInteger : MonoBehaviour
{
/// <summary>
/// Variable of integer type
/// </summary>
public GenericReference<int> _variable;
/// <summary>
/// Fetch Variable
/// </summary>
public void FetchVariable()
{
Debug.Log(_variable.Value);
}
}