Search Unity

small mono/csharp script based driver for multiple mice support in unity

Discussion in 'Scripting' started by robindev, Sep 2, 2014.

  1. robindev

    robindev

    Joined:
    Sep 2, 2014
    Posts:
    8
    Hi All

    RawMouseDriver is a C# library that can be used by unity to access multiple mice independently. This can be useful for example for a 2 player split screen first person shooter, where each player controls its character with its own mouse. I based the code on the example found here: http://jstookey.com/arcade/rawmouse/ from Peter Brumblay (peter@tyrconsulting.com).

    I created it for myself to be able to use multiple mice in a unity project. I'm releasing this code and the code used from Peter Brumblay as a package in the hope that it can be useful for others as well.

    Instalation:
    copy the RawInputSharp.dll and RawMouseDriver.dll into the assets directory of Unity.
    Then you can reference them from the script like so:

    Code (CSharp):
    1.  
    2. using RawMouseDriver;
    3. using RawInputSharp;
    4.  
    5. public class example : MonoBehaviour
    6. {
    7.     private RawMouseDriver.RawMouseDriver mousedriver;
    8.     private RawMouse mouse1;
    9.     private RawMouse mouse2;
    10.  
    11.     private float moveY = 0.0f;
    12.     private float moveX = 0.0f;
    13.  
    14.     void Start ()
    15.     {
    16.         mousedriver = new RawMouseDriver.RawMouseDriver ();
    17.     }
    18.     void Update()
    19.     {
    20.         mousedriver.GetMouse (0, ref mouse1);
    21.         mousedriver.GetMouse (1, ref mouse1);
    22.      
    23.         moveY += mouse1.YDelta;
    24.         transform.Translate(Vector3.forward * moveY);
    25.      
    26.         moveX += mouse2.XDelta;
    27.         transform.Translate(Vector3.forward * moveX);
    28.     }
    29.     void OnApplicationQuit()
    30.     {
    31.         mousedriver.Dispose ();
    32.     }
    33. }
    34.  
    By using "GetMouse" with an index it is possible to access the data of the individual mice connected to the system, and retrieve the respective data. In the background the RawMouseDriver.dll will create a window that is used to capture the mouse data. I hid this window from view, but if you experience any sort of buggy behavior or need to troubleshoot, you can access this window by pressing alt-tab and selecting "Connected Mice". Here you see a list of detected mice, and when you select one, you can see the X and Y coordinates at the bottom that should update when you move the respective mouse.

    Known bug:
    When you launch an app using this code from the editor, it will give you a popup saying 'oops ...' twice, but in standalone this does not happen. If any of you know how to solve this, please let me know. I think it has to do with the moment the dialog gets created/updated, but I do not know how to solve it.

    Please feel free to contact me when questions arise.

    Best regards,

    Robin
     

    Attached Files:

    Last edited: Sep 2, 2014
    Sovogal and wccrawford like this.
  2. robindev

    robindev

    Joined:
    Sep 2, 2014
    Posts:
    8
    trying out some more things, I got the feeling the program probably will need to be made threadsafe(which it isn't at the moment)

    I attached a modified version of mouselook(taken from The Palace of Orinthalian asset in the asset store) that uses the separate mice, and can be used for a 2 player split screen FPS. Please also note that you need to set unity build settings to x86 architecture, and player settings for Api compatibility level to '.NET 2.0' instead of '.NET 2.0 subset' to make it work;
    example.jpg
     

    Attached Files:

  3. CaoMengde777

    CaoMengde777

    Joined:
    Nov 5, 2013
    Posts:
    813
    awesome!!! thanks alot!
     
  4. Merubokkusu

    Merubokkusu

    Joined:
    Aug 21, 2014
    Posts:
    1
    EDIT: I think I fixed it by doing this
    File -> Build Settings -> Player Settings -> Other Settings -> Change .NET 2.0 Subset to .NET 2.0


    I'm getting this error.

    Code (csharp):
    1. Internal compiler error. See the console log for more information. output was:
    2. Unhandled Exception: System.TypeLoadException: Could not load type 'RawMouseDriver.RawMouseDriver' from assembly 'RawMouseDriver, Version=1.0.5358.22891, Culture=neutral, PublicKeyToken=null'.
    3.  
    4.   at (wrapper managed-to-native) System.MonoType:GetMethodsByName (string,System.Reflection.BindingFlags,bool,System.Type)
    5.  
    6.   at System.MonoType.GetMethods (BindingFlags bindingAttr) [0x00000] in <filename unknown>:0
    7.  
    8.   at Mono.CSharp.MemberCache.AddMethods (BindingFlags bf, System.Type type) [0x00000] in <filename unknown>:0
    9.  
    10.   at Mono.CSharp.MemberCache.AddMethods (System.Type type) [0x00000] in <filename unknown>:0
    11.  
    12.   at Mono.CSharp.MemberCache..ctor (IMemberContainer container) [0x00000] in <filename unknown>:0
    13.  
    14.   at Mono.CSharp.TypeHandle..ctor (System.Type type) [0x00000] in <filename unknown>:0
    15.  
    16.   at Mono.CSharp.TypeHandle.GetTypeHandle (System.Type t) [0x00000] in <filename unknown>:0
    17.  
    18.   at Mono.CSharp.TypeHandle.GetMemberCache (System.Type t) [0x00000] in <filename unknown>:0
    19.  
    20.   at Mono.CSharp.TypeManager.MemberLookup_FindMembers (System.Type t, MemberTypes mt, BindingFlags bf, System.String name, System.Boolean& used_cache) [0x00000] in <filename unknown>:0
    21.  
    22.   at Mono.CSharp.TypeManager.RealMemberLookup (System.Type invocation_type, System.Type qualifier_type, System.Type queried_type, MemberTypes mt, BindingFlags original_bf, System.String name, IList almost_match) [0x00000] in <filename unknown>:0
    23.  
    24.   at Mono.CSharp.TypeManager.MemberLookup (System.Type invocation_type, System.Type qualifier_type, System.Type queried_type, MemberTypes mt, BindingFlags original_bf, System.String name, IList almost_match) [0x00000] in <filename unknown>:0
    25.  
    26.   at Mono.CSharp.Expression.MemberLookup (Mono.CSharp.CompilerContext ctx, System.Type container_type, System.Type qualifier_type, System.Type queried_type, System.String name, MemberTypes mt, BindingFlags bf, Location loc) [0x00000] in <filename unknown>:0
    27.  
    28.   at Mono.CSharp.Expression.MemberLookupFinal (Mono.CSharp.ResolveContext ec, System.Type qualifier_type, System.Type queried_type, System.String name, MemberTypes mt, BindingFlags bf, Location loc) [0x00000] in <filename unknown>:0
    29.  
    30.   at Mono.CSharp.New.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
    31.  
    32.   at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags) [0x00000] in <filename unknown>:0
    33.  
    34.   at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
    35.  
    36.   at Mono.CSharp.Assign.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
    37.  
    38.   at Mono.CSharp.SimpleAssign.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
    39.  
    40.   at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags) [0x00000] in <filename unknown>:0
    41.  
    42.   at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0
    43.  
    44.   at Mono.CSharp.ExpressionStatement.ResolveStatement (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0
    45.  
    46.   at Mono.CSharp.StatementExpression.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0
    47.  
    48.   at Mono.CSharp.Block.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0
    49.  
    50.   at Mono.CSharp.ToplevelBlock.Resolve (Mono.CSharp.FlowBranching parent, Mono.CSharp.BlockContext rc, Mono.CSharp.ParametersCompiled ip, IMethodData md) [0x00000] in <filename unknown>:0
    51.  
     
    Last edited: Sep 25, 2014
  5. robindev

    robindev

    Joined:
    Sep 2, 2014
    Posts:
    8
    Yes, exactly! I wrote it in the readme, but its good to also write it here :)
     
    Merubokkusu likes this.
  6. chrisMary

    chrisMary

    Joined:
    Nov 27, 2013
    Posts:
    16
    Hello ! Great Work !! Thank you !
    Do you happen to know if it works on Linux ?
     
    Last edited: Oct 26, 2014
  7. jordi-adh

    jordi-adh

    Joined:
    Jul 28, 2015
    Posts:
    2
    i get the error:
     
  8. chrisMary

    chrisMary

    Joined:
    Nov 27, 2013
    Posts:
    16
    In your player settings, you should set the framework to .Net 2.0 instead of .Net subset

    Oops, didn't see there was an answer for that before ^^
     
  9. ENAYSOFT

    ENAYSOFT

    Joined:
    Aug 22, 2012
    Posts:
    13
    Hello,

    Not sure if this driver still works in Unity5, but having some issues getting it to work.
    I've set my setting to .NET 2.0 and it compiles, and I can debug that two mice exist when I plug them both in, but moving the mouse and testing for buttons using mouse1.Buttons etc, but nothing seems to occur.
    i've included the two files in my project, but I just don't get any input, any tips on what I might be doing wrong?

    Also, pretty sure this is a typo, but surely it should be:-

    mousedriver.GetMouse (1, ref mouse2);

    [edit]

    Ok, I figured out what the problem is, 64 bit! Works in 32 bit but not in 64 bit.
    That's a shame... As this was exactly what I was looking for.
     
    Last edited: Nov 16, 2015
  10. alex0817

    alex0817

    Joined:
    Apr 12, 2019
    Posts:
    2
    Hey!!
    Any chance you still working on these dlls ? is there a 64bit version?