Search Unity

Tracking the mouse DOTS Style

Discussion in 'Entity Component System' started by DanSuperGP, Jul 14, 2020.

  1. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    408
    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.

    Code (CSharp):
    1. using Unity.Entities;
    2.  
    3. public class MouseDrag : SystemBase
    4. {
    5.     private bool _dragStarted;
    6.  
    7.     protected override void OnUpdate()
    8.     {
    9.         Entities.WithAll<LeftMouseDownComponent, Tag_LeftMouseDownThisFrame>().WithNone<Tag_MouseOutsideScreen>()
    10.             .ForEach((in MouseData mouseData) =>
    11.             {
    12.                 _dragStarted = true;
    13.  
    14.                 // Handle drag start.
    15.  
    16.             }).Schedule();
    17.  
    18.         Entities.WithAll<LeftMouseDownComponent>().WithNone<Tag_MouseOutsideScreen, Tag_LeftMouseDownThisFrame>()
    19.             .ForEach((in MouseData mouseData) =>
    20.             {
    21.                 if(! _dragStarted)
    22.                     continue;
    23.  
    24.                 // Handle drag update
    25.  
    26.             }).Schedule();
    27.  
    28.         Entities.WithAll<LeftMouseDownComponent, RightMouseDownComponent, RightMouseDownThisFrame>()
    29.             .WithNone(Tag_MouseOutsideScreen)
    30.             .ForEach((in MouseData mouseData) =>
    31.             {
    32.                 if(! _dragStarted)
    33.                     continue;
    34.  
    35.                 //Handle drag end.
    36.  
    37.                 _dragStarted = false;
    38.             }).Schedule();
    39.     }
    40. }
    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.
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    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.
     
  3. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    408
    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.
     
    exiguous likes this.
  4. ScriptsEngineer

    ScriptsEngineer

    Joined:
    Jun 8, 2018
    Posts:
    37
    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.
     
    DanSuperGP likes this.
  5. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Thanks Daniel. That is good news.
     
  6. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    408
    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.
     
    ScriptsEngineer likes this.
  7. BitPax

    BitPax

    Joined:
    Oct 4, 2019
    Posts:
    89
    Instead of
    Code (CSharp):
    1. .Schedule()
    shouldn't it be
    Code (CSharp):
    1. .Run()
    since it's input/output so it runs on the main thread and there's no delay?
     
    Mockarutan likes this.
  8. Gwaar

    Gwaar

    Joined:
    Apr 30, 2020
    Posts:
    1
    hey, this is super useful! Thanks a bunch :)