MouseCoordinate.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class MouseCoordinate : MonoBehaviour
  6. {
  7. public RawImage Jt;
  8. public Transform bg;
  9. public RectTransform canvasRect;
  10. public GameObject canvas;
  11. public Camera uiCamera;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. GameObject obj = canvasRect.parent.gameObject;
  16. canvas = obj.transform.parent.gameObject;
  17. uiCamera = canvas.GetComponent<Canvas>().worldCamera;
  18. }
  19. Vector2 outVec;
  20. // Update is called once per frame
  21. void Update()
  22. {
  23. #if UNITY_EDITOR
  24. if (Input.GetMouseButtonDown(0))
  25. {
  26. Debug.Log("GetMouseButtonDown " + Input.mousePosition);
  27. // Debug.Log(Input.GetTouch(0).position.x + "AAA" + Input.GetTouch(0).position.y);
  28. // Debug.Log(Screen.width + "BBB" + Screen.height);
  29. string x = (Input.mousePosition.x / Screen.width).ToString();
  30. string y = ((Screen.height - Input.mousePosition.y) / Screen.height).ToString();
  31. Debug.Log(x + "CCC" + y);
  32. WSHandler.Rtc.coordinate(10000, x, y, AgoraVideoAudioManager.Instance.GetUID(AgoraVideoAudioManager.Instance.mainViewPeerId).ToString());
  33. // CreateJT(ScreenPointToUILocalPoint(canvasRect,Input.mousePosition));
  34. }
  35. #endif
  36. #if UNITY_ANDROID
  37. if(Input.touchCount>0&& Input.GetTouch(0).phase == TouchPhase.Ended)
  38. {
  39. Debug.Log(Input.mousePosition);
  40. string x = (Input.mousePosition.x / Screen.width).ToString();
  41. string y = ((Screen.height - Input.mousePosition.y) / Screen.height).ToString();
  42. Debug.Log(x + "CCC" + y);
  43. WSHandler.Rtc.coordinate(10000, x, y, AgoraVideoAudioManager.Instance.GetUID(AgoraVideoAudioManager.Instance.mainViewPeerId).ToString());
  44. // CreateJT(ScreenPointToUILocalPoint(canvasRect, Input.mousePosition));
  45. }
  46. #endif
  47. }
  48. public Vector2 ScreenPointToUILocalPoint(RectTransform parentRT, Vector2 screenPoint)
  49. {
  50. Vector2 localPos;
  51. RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRT, screenPoint, uiCamera, out localPos);
  52. // 转换后的 localPos 使用下面方法赋值
  53. // target 为需要使用的 UI RectTransform
  54. // parentRT 是 target.parent.GetComponent<RectTransform>()
  55. // 最后赋值 target.anchoredPosition = localPos;
  56. return localPos;
  57. }
  58. void CreateJT( Vector2 pos )
  59. {
  60. RawImage obj = Instantiate(Jt, bg);
  61. obj.GetComponent<RectTransform>().anchoredPosition = pos;
  62. obj.gameObject.SetActive(true);
  63. }
  64. }