Search Unity

Make the cursor stay inside the game, because it's warmer.

Discussion in 'Scripting' started by taoa, Feb 18, 2010.

  1. taoa

    taoa

    Joined:
    Dec 10, 2009
    Posts:
    88
    Hello,

    Anyone knows a nice way to make the cursor stay inside the game window? Bearing in mind that I'm on a standalone application, not a web-based game.

    Before you tell me to use the lockCursor property, let me tell you that I still want a moving cursor in the game, but I don't want it to be able to leave the game window.
    And that also applies to running the game in FullScreen, as even there does the cursor go away from the game.

    Also, before you tell me that if I want such a behaviour I'll have to make my own cursor system, then how can I make my own cursor system work with Unity's GUI?

    Thanks for any help provided.

    (P.S.: I've seen dicussions more or less remotely related to this before, but none gave any clear answer, so there! :))
     
  2. taoa

    taoa

    Joined:
    Dec 10, 2009
    Posts:
    88
    Bump.


    Also: How can I get the game frame's (or window) position in the system's screen?
    Thanks!
     
  3. AmazingInt

    AmazingInt

    Joined:
    Dec 7, 2009
    Posts:
    157
    Lock cursor with custom mouse pointer would likely be the only solution with a windowed app as windows will allow the cursor to move off the app window.

    Fullscreen shouldn't be a problem unless you're running dual monitors.
     
  4. taoa

    taoa

    Joined:
    Dec 10, 2009
    Posts:
    88
    Unfortunately not, as locking the cursor would force it at a certain position, thus breaking Unity's GUI.

    That is precisely the problem! :eek:
    Most people have dual screens nowadays, I know everyone around me have.
     
  5. cdalexander

    cdalexander

    Joined:
    Nov 9, 2009
    Posts:
    73
    So essentially, there is no way other than the plugin with C#?

    I'm new to C#, but as I understand it, this is the way it is done...

    Code (csharp):
    1.  
    2. /* file: MouseControl.c */
    3.  
    4. // for Unity commands
    5. using UnityEngine;
    6.  
    7. // not sure if we even need this
    8. using System.Collections;
    9.  
    10. // for DllImport command
    11. using System.Runtime.InteropServices;
    12.  
    13. public class MouseControl: MonoBehaviour {
    14.  
    15.   [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    16.  
    17.   public static extern int SetCursorPos(int x, int y);
    18.   private float mouseX;
    19.   private float mouseY;
    20.  
    21.   void Start () {
    22.     // x and y will refer to the monitor and
    23.     // not the "Game Screen" as one would expect
    24.     SetCursorPos ((int)mouseX, (int)mouseY);
    25.   }
    26. }
    27.  
     
  6. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    how do I use that plugin?

    I am trying to implement this myself.

    do I just put in in a CSharp file and drop it into unity? Or does that have to be compiled some different way?
     
  7. cdalexander

    cdalexander

    Joined:
    Nov 9, 2009
    Posts:
    73
    You would have to copy whatever DLL file you wish to use (in the example I used user32.dll) to your Assets folder. I gave a C# example because I don't think UnityScript can import plugins like this (although I really wish it could).

    BUT BEWARE -- When using DLL files not created by you, you should read and understand the license regarding that file. If you publish something for profit using a DLL file that belongs to another company (like Microsoft), you could get in legal trouble.

    Unity has a few Mono DLL files that have a wealth of functionality. They are within the Unity3D "Install Directory", in the path
    "<unity program directory>\Editor\Data\MonoCompiler.framework\"

    I believe the System.Windows.Forms.dll has mouse methods you could use. I have assumed we were allowed to use this for development, but you may want to confirm this.

    You may also want to use some sort of DLL Info application to find out what methods (functions) are inside of a DLL file. Then you can do a quick Google to find out how to use it. The application "Dependency Walker" isn't that bad, give it a try:
    http://www.dependencywalker.com/
     
  8. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    Wait a sec. You can import DLL's into unity?!

    Does this work in the indie version or is it pro only?

    edit*

    ok this works in indie... I did not know you could import dll functionality... What is the difference between this and native code plugin?

    Also. The cursor can still be moved away for like a millsecond. It's as though the mouse updates at 1/1000 of a second and the unity script Update functions updates at 1/100 of second. Is there anyway to lock the mouse cursor to a specific position so it can't move at all?

    and how would you implement this with OSX?
     
  9. cdalexander

    cdalexander

    Joined:
    Nov 9, 2009
    Posts:
    73
    As you can see, you can import DLL functionality into Indy. I use PRO so I was unable to verify that, but it looks like you did.

    Not sure what you mean. In my point of view, a plugin is not native, hence the name 'plug-in'. Anything can be a plugin as long as Unity can A) Read it B) Execute it. You can use Visual Studios to write custom plugins. To learn a little bit about Plugins with Unity, here are some starting points:

    Plugin Documentation
    Texture Plugin Example
    Wordpress Plugin Example

    There are 2 commands native to Unity that you could use with the DLL:

    Code (csharp):
    1.  
    2. // put cursor center screen and freeze it
    3. // this resets per frame, so keep it in Update()
    4. if (boolVar)
    5.   Screen.lockCuror = true;
    6.  
    7. // hide cursor
    8. // this resets per frame, so keep it in Update()
    9. if (boolVar)
    10.   Screen.showCursor = false;
    11.  
    12. // now use the DLL to fix cursor at your preferred location
    13.  
    The 'lockCursor" may keep the cursor frozen until you unlock it by setting your boolean variable to false, or until the User ctrl|alt-Tab which unlocks it automatically.

    Manual: Screen.lockCursor
    Manual: Screen.showCursor

    Don't know anything about OSX (sorry).

    Good Luck!
     
  10. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    By native code plugin I mean, a c++ plugin. Using code, loading a dll written in c or c++.

    All those pages say that plugins, with that dll import command, are pro only.

    But yet the dll import of user32.dll works just fine in indie.

    Whats going on here? Could anyone on the Unity team clarify this?

    There isn't some bug in unity indie this release that accidentally enabled plugins? Or does the dllimport function really work just fine in indie?

    And if thats the case, then what exactly is the pro plugin support opening up?