FPSCursorRenderer.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using UnityEngine;
  3. namespace ZenFulcrum.EmbeddedBrowser {
  4. /**
  5. * Draws a crosshair in the middle of the screen which changes to cursors as you mouseover
  6. * things in world-space browsers.
  7. *
  8. * Often, this will be created automatically. If you want to alter parameters, add this script
  9. * to an object (such as the camera) and edit them there.
  10. */
  11. [Obsolete("Use PointerUIMesh and CursorRendererOverlay instead.")]
  12. public class FPSCursorRenderer : MonoBehaviour {
  13. private static FPSCursorRenderer _instance;
  14. public static FPSCursorRenderer Instance {
  15. get {
  16. if (!_instance) {
  17. _instance = FindObjectOfType<FPSCursorRenderer>();
  18. if (!_instance) {
  19. var go = new GameObject("Cursor Crosshair");
  20. _instance = go.AddComponent<FPSCursorRenderer>();
  21. }
  22. }
  23. return _instance;
  24. }
  25. }
  26. [Tooltip("How large should we render the cursor?")]
  27. public float scale = .5f;
  28. [Tooltip("How far can we reach to push buttons and such?")]
  29. public float maxDistance = 7f;
  30. [Tooltip("What are we using to point at things? Leave as null to use Camera.main")]
  31. public Transform pointer;
  32. /**
  33. * Toggle this to enable/disable input for all FPSBrowserUI objects.
  34. * This is useful, for example, during plot sequences and pause menus.
  35. */
  36. public bool EnableInput { get; set; }
  37. public static void SetUpBrowserInput(Browser browser, MeshCollider mesh) {
  38. var crossHair = Instance;
  39. var pointer = crossHair.pointer;
  40. if (!pointer) pointer = Camera.main.transform;//nb: don't use crossHair.pointer ?? camera, will incorrectly return null
  41. var fpsUI = FPSBrowserUI.Create(mesh, pointer, crossHair);
  42. fpsUI.maxDistance = crossHair.maxDistance;
  43. browser.UIHandler = fpsUI;
  44. }
  45. protected BrowserCursor baseCursor, currentCursor;
  46. public void Start() {
  47. EnableInput = true;
  48. baseCursor = new BrowserCursor();
  49. baseCursor.SetActiveCursor(BrowserNative.CursorType.Cross);
  50. }
  51. public void OnGUI() {
  52. if (!EnableInput) return;
  53. var cursor = currentCursor ?? baseCursor;
  54. var tex = cursor.Texture;
  55. if (tex == null) return;//hidden cursor
  56. var pos = new Rect(Screen.width / 2f, Screen.height / 2f, tex.width * scale, tex.height * scale);
  57. pos.x -= cursor.Hotspot.x * scale;
  58. pos.y -= cursor.Hotspot.y * scale;
  59. GUI.DrawTexture(pos, tex);
  60. }
  61. public void SetCursor(BrowserCursor newCursor, FPSBrowserUI ui) {
  62. currentCursor = newCursor;
  63. }
  64. }
  65. }