I put together a little package for tracking the mouse DOTS style.
You can find it here.
https://bitbucket.org/DSuperCynicsUnited/dots_mouse_handling/src/master/
It’s designed for projects where the mouse is the primary mode of player control, like an RTS.
It maintains a singleton entity which captures useful mouse behavior that systems can query and respond to.
For instance here’s an example of a system to track a mouse drag.
using Unity.Entities;
public class MouseDrag : SystemBase
{
private bool _dragStarted;
protected override void OnUpdate()
{
Entities.WithAll<LeftMouseDownComponent, Tag_LeftMouseDownThisFrame>().WithNone<Tag_MouseOutsideScreen>()
.ForEach((in MouseData mouseData) =>
{
_dragStarted = true;
// Handle drag start.
}).Schedule();
Entities.WithAll<LeftMouseDownComponent>().WithNone<Tag_MouseOutsideScreen, Tag_LeftMouseDownThisFrame>()
.ForEach((in MouseData mouseData) =>
{
if(! _dragStarted)
continue;
// Handle drag update
}).Schedule();
Entities.WithAll<LeftMouseDownComponent, RightMouseDownComponent, RightMouseDownThisFrame>()
.WithNone(Tag_MouseOutsideScreen)
.ForEach((in MouseData mouseData) =>
{
if(! _dragStarted)
continue;
//Handle drag end.
_dragStarted = false;
}).Schedule();
}
}
Areas to improve :
I’m looking for good ways to manage when there are many different types of systems which use the MouseData in different contexts so that multiple systems don’t step on each other’s toes.