Skip to content

ricardolee/Unity3D_ToolKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Event Dispatcher

Very effective way to manager game event. Like take damage and shoot etc..

  • Trigger Event simple
  • Configure Event Listener simple
  • Support config by Attribute
  • Support filter if you want modify event args.

Usage

Few simple step

Attach a EventDispatcher to GameObject

EventDispatcher events;
void Start()
{
    events = GetComponent<EventDispatcher>()
}

Declare a EventListener

[EventListener(name = "DAMAGE")]
void DamageHealth(int dmg)
{
    health -= dmg;
}

Trigger the Event

events.Trigger("DAMAGE", 10);

Optional

Declare a Filter

[EventFilter(name = "DAMAGE")]
void DoubleDamage(object[] objs)
{
    objs[0] = (int)objs[0] * 2;
}

you can return a bool. Break call chain if you return false.

State Manager

Same manager your game object state.

  • Change state simple, use enum
  • Support multi-state, like “GameState” “Health” etc
  • Support config state event by Attribute
  • Support inject state machine by Attribute
  • Config unity behaviour callback easy, Like Update, FixUpdate

Usage

Require a StateManager to your class it will auto add to your gameobject.

[RequireComponent(typeof(StateManager))]
class YourBehaviour : Monobehaviour

Declare a enum

public enum GameState {
    Play, Success, Failure
}

Declare a StateMachine with [StateMachinInject]

[StateMachineInject]
public StateMachine<GameState> _gameStateMachie;

Init && Change State

_gameStateMachine.Init(Play);
_gameStateMachine.Change(GameState.Success);

GetCurrentState

stateManager.CurrentState; // if not init, it will return null

Config StateEventCallback

[StateEvent(state = GameObject.Play, on = StateEvent.Enter)] // The Enter support coroutine
void Play() {

}
 Proxy MonoBehaviour event Callback
[StateListener(state = "GameState", when = "Play", on = "Update")]
void PlayUpdate()
{
   // Do Update in Play;
}

void Update() {
    stateManager.Update;
}

When yout change “GameState” to Success or Play, it will auto proxy to the true Update callback.

Get a StateMachine Manual

StateMachine sm = stateManager.GetStateMachine<GameState>();

About

Toolkit for Unity3D (C#)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published