CUI_DragBetweenCanvases.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. namespace CurvedUI
  7. {
  8. /// <summary>
  9. /// This component enables accurate object dragging over curved canvas. It also allows user to drop the object on another canvas.
  10. /// It supports both mouse and gaze controllers. Add it to your canvas object with image component.
  11. /// </summary>
  12. public class CUI_DragBetweenCanvases : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
  13. {
  14. //saved location where we grabbed the panel
  15. Vector2 dragPoint;
  16. public void OnBeginDrag(PointerEventData data)
  17. {
  18. Debug.Log("OnBeginDrag");
  19. Vector2 newPos = Vector2.zero;
  20. RaycastPosition(out newPos);
  21. //save distance from click point to object center to allow for precise dragging
  22. dragPoint = new Vector2((transform as RectTransform).localPosition.x, (transform as RectTransform).localPosition.y) - newPos;
  23. }
  24. /// <summary>
  25. /// drag the transform along the mouse. We use raycast to determine its position on curved canvas.
  26. /// </summary>
  27. /// <param name="data"></param>
  28. public void OnDrag(PointerEventData data)
  29. {
  30. CurvedUISettings myCurvedCanvas = GetComponentInParent<CurvedUISettings>();
  31. Ray ray3d = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2.0f, Screen.height / 2.0f));
  32. if (CurvedUIInputModule.ControlMethod == CurvedUIInputModule.CUIControlMethod.MOUSE)
  33. {
  34. //position when using mouse
  35. ray3d = Camera.main.ScreenPointToRay(Input.mousePosition);
  36. }
  37. else if (CurvedUIInputModule.ControlMethod == CurvedUIInputModule.CUIControlMethod.GAZE)
  38. {
  39. //position when using gaze
  40. ray3d = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2.0f, Screen.height / 2.0f));
  41. }
  42. //find a canvas underneath the pointer.
  43. RaycastHit hit;
  44. if (Physics.Raycast(ray3d, out hit))
  45. {
  46. CurvedUISettings curvedCanvasUnderPointer = hit.collider.GetComponentInParent<CurvedUISettings>();
  47. Vector2 newPos = Vector2.zero;
  48. if (curvedCanvasUnderPointer != null)
  49. {
  50. //change canvas if we moved it to the other one.
  51. if(curvedCanvasUnderPointer != myCurvedCanvas)
  52. {
  53. //move this panel to the other canvas and resets its position.
  54. this.transform.SetParent(curvedCanvasUnderPointer.transform);
  55. this.transform.ResetTransform();
  56. //force the panel and each of its children to update their parent CurvedUISettings.
  57. //This will ensure they will rebuild with proper angle and stuff.
  58. foreach(CurvedUIVertexEffect eff in GetComponentsInChildren<CurvedUIVertexEffect>())
  59. {
  60. eff.FindParentSettings(true);
  61. }
  62. }
  63. //find the raycast position in flat canvas units.
  64. curvedCanvasUnderPointer.RaycastToCanvasSpace(ray3d, out newPos);
  65. //set the new position of the panel. Add the drag point so we're still holding the object in the same spot.
  66. (transform as RectTransform).localPosition = newPos + dragPoint;
  67. }
  68. }
  69. }
  70. public void OnEndDrag(PointerEventData data)
  71. {
  72. Debug.Log("OnEndDrag");
  73. }
  74. void RaycastPosition(out Vector2 newPos)
  75. {
  76. if (CurvedUIInputModule.ControlMethod == CurvedUIInputModule.CUIControlMethod.MOUSE)
  77. {
  78. //position when using mouse
  79. GetComponentInParent<CurvedUISettings>().RaycastToCanvasSpace(Camera.main.ScreenPointToRay(Input.mousePosition), out newPos);
  80. }
  81. else if (CurvedUIInputModule.ControlMethod == CurvedUIInputModule.CUIControlMethod.GAZE)
  82. {
  83. //position when using gaze
  84. GetComponentInParent<CurvedUISettings>().RaycastToCanvasSpace(Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2.0f, Screen.height / 2.0f)), out newPos);
  85. }
  86. else newPos = Vector2.zero;
  87. }
  88. }
  89. }