MouseCursor.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using UnityEngine;
  2. using System.Collections;
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProMovieCapture
  7. {
  8. /// <summary>
  9. /// Draws a mouse cursor on the screen using IMGUI, allowing the cursor to be captured when using CaptureFromScreen component
  10. /// </summary>
  11. [AddComponentMenu("AVPro Movie Capture/Utils/Render Mouse Cursor", 302)]
  12. public class MouseCursor : MonoBehaviour
  13. {
  14. [SerializeField] Texture2D _texture = null;
  15. [SerializeField] Vector2 _hotspotOffset = Vector2.zero;
  16. [SerializeField, Range(1, 16)] int _sizeScale = 1;
  17. [SerializeField] int _depth = -9999;
  18. // State
  19. private GUIContent _content;
  20. void Start()
  21. {
  22. SetTexture(_texture);
  23. }
  24. public void SetTexture(Texture2D texture)
  25. {
  26. if (texture != null)
  27. {
  28. _content = new GUIContent(texture);
  29. _texture = texture;
  30. }
  31. }
  32. private void OnGUI()
  33. {
  34. if (_content != null)
  35. {
  36. GUI.depth = _depth;
  37. Vector2 p = Event.current.mousePosition;
  38. Rect rect = new Rect(p.x - _hotspotOffset.x, p.y - _hotspotOffset.y, _texture.width * _sizeScale, _texture.height * _sizeScale);
  39. GUI.Label(rect, _content);
  40. }
  41. }
  42. }
  43. }