Unity Community


Results 1 to 3 of 3

  1. Location
    London
    Posts
    545

    Hardware Cursor Support

    Well, I've passed 100 hours of evaluating Unity3D Pro. Many headaches like no included A*Pathfinding and an inability to make Terrain rotations. I've got over those hurdles.

    Now I'm trying to implement a custom hardware cursor. I'm working on an RTS game, where the state of the cursor is used to communicate quite a lot of visual clues to the player. Using a software cursor is NOT ACCEPTABLE, any lag with the cursor will kill the gaming experience, as documented elsewhere on this forum.

    So far my exhaustive searches have lead to:
    http://forum.unity3d.com/threads/701...=custom+cursor
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    http://blog.naver.com/PostView.nhn?b...o=150096564973

    and I've finally come up with the following script based on the three Windows-only solutions I can find through Google. When I run the game (within the Editor), the custom mouse pointer appears, but then vanishes when the 3D scene appears, resetting to the default windows cursor. I read somewhere that the solution only worked on .exe builds, so I ran a build and the cursor doesn't show at all in that. (Windows 7 64-bit).

    Is there anyone that has a solution they wouldn't mind sharing?

    Code:  
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4. using System.Runtime.InteropServices;
    5.  
    6. public class CursorManager : MonoBehaviour
    7. {
    8.     [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
    9.     public static extern IntPtr SetCursor(IntPtr hCursor);
    10.    
    11.     [DllImport("user32.dll")]
    12.     public static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
    13.    
    14.     [DllImport("user32.dll")]
    15.     public static extern IntPtr LoadImage(IntPtr hInstance, string lpImageName, uint uType, int cxDesired, int cyDesired, uint fuLoad);
    16.  
    17.     //fuLoad
    18.     public const uint LR_CREATEDIBSECTION     =0x00002000;
    19.     public const uint LR_DEFAULTCOLOR     =0x00000000;
    20.     public const uint LR_DEFAULTSIZE     =0x00000040;
    21.     public const uint LR_LOADFROMFILE     =0x00000010;
    22.     public const uint LR_LOADMAP3DCOLORS     =0x00001000;
    23.     public const uint LR_LOADTRANSPARENT     =0x00000020;
    24.     public const uint LR_MONOCHROME     =0x00000001;
    25.     public const uint LR_SHARED     =0x00008000;
    26.     public const uint LR_VGACOLOR     =0x00000080;
    27.    
    28.     public void ChangeCursorToArrow()
    29.     {
    30.         SetCursor(LoadCursor(IntPtr.Zero, 32512));
    31.     }
    32.    
    33.     public void ChangeCursorToCustom(string filePath)
    34.     {
    35.         SetCursor(LoadImage(IntPtr.Zero, filePath, 2, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE));
    36.     }
    37.    
    38.     public void Init()
    39.     {
    40.         string cursorPath = Application.dataPath+"/Resources/Cursors/normal.cur";
    41.         ChangeCursorToCustom(cursorPath);
    42.     }
    43. }
    Last edited by gamesfoundry; 05-19-2011 at 04:24 AM.


  2. Posts
    484
    In our last game I implemented a hardware cursor using C# in Unity. But no matter what I did, it was not possible to reset the cursor to the previous one. Luckily it was only a prototype, and so nobody noticed/cared.
    Later one of the guys I worked with on this game came up to me and said that he succeeded resetting the cursor when doing so in C++ (in a dll) using the exact same approach I did in C#, and so had a fully functioning hardware cursor.
    I'm pretty sure I'm not allowed to rip the code out of our game and post it here, so I will just say that you might want to try using C++ for this one.
    Last edited by meh11; 05-19-2011 at 04:27 AM.


  3. Location
    London
    Posts
    545
    Well, looks like it's the Unity GUI system that is resetting the cursor.

    Anyhows, I've managed to get lubo3d's solution working for now, as I have no C++ experience.

    Please Unity Team, sort this out in the main API ASAP so it also works when playing in the Editor. I nearly threw in the towel during my eval because of this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •