Easy bindings for events from Unity uGui to ECS framework - main goal of this extension.
Tested on unity 2018.3 (dependent on Unity engine) and contains assembly definition for compiling to separate assembly file for performance reason.
Dependent on ECS framework - ECS framework should be imported to unity project first.
Ecs run-system that generates entities with events data to ecs world
. Should be placed on root GameObject of Ui hierarchy in scene and connected in ecs world
before any systems that should process events from ui:
public class Startup : MonoBehaviour {
// Field that should be initialized by instance of `EcsUiEmitter` assigned to Ui root GameObject.
[SerializeField]
EcsUiEmitter _uiEmitter;
EcsSystems _systems;
void Start () {
var world = new EcsWorld ();
_systems = new EcsSystems(world)
.Add (_uiEmitter);
// Additional initialization here...
_systems.Initialize ();
}
}
MonoBehaviour components that should be added to uGui widgets to transfer events from them to ecs-world
(EcsUiClickAction
, EcsUiDragAction
and others). Each action component contains reference to EcsUiEmitter
in scene (if not inited - will try to find emitter automatically) and logical name WidgetName
that can helps to detect source of event inside ecs-system.
Event data containers: EcsUiClickEvent
, EcsUiBeginDragEvent
, EcsUiEndDragEvent
and others - they can be used as ecs-components with standard filtering through EcsFilter
:
[EcsInject]
public class TestUiClickEventSystem : IEcsRunSystem {
EcsWorld _world = null;
EcsFilter<EcsUiClickEvent> _clickEvents = null;
void IEcsRunSystem.Run () {
for (var i = 0; i < _clickEvents.EntitiesCount; i++) {
EcsUiClickEvent data = _clickEvents.Components1[i];
Debug.Log ("Im clicked!", data.Sender);
}
}
}
public class Startup : MonoBehaviour {
// Field that should be initialized by instance of `EcsUiEmitter` assigned to Ui root GameObject.
[SerializeField]
EcsUiEmitter _uiEmitter;
EcsSystems _systems;
IEnumerator Start () {
// For correct registering named widgets at EcsUiEmitter.
yield return null;
var world = new EcsWorld ();
_systems = new EcsSystems(world);
_systems
.Add (_uiEmitter)
// Additional systems here...
.Initialize ();
}
void Update () {
if (_systems != null) {
// Process systems.
_systems.Run();
// Important: automatic clearing one-frame components (ui-events).
_world.RemoveOneFrameComponents ();
}
}
void OnDisable () {
_systems.Dispose ();
_systems = null;
_world.Dispose ();
_world = null;
}
}
The software released under the terms of the MIT license. Enjoy.
Its free opensource software, but you can buy me a coffee: