当前位置:   article > 正文

unity 更换鼠标图标,设置默认图标_unity default cursor

unity default cursor

工具:unity2018.4.2f1、VS2017

一、准备好鼠标样式,如图我用PS截取的鼠标图标(大小:70x70):

二、将图标放入工程,并设置图片类型,如图所示:

三、开始写代码,代码如下:

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. public class ChangeMouseImage : MonoBehaviour
  4. {
  5. //鼠标样式
  6. [SerializeField]
  7. Texture2D m_mouseStyle;
  8. bool m_isDefault = false;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. transform.Find("ChangeMouseBtn").GetComponent<Button>().onClick.AddListener(() =>ChangeImage());
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. }
  18. void ChangeImage()
  19. {
  20. m_isDefault = !m_isDefault;
  21. //unity默认鼠标样式
  22. if (m_isDefault)
  23. {
  24. Cursor.SetCursor(null,Vector2.zero,CursorMode.Auto);
  25. }
  26. else
  27. {
  28. // 摘要:
  29. // Specify a custom cursor that you wish to use as a cursor.
  30. // 指定要用作光标的自定义光标。
  31. //
  32. // 参数:
  33. // texture:
  34. // The texture to use for the cursor. To use a texture, you must first import it
  35. // with `Read/Write`enabled. Alternatively, you can use the default cursor import
  36. // setting. If you created your cursor texture from code, it must be in RGBA32 format,
  37. // have alphaIsTransparency enabled, and have no mip chain. To use the default cursor,
  38. // set the texture to `Null`.
  39. // 用于光标的纹理。要使用纹理,必须先导入它 启用“读 / 写”。或者,可以使用默认的光标导入
  40. // 设置。如果从代码创建光标纹理,则它必须为RGBA32格式,启用AlphaistTransparency,并且没有mip链。要使用默认光标,
  41. // 将纹理设置为“Null”。
  42. //
  43. // hotspot:
  44. // The offset from the top left of the texture to use as the target point (must
  45. // be within the bounds of the cursor).
  46. // 要用作目标点的纹理左上角的偏移(必须在光标的范围内)。
  47. //
  48. // cursorMode:
  49. // Allow this cursor to render as a hardware cursor on supported platforms, or force
  50. // software cursor.
  51. // 允许此游标在支持的平台上呈现为硬件游标,或强制软件光标。
  52. Cursor.SetCursor(m_mouseStyle,new Vector2(5,5), CursorMode.Auto);
  53. //这里第二个参数设置为5,5是因为我的鼠标图片存在空白像素
  54. }
  55. }
  56. }

如图:

如果鼠标是充满整个图片的话,鼠标光标会偏大,不好看,所以我特意在周围添了些空白像素

 

注:脚本挂载在canvas上,不要忘记将图片拖到脚本上

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/48835
推荐阅读
相关标签
  

闽ICP备14008679号