Skip to main content

02 Basic Types: Passing Data with Events

๐Ÿ“ Demo Infoโ€‹

  • Scene Preview

    Demo 02 Scene View

  • Scene Path

    Assets/TinyGiants/GameEventSystem/Demo/02_BasicTypesEvent/02_BasicTypesEvent.unity
    Goal

    To demonstrate the Generic Event System, which allows passing standard C# and Unity types (String, Vector3, GameObject, Material) as arguments without writing custom event classes.


๐Ÿ“ Descriptionโ€‹

Beyond Void Events While void events are great for simple signals, most games need to pass data: "How much damage?", "Which item?", "Where to spawn?".

This demo showcases 4 different data types being passed dynamically:

  1. String: Sends text to update a UI label.
  2. Vector3: Sends coordinates to move a 3D object.
  3. GameObject: Sends a Prefab reference to be spawned.
  4. Material: Sends a Material asset to change an object's color.

The Magic: You do not need to create separate scripts like StringGameEvent or Vector3GameEvent. The system generates concrete classes for you automatically.


๐Ÿ› ๏ธ Scene Setupโ€‹

The scene contains interactable elements for each data type:

  1. HoloDisplay (Text): Receives the String event.
  2. Floating Cube: Receives the Vector3 event (Movement) and Material event (Color).
  3. Spawn Point: Receives the GameObject event to spawn prefabs.
  4. UI Buttons: Four buttons at the bottom to trigger each specific event type.

๐ŸŽฎ How to Testโ€‹

  1. Enter Play Mode.
  2. Click "Raise (String)":
    • Observation: The holographic text updates with "Hello World [count]".
  3. Click "Raise (Vector3)":
    • Observation: The cube teleports to a random position.
  4. Click "Raise (GameObject)":
    • Observation: A random primitive (Cube/Sphere) is instantiated at the center.
  5. Click "Raise (Material)":
    • Observation: The cube changes color (Red/Green/Blue/Yellow).

๐Ÿ”‘ Key Configurationโ€‹

1. Event Definition (Editor)โ€‹

In the Game Event Editor, we have pre-created 4 events, each corresponding to a specific data type (String, Vector3, GameObject, Material).

Editor List View

2. Receiver Action Binding (Behavior)โ€‹

Each event is configured to call a specific method on the BasicTypesEventReceiver script.

  • OnString โ†’ binds to OnMessageReceived(string)
  • OnVector3 โ†’ binds to OnMoveReceived(Vector3)
  • Note: The system automatically enforces type safety, ensuring you can only bind methods with matching parameters.

3. Raiser Event Assignment (Inspector)โ€‹

Select the BasicTypesEventRaiser object. The script exposes 4 distinct slots. Notice how the Dropdown filters the listโ€”you can only assign a GameEvent<string> asset to the "Message Event" slot.

Raiser Inspector


๐Ÿ’ป Code Walkthroughโ€‹

1. The Sender (BasicTypesEventRaiser.cs)โ€‹

Notice how GameEvent<T> is used for different types.

public class BasicTypesEventRaiser : MonoBehaviour
{
// 1. String Event
[GameEventDropdown] public GameEvent<string> messageEvent;
public string messageToSend = "Hello World";

// 2. Vector3 Event
[GameEventDropdown] public GameEvent<Vector3> movementEvent;

public void RaiseString()
{
// Raises event with a dynamic string argument
messageEvent.Raise($"{messageToSend} [{_count++}]");
}

public void RaiseVector3()
{
// Calculates a random position and sends it
Vector3 randomPos = new Vector3(Random.Range(-2f, 2f), Random.Range(0f, 3f), 0);
movementEvent.Raise(randomPos);
}
}

2. The Receiver (BasicTypesEventReceiver.cs)โ€‹

The receiver methods must have matching parameters.

public class BasicTypesEventReceiver : MonoBehaviour
{
[SerializeField] private TextMeshPro logText;
[SerializeField] private Transform movingCube;

// Signature Match: void (string)
public void OnMessageReceived(string msg)
{
logText.text = $"Received: {msg}";
}

// Signature Match: void (Vector3)
public void OnMoveReceived(Vector3 pos)
{
movingCube.localPosition = pos;
}
}