0
 using System.Collections;
 using System.Collections.Generic; 
 using UnityEngine;

 public class mouseScript : MonoBehaviour {

 public Texture2D cursorTexture;
 public CursorMode cursorMode = CursorMode.Auto; 
 public Vector2 hotSpot = Vector2.zero;

 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {

 }
 void OnMouseEnter()
 {
     Debug.Log("texture mouse " + cursorTexture + "  cusor visiblity" + Cursor.visible); 
     Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
 }
 void OnMouseExit()
 {
     Cursor.SetCursor(null, Vector2.zero, cursorMode);
 }

}

I changed the mouse image properties:

  • texture type - cursor
  • read/write - enable html5 - override for webgl
  • max size - 32
  • format - RGB
  • compressed - DXT1

I used this coding to change the mouse cursor. In unity editor it was working correctly, when I export to html5 mouse, cursor is not changing.

When I debug the scene in browser it displays an error like this:

Error loading this URL: Could not load the source for blob:http://localhost:56077/f3a83177-83ac-4798-81fe-1b07da4548c2. [Exception... "Failed to open input source 'file:////localhost:56077/f3a83177-83ac-4798-81fe-1b07da4548c2'" nsresult: "0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH)" location: "JS frame :: resource://gre/modules/commonjs/toolkit/loader.js -> resource://devtools/shared/DevToolsUtils.js :: mainThreadFetch :: line 518" data: yes] Stack: mainThreadFetch@resource://gre/modules/commonjs/toolkit/loader.js -> resource://devtools/shared/DevToolsUtils.js:518:5 _getSourceText/<@resource://gre/modules/commonjs/toolkit/loader.js -> resource://devtools/server/actors/source.js:393:27 process@resource://gre/modules/Promise.jsm -> resource://gre/modules/Promise-backend.js:922:23 walkerLoop@resource://gre/modules/Promise.jsm -> resource://gre/modules/Promise-backend.js:806:7 scheduleWalkerLoop/<@resource://gre/modules/Promise.jsm -> resource://gre/modules/Promise-backend.js:742:11 Line: 518, column: 0

gman
  • 100,619
  • 31
  • 269
  • 393
Akila Kannan
  • 13
  • 1
  • 4
  • While my comment won't solve your problem exactly, you could create a [Javascript plugin](https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html) to change the class of the HTML body element. Then, according to the class of the body, you would be able to [change the cursor image in CSS](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_User_Interface/Using_URL_values_for_the_cursor_property) – Hellium Jun 29 '17 at 07:43

3 Answers3

3

I assume this is WebGL. Last time I tried to use the Cursor API with CursorMode.Auto option on WebGL, it didn't work and should be considered a bug.

I had to use CursorMode.ForceSoftware for WebGL and CursorMode.Auto for other builds with the help of the UNITY_WEBGL macro.

#if UNITY_WEBGL
        Cursor.SetCursor(cursorTexture, hotSpot, CursorMode.ForceSoftware);
#else
        Cursor.SetCursor(cursorTexture, hotSpot, CursorMode.Auto);
#endif

Note that if this does not still work then you have to make your own Cursor API which is really easy.

Simply disable the main cursor with Cursor.visible = false; then use a fake mouse pointer texture and set its position with Input.mousePosition.

Another option is to make a Javascript plugin but that should be a last resort solution.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • if i use CursorMode.ForceSoftware ,then both custom and default mouse cursor is visible. i used Cursor.visible = false statement,but still mouse cursor is visible – Akila Kannan Jun 29 '17 at 10:54
1

I was also getting the same problem that my hand cursor was not showing in webgl build. So i took @programmer answer and here is the modified version that worked for me:

#if UNITY_WEBGL
        float xspot = cursor.width / 2;
        float yspot = cursor.height / 2;
        Vector2 hotSpot = new Vector2(xspot, yspot);
        Cursor.SetCursor(cursor, hotSpot, CursorMode.ForceSoftware);
#else
         Cursor.SetCursor(cursor, Vector2.zero, CursorMode.Auto);
#endif

With additional step of setting with image, I set my image size to 32 in image import settings.

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
0

just set in Playersetting default Cursor.run in chrome browser. you need to put a software called "NetBox2.exe" with unityWebGL in a same directory. run it , input "http://localhost/mygame/" in browser.