Git Product home page Git Product logo

eventchannelunit's Introduction

Event Channel Unit

GitHub Actions Workflow Status openupm GitHub Release

Overview

This Package is designed to integrate the EventChannel pattern into Unity's visual scripting environment.

By using the Event Channel Unit, you can easily create, manage, and trigger events through visual scripting. This makes your game or application architecture cleaner, more modular, and easier to maintain.

Features

  • EventChannel Pattern: Implements the EventChannel pattern to decouple event publishers and subscribers, enhancing code modularity and reusability.
  • Visual Scripting Support: Seamlessly integrates with Unity's visual scripting tools, allowing for drag-and-drop event management.

UI

Visual Scripting Unit

vsunits

By default, EventUnits are implemented for the following types of EventChannels.

  • void
  • bool
  • int
  • float
  • string
  • Vector2
  • Vector3

Additionally, you can trigger an event by calling RaiseEvent() for each EventChannel.

Inspector

inspector

When you select an EventChannel asset, these features are available in the inspector to facilitate development:

  • Object Listeners: Objects currently subscribing to the selected EventChannel asset's event.
  • Unit Listeners: Visual Scripting Units currently subscribing to the selected EventChannel asset's event.
  • Raise Event: You can publish an event with the Test Value.
  • Log: You can view the log of events published since this asset was opened in the inspector.

Example Usage

Visual Scripting

Transition with EventUnit

ex1

In this example, we subscribe to an event from VoidChannel and transition without arguments, but you can also control the transition by branching conditions based on arguments.

Raize Event with State Graph

ex2

In this example, we raise BoolChannel events at the Enter and Exit of the state. These events control the On/Off state of a panel displayed during a specific state.

At the design stage of the State Management, you only need to think about abstract things like what to display, and you can implement how to actually fade in or fade out later.

GameObject

Spawn GameObject with IntChannel

using EventChannelUnit.Runtime;
using UnityEngine;
public class Spawner : MonoBehaviour
{
    [SerializeField] private IntChannel spawnChannel;
    [SerializeField] private GameObject itemPrefab;
    
    private void OnEnable()
    {
        spawnChannel.OnEventRaised += Spawn;
    }
    private void OnDisable()
    {
        spawnChannel.OnEventRaised -= Spawn;
    }
    private void Spawn(int count)
    {
        for (int i = 0; i < count; i++)
        {
            var item = Instantiate(itemPrefab, transform);
            item.transform.localPosition = new ()
            {
                x = Random.Range(-1f, 1f),
                y = Random.Range(-1f, 1f),
                z = Random.Range(-1f, 1f)
            };
            item.transform.rotation = Random.rotation;
            _items.Add(item);
        }
    }
}

In this Spawner example, GameObjects are instantiated at random positions based on the number of arguments in an IntChannel event.

Toggle CanvasGroup Visibility with BoolChannel

using EventChannelUnit.Runtime;
using UnityEngine;

[RequireComponent(typeof(CanvasGroup))]
public class CanvasGroupEventListener : MonoBehaviour
{
    [SerializeField] private BoolChannel visibilityChannel;
    [SerializeField] private bool defaultIsActive = false;
    private CanvasGroup _canvasGroup;
    private void Awake()
    {
        _canvasGroup = GetComponent<CanvasGroup>();
        SetVisibility(defaultIsActive);
    }
    private void OnEnable()
    {
        visibilityChannel.OnEventRaised += SetVisibility;
    }

    private void OnDisable()
    {
        visibilityChannel.OnEventRaised -= SetVisibility;
    }

    private void SetVisibility(bool show)
    {
        _canvasGroup.alpha = show ? 1f : 0f;
        _canvasGroup.blocksRaycasts = show;
        _canvasGroup.interactable = show;
    }
}

In this example, we assume a UI panel with a CanvasGroup, where visibility can be toggled based on the arguments of a BoolChannel event. Additionally, fading effects can be achieved by using other Tween libraries with SetVisibility.

eventchannelunit's People

Contributors

jnphgs avatar

Watchers

 avatar

eventchannelunit's Issues

Missing AssemblyDefinition File in Visual Scripting's Node Library

Problem Details

The AssemblyDefinition file is not added to the Node Library in ProjectSettings for Visual Scripting. As a result, the Channel class cannot be accessed from Visual Scripting.

Expected Behavior

It is expected that the necessary AssemblyDefinition file is added to the Node Library, allowing access to each Channel class from Visual Scripting Editor.

Reproduction Steps

  1. Import EventChannelUnit Package.
  2. Open ProjectSettings
  3. Navigate to the Visual Scripting settings
  4. Check the Node Library configuration

Issues with package addition and suggestions for improvement

Problem Details

  • Assembly Not Added to Project Settings: When a package is added while Visual Scripting is not initialized, the assembly does not get added to the Project Settings.
  • Package Addition Halts Process: Sometimes, the process stops when adding a package.
  • Editor Hangs on Startup: When adding a package to the manifest.json and launching the editor, the process occasionally halts.

Suggested Improvement

I propose displaying the editor window during package import to allow for the initialization of Visual Scripting, addition of the assembly, and rebuilding of the Node Library at any desired moment. I believe this could resolve the issues mentioned above.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.