Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Free MouseController Package

Discussion in 'General Discussion' started by PolymorphiK Games, Oct 18, 2014.

?

Should I add this to the Asset Store Free of charge?

Poll closed Oct 25, 2014.
  1. Yes, add it.

    100.0%
  2. No, there is no need.

    0 vote(s)
    0.0%
  3. Meh.

    0 vote(s)
    0.0%
  1. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    Hello,

    I ran into a problem today where for my game I wanted to set the player's mouse position to the center of the screen. Unity does not offer this off the bat, so I had to create my own solution. Anyways, to anyone that may need this for their game, I created a very simple DLL to allow cross-platform for Mac, Windows and Linux (Not tested but should work) mouse position setting.

    Let me know how it works for you developers and I can place it on the Unity Asset Store free of charge and add to it as people request features for it.

    Grab: v1.0.0 Mouse Controller
    Grab: v1.0.1 Mouse Controller
     
    Last edited: Oct 19, 2014
  2. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789
    Why a DLL? Then you cannot alter it to suit your games specifics. Release the scripts themselves.
     
  3. Myhijim

    Myhijim

    Joined:
    Jun 15, 2012
    Posts:
    1,148
    What about lockCursor?

    Or am I missing the point?

    Code (CSharp):
    1. Screen.lockCursor = true;
     
    randomperson42 likes this.
  4. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    Looks pretty useful.
    His plugin allows to set specific x y position. LockCursor sets position to center of the screen only.
     
  5. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    I had to make it a DLL to have it be cross platform and make the set up easier. In this case, all you have to do is add the package and done. Or else, you will have to add Windows and Drawing manually to Mono and not everyone on Mac has VS. it was meant as a convenience.
     
    Last edited: Oct 18, 2014
  6. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    Here is the code that is in the DLL, its just a single program file. Its really easy, I am really surprised Unity does not have this built in their UnityEngine.Input class.

    Anyways, again if anyone would like a feature for the MouseController I can add whatever feature people would like.

    Code (CSharp):
    1.  
    2. //----------------------------------------------
    3. // Created by PolymorphiK Games
    4. //----------------------------------------------
    5.  
    6. using System;
    7.  
    8. namespace MouseController {
    9.     /// <summary>
    10.     /// GlobalMouse allows you to modifiy the
    11.     /// desktop curser's position etc.
    12.     /// </summary>
    13.     public static class GlobalMouse : System.Object {
    14.         private static int cachedX = 0;
    15.         private static int cachedY = 0;
    16.         private static bool isVisable = true;
    17.  
    18.         /// <summary>
    19.         /// Sets the curser at the passed integer values
    20.         /// for X and Y.
    21.         /// </summary>
    22.         /// <param name="xPosition"></param>
    23.         /// <param name="yPosition"></param>
    24.         public static void SetMousePosition(int xPosition = 0, int yPosition = 0) {
    25.             xPosition = Math.Abs(xPosition);
    26.             yPosition = Math.Abs(yPosition);
    27.  
    28.             System.Windows.Forms.Cursor.Position = new System.Drawing.Point(xPosition, yPosition);
    29.         }
    30.  
    31.         /// <summary>
    32.         /// Sets the visabilty of the mouse.
    33.         /// </summary>
    34.         /// <param name="isVisable">True, to make the mouse cursor visable in the window. False to hide the mouse cursor</param>
    35.         public static void SetMouseVisability(bool isVisable = true) {
    36.             if (isVisable == true && GlobalMouse.IsVisable == false) {
    37.                 System.Windows.Forms.Cursor.Show();
    38.                 GlobalMouse.isVisable = true;
    39.                 return;
    40.             }
    41.             if(isVisable == false && GlobalMouse.IsVisable == true) {
    42.                 GlobalMouse.isVisable = false;
    43.                 System.Windows.Forms.Cursor.Hide();
    44.                 return;
    45.             }
    46.         }
    47.  
    48.         /// <summary>
    49.         /// Caches the mouse position.
    50.         /// </summary>
    51.         /// <param name="xPosition">If left at -1 it will store the current mouse position on the X.</param>
    52.         /// <param name="yPosition">If left at -1 it will store the current mouse position on the Y.</param>
    53.         public static void CacheMousePosition(int xPosition = -1, int yPosition = -1) {
    54.             GlobalMouse.cachedX = (xPosition == -1) ? System.Windows.Forms.Cursor.Position.X : xPosition;
    55.             GlobalMouse.cachedY = (yPosition == -1) ? System.Windows.Forms.Cursor.Position.Y : yPosition;
    56.         }
    57.  
    58.         /// <summary>
    59.         /// Resets the cached X and Y values to 0.
    60.         /// </summary>
    61.         public static void DumpCacheMousePosition() {
    62.             GlobalMouse.cachedX = 0;
    63.             GlobalMouse.cachedY = 0;
    64.         }
    65.  
    66.         /// <summary>
    67.         /// Returns the X cache position of the Mouse.
    68.         /// </summary>
    69.         public static int XPosition {
    70.             get {
    71.                 return GlobalMouse.cachedX;
    72.             }
    73.         }
    74.  
    75.         /// <summary>
    76.         /// Returns the Y cache position of the Mouse.
    77.         /// </summary>
    78.         public static int YPosition {
    79.             get {
    80.                 return GlobalMouse.cachedY;
    81.             }
    82.         }
    83.  
    84.         /// <summary>
    85.         /// Returns true if the mouse is visable,
    86.         /// return false if the mouse is not visable.
    87.         /// </summary>
    88.         public static bool IsVisable {
    89.             get {
    90.                 return GlobalMouse.isVisable;
    91.             }
    92.         }
    93.     }
    94. }
    95.  
     
    ippdev likes this.
  7. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789

    Oh..OK.. It seemed to me most folks release DLL's so others cannot see the code. Been showstopped by them prior if I wanted to do more than the DLL offered and still keep it's functionality.
     
  8. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    Yeah I have a Mac with Windows 8.1 on Bootcamp and I would not have been able to do this on the Mac without having to create a Framework and blah too much work. On Windows, I was able to achieve what I needed with 1 line of code. Plus I'd rather stick to just one language, in this case C#, rather than using C++ / Obj-C for Mac, and C# for Windows. With the package it achieves the results that I needed, but may not reflect what others may need, so if anyone wants something added, I can do that if its possible.

    From the poll so far, I might just end up adding this to the Asset Store for free. Seems like a useful small tool for Desktop games.
     
  9. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    v1.0.1 (See OP above) is now up on dropbox. I will post up the script code once I get back on my PC. I want to see what people think about the package so please let me know how it works.

    PS: First iteration on Dynamic mouse positioning from Window. (Little Difficult for Cross-Platform use)

    EDIT: DLL Code.
    Code (CSharp):
    1. //----------------------------------------------
    2. //            MouseController
    3. // Copyright © 2014 PolymorphiK Games
    4. //----------------------------------------------
    5. // If you add to the code, please let me know
    6. // so I can add the updates so everyone can benefit
    7. // from the changes.
    8. //----------------------------------------------
    9. // Programmer: Kevin Pacheco
    10. // Email: k.pacheco@me.com
    11. // Subject: MouseController Modifications.
    12. // Body: .zipped scripts with documentation.
    13. //----------------------------------------------
    14.  
    15. using System;
    16.  
    17. namespace MouseController {
    18.     /// <summary>
    19.     /// GlobalMouse allows you to modifiy the
    20.     /// desktop curser's position etc.
    21.     /// </summary>
    22.     public sealed class GlobalMouse : UnityEngine.MonoBehaviour {
    23.         private static GlobalMouse _instance = null;
    24.         private static bool interrupt;
    25.         private CursorLockProperties mouseProperties;
    26.         private int cachedX = 0;
    27.         private int cachedY = 0;
    28.         private bool isVisable = true;
    29.  
    30.         #region Internal
    31.         /// <summary>
    32.         /// Creates an instance of GlobalMouse.
    33.         /// </summary>
    34.         private static GlobalMouse SceneInstance {
    35.             get {
    36.                 if (GlobalMouse._instance == null) {
    37.                     UnityEngine.GameObject globalMouse = new UnityEngine.GameObject("_GlobalMouse");
    38.                     DontDestroyOnLoad(globalMouse);
    39.                     GlobalMouse._instance = globalMouse.AddComponent<GlobalMouse>();
    40.                     GlobalMouse._instance.mouseProperties = new CursorLockProperties();
    41.                 }
    42.  
    43.                 return GlobalMouse._instance;
    44.             }
    45.         }
    46.  
    47.         /// <summary>
    48.         /// Coroutine for keeping the mouse locked.
    49.         /// </summary>
    50.         /// <returns></returns>
    51.         private System.Collections.IEnumerator LockCursor() {
    52.             System.Drawing.Point lockedPoint = new System.Drawing.Point(GlobalMouse.SceneInstance.mouseProperties.LockedX, GlobalMouse.SceneInstance.mouseProperties.LockedY);
    53.  
    54.             while (true) {
    55.                 if (GlobalMouse.interrupt == true) {
    56.                     yield return new UnityEngine.WaitForFixedUpdate();
    57.                 }
    58.  
    59.                 System.Windows.Forms.Cursor.Position = lockedPoint;
    60.  
    61.                 yield return null;
    62.             }
    63.         }
    64.  
    65.         void OnApplicationPause(bool pause) {
    66.             if (pause == true) {
    67.                 GlobalMouse.interrupt = true;
    68.                 if (GlobalMouse.SceneInstance.mouseProperties.Seal == false) {
    69.                     GlobalMouse.SceneInstance.StopCoroutine("LockCursor");
    70.                 }
    71.             } else {
    72.                 GlobalMouse.interrupt = false;
    73.                 if (GlobalMouse.SceneInstance.mouseProperties.Seal == false) {
    74.                     GlobalMouse.SceneInstance.StartCoroutine("LockCursor");
    75.                 }
    76.             }
    77.         }
    78.  
    79.         /// <summary>
    80.         /// Finds and returns the IntPtr for the Window process based
    81.         /// on the windowTile (in this case the product name from PlayerSettings.productName).
    82.         /// </summary>
    83.         /// <param name="windowTitle"></param>
    84.         /// <returns></returns>
    85.         private static System.IntPtr GetWindowWithName(string windowTitle) {
    86.             System.IntPtr window = System.IntPtr.Zero;
    87.  
    88.             foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcesses()) {
    89.                 if (process.MainWindowTitle.Contains(windowTitle)) {
    90.                     window = process.MainWindowHandle;
    91.                 }
    92.             }
    93.  
    94.             return window;
    95.         }
    96.         #endregion
    97.  
    98.         #region Properties
    99.         /// <summary>
    100.         /// CursorLockProperties: Values based on the
    101.         /// lock state of the mouse cursor.
    102.         /// </summary>
    103.         public static CursorLockProperties CursorLockProperties {
    104.             get {
    105.                 return GlobalMouse.SceneInstance.mouseProperties;
    106.             }
    107.         }
    108.  
    109.         /// <summary>
    110.         /// Returns the X cache position of the Mouse.
    111.         /// </summary>
    112.         public static int XPosition {
    113.             get {
    114.                 return GlobalMouse.SceneInstance.cachedX;
    115.             }
    116.         }
    117.  
    118.         /// <summary>
    119.         /// Returns the Y cache position of the Mouse.
    120.         /// </summary>
    121.         public static int YPosition {
    122.             get {
    123.                 return GlobalMouse.SceneInstance.cachedY;
    124.             }
    125.         }
    126.  
    127.         /// <summary>
    128.         /// Returns true if the mouse is visable,
    129.         /// return false if the mouse is not visable.
    130.         /// </summary>
    131.         public static bool IsVisable {
    132.             get {
    133.                 return GlobalMouse.SceneInstance.isVisable;
    134.             }
    135.         }
    136.         #endregion
    137.  
    138.         #region Methods
    139.         /// <summary>
    140.         /// Caches the mouse position.
    141.         /// </summary>
    142.         /// <param name="xPosition">If left at -1 it will store the current mouse position on the X.</param>
    143.         /// <param name="yPosition">If left at -1 it will store the current mouse position on the Y.</param>
    144.         public static void CacheMousePosition(int xPosition = -1, int yPosition = -1) {
    145.             GlobalMouse.SceneInstance.cachedX = (xPosition == -1) ? System.Windows.Forms.Cursor.Position.X : xPosition;
    146.             GlobalMouse.SceneInstance.cachedY = (yPosition == -1) ? System.Windows.Forms.Cursor.Position.Y : yPosition;
    147.         }
    148.  
    149.         /// <summary>
    150.         /// Resets the cached X and Y values to 0.
    151.         /// </summary>
    152.         public static void DumpCacheMousePosition() {
    153.             GlobalMouse.SceneInstance.cachedX = 0;
    154.             GlobalMouse.SceneInstance.cachedY = 0;
    155.         }
    156.  
    157.         /// <summary>
    158.         /// Locks the mouse to a certain part of the screen.
    159.         /// </summary>
    160.         /// <param name="seal">True: Keep curser locked even if the application is interupted. False: Lock and unlock when application is interupted</param>
    161.         /// <param name="xPosition">If XPosition is -1, it will lock at UnityEngine.Screen.Width / 2.0f</param>
    162.         /// <param name="yPosition">If YPosition is -1, it will lock at UnityEngine.Screen.Height / 2.0f</param>
    163.         public static void LockCurser(bool seal, int xPosition = -1, int yPosition = -1) {
    164.             xPosition = (xPosition == -1) ? (int)(UnityEngine.Screen.width / 2.0f) : xPosition;
    165.             yPosition = (yPosition == -1) ? (int)(UnityEngine.Screen.height / 2.0f) : yPosition;
    166.  
    167.             // UnityEngine.Debug.Log("C# Width: " + System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width);
    168.             // UnityEngine.Debug.Log("C# Height: " + System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
    169.             // UnityEngine.Debug.Log("Unity Width: " + UnityEngine.Screen.width);
    170.             // UnityEngine.Debug.Log("Unity Height: " + UnityEngine.Screen.height);
    171.        
    172.  
    173.             GlobalMouse.SceneInstance.StopCoroutine("LockCursor");
    174.             GlobalMouse.SceneInstance.mouseProperties = new CursorLockProperties(true, seal, xPosition, yPosition);
    175.             GlobalMouse.SceneInstance.StartCoroutine("LockCursor");
    176.         }
    177.  
    178.         /// <summary>
    179.         /// Removes the GlobalMouse GameObject from the scene.
    180.         /// </summary>
    181.         public static void RemoveFromScene() {
    182.             GlobalMouse.SceneInstance.StopCoroutine("LockCursor");
    183.             DestroyImmediate(GlobalMouse.SceneInstance.gameObject);
    184.         }
    185.  
    186.         /// <summary>
    187.         /// Unlocks the curser from its current locked state.
    188.         /// </summary>
    189.         public static void UnlockCurser() {
    190.             GlobalMouse.SceneInstance.StopCoroutine("LockCursor");
    191.             GlobalMouse.SceneInstance.mouseProperties = new CursorLockProperties();
    192.         }
    193.  
    194.         /// <summary>
    195.         /// Sets the curser at the passed UInt32 values
    196.         /// for X and Y.
    197.         /// </summary>
    198.         /// <param name="xPosition">X position for mouse position.</param>
    199.         /// <param name="yPosition">Y position for mouse position.</param>
    200.         public static void SetMousePosition(int xPosition = 0, int yPosition = 0) {
    201.             xPosition = Math.Abs(xPosition);
    202.             yPosition = Math.Abs(yPosition);
    203.  
    204.             System.Windows.Forms.Cursor.Position = new System.Drawing.Point(xPosition, yPosition);
    205.         }
    206.  
    207.         /// <summary>
    208.         /// Sets the visabilty of the mouse.
    209.         /// </summary>
    210.         /// <param name="isVisable">True, to make the mouse cursor visable in the window. False to hide the mouse cursor</param>
    211.         public static void SetMouseVisability(bool isVisable = true) {
    212.             if (isVisable == true && GlobalMouse.IsVisable == false) {
    213.                 System.Windows.Forms.Cursor.Show();
    214.                 GlobalMouse.SceneInstance.isVisable = true;
    215.                 return;
    216.             }
    217.             if (isVisable == false && GlobalMouse.IsVisable == true) {
    218.                 GlobalMouse.SceneInstance.isVisable = false;
    219.                 System.Windows.Forms.Cursor.Hide();
    220.                 return;
    221.             }
    222.         }
    223.         #endregion
    224.     }
    225.  
    226.     /// <summary>
    227.     /// Locked Cursor Properties to define if the cursor is
    228.     /// locked and where it is locked.
    229.     /// </summary>
    230.     [System.Serializable]
    231.     public sealed class CursorLockProperties : System.Object {
    232.         private bool isLocked = false;
    233.         private int lockedX = -1;
    234.         private int lockedY = -1;
    235.         private bool seal = false;
    236.  
    237.         /// <summary>
    238.         /// Creates an instance of CursorLockProperties.
    239.         /// </summary>
    240.         /// <param name="isLocked"></param>
    241.         /// <param name="seal"></param>
    242.         /// <param name="lockedX"></param>
    243.         /// <param name="lockedY"></param>
    244.         public CursorLockProperties(bool isLocked = false, bool seal = false, int lockedX = -1, int lockedY = -1) {
    245.             this.isLocked = isLocked;
    246.             this.lockedX = lockedX;
    247.             this.lockedY = lockedY;
    248.             this.seal = seal;
    249.         }
    250.  
    251.         #region Properties
    252.         /// <summary>
    253.         /// Boolean: If the cursor is currenty locked.
    254.         /// </summary>
    255.         public bool IsLocked {
    256.             get {
    257.                 return this.isLocked;
    258.             }
    259.         }
    260.  
    261.         /// <summary>
    262.         /// Integer: If locked, will return the X position of the
    263.         /// mouse's locked position. If not locked, will
    264.         /// return -1 by default.
    265.         /// </summary>
    266.         public int LockedX {
    267.             get {
    268.                 return this.lockedX;
    269.             }
    270.         }
    271.  
    272.         /// <summary>
    273.         /// Integer: If locked, will return the Y position of the
    274.         /// mouse's locked position. If not locked, will
    275.         /// return -1 by default.
    276.         /// </summary>
    277.         public int LockedY {
    278.             get {
    279.                 return this.lockedY;
    280.             }
    281.         }
    282.  
    283.         /// <summary>
    284.         /// Boolean: Returns if the cursor will be locked,
    285.         /// even if the application gets interuppted.
    286.         /// </summary>
    287.         public bool Seal {
    288.             get {
    289.                 return seal;
    290.             }
    291.         }
    292.         #endregion
    293.     }
    294. }
    295.  
     
    Last edited: Oct 19, 2014