Tracking the mouse DOTS Style

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.

3 Likes

Hey Daniel,
I would like to use your code. Unfortunately it lacks proper licensing information (or at least I have not found it). I would apreciate if you could add a license so it is “safe” to use for other people. I know this legal stuff is nasty and annoying. And I’m sorry to brung this up. But without license there is little use to make it available to others in the first place.
Anyway. Many thanks for your efforts.

Hey, I had put a license file in the repo… I guess I never pushed it up to Bitbucket.

It’s been pushed now. Creative Commons Zero, you should be good to go.

1 Like

I will open a discussion that may not have a perfect solution, but I believe that instead of having an entity with mouse input component, but rather a component to each entity that I want to have input with mouseInput could avoid structural changes and also have a cleaning system.

1 Like

Thanks Daniel. That is good news.

You might very well be right. I haven’t had much opportunity to test it unfortunately since I got a new job not long after I wrote this and have been super busy.

1 Like

Instead of .Schedule() shouldn’t it be .Run() since it’s input/output so it runs on the main thread and there’s no delay?

1 Like

hey, this is super useful! Thanks a bunch :slight_smile: