【问题标题】:Mouse cursor is not changing on WebGL鼠标光标在 WebGL 上没有改变
【发布时间】:2017-12-02 18:17:41
【问题描述】:
 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);
 }

}

我更改了鼠标图像属性:

  • 纹理类型 - 光标
  • 读/写 - 启用 html5 - 覆盖 webgl
  • 最大尺寸 - 32
  • 格式 - RGB
  • 压缩 - DXT1

我用这个编码来改变鼠标光标。在统一编辑器中它工作正常,当我导出到 html5 鼠标时,光标没有改变。

当我在浏览器中调试场景时,它会显示如下错误:

加载此 URL 时出错:无法加载 blob 的源代码:http://localhost:56077/f3a83177-83ac-4798-81fe-1b07da4548c2。 [异常...“无法打开输入源'file:////localhost:56077/f3a83177-83ac-4798-81fe-1b07da4548c2'”nsresult:“0x80520001(NS_ERROR_FILE_UNRECOGNIZED_PATH)”位置:“JS 框架 :: 资源: //gre/modules/commonjs/toolkit/loader.js -> resource://devtools/shared/DevToolsUtils.js :: mainThreadFetch :: line 518" data: yes] 堆栈:mainThreadFetch@resource://gre/modules/commonjs/toolkit/loader.js -> resource://devtools/shared/DevToolsUtils.js:518:5 _getSourceText/ 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-backend.js:742:11 行:518,列:0

【问题讨论】:

标签: c# html unity3d mouse-cursor unity-webgl


【解决方案1】:

我假设这是 WebGL。上次我尝试在 WebGL 上使用 Cursor API 和 CursorMode.Auto 选项,它不起作用,应该被视为一个错误。

我不得不将CursorMode.ForceSoftware 用于 WebGL,CursorMode.Auto 用于其他 在 UNITY_WEBGL 宏的帮助下构建。

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

请注意,如果这仍然不起作用,那么您必须制作自己的 Cursor API,这非常简单。

只需使用Cursor.visible = false; 禁用主光标,然后使用假鼠标指针纹理并使用Input.mousePosition 设置其位置。

另一种选择是制作一个 Javascript 插件,但这应该是最后的解决方案。

【讨论】:

  • 如果我使用 CursorMode.ForceSoftware ,那么自定义和默认鼠标光标都是可见的。我使用了 Cursor.visible = false 语句,但鼠标光标仍然可见
【解决方案2】:

我也遇到了同样的问题,我的手形光标没有在 webgl 构建中显示。所以我接受了@programmer 的回答,这是对我有用的修改版本:

#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

通过额外的图像设置步骤,我在图像导入设置中将图像大小设置为 32。

【讨论】:

    【解决方案3】:

    只需在 Chrome 浏览器中的 Playersetting 默认 Cursor.run 中设置。 您需要将名为“NetBox2.exe”的软件与 unityWebGL 放在同一目录中。 运行它,在浏览器中输入“http://localhost/mygame/”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-22
      • 2018-06-10
      • 2015-09-27
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      相关资源
      最近更新 更多