12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class MouseCoordinate : MonoBehaviour
- {
- public RawImage Jt;
- public Transform bg;
- public RectTransform canvasRect;
- public GameObject canvas;
- public Camera uiCamera;
- // Start is called before the first frame update
- void Start()
- {
- GameObject obj = canvasRect.parent.gameObject;
- canvas = obj.transform.parent.gameObject;
- uiCamera = canvas.GetComponent<Canvas>().worldCamera;
- }
- Vector2 outVec;
- // Update is called once per frame
- void Update()
- {
- #if UNITY_EDITOR
- if (Input.GetMouseButtonDown(0))
- {
- Debug.Log("GetMouseButtonDown " + Input.mousePosition);
- // Debug.Log(Input.GetTouch(0).position.x + "AAA" + Input.GetTouch(0).position.y);
- // Debug.Log(Screen.width + "BBB" + Screen.height);
- string x = (Input.mousePosition.x / Screen.width).ToString();
- string y = ((Screen.height - Input.mousePosition.y) / Screen.height).ToString();
- Debug.Log(x + "CCC" + y);
- WSHandler.Rtc.coordinate(10000, x, y, AgoraVideoAudioManager.Instance.GetUID(AgoraVideoAudioManager.Instance.mainViewPeerId).ToString());
- // CreateJT(ScreenPointToUILocalPoint(canvasRect,Input.mousePosition));
-
- }
- #endif
- #if UNITY_ANDROID
- if(Input.touchCount>0&& Input.GetTouch(0).phase == TouchPhase.Ended)
- {
- Debug.Log(Input.mousePosition);
- string x = (Input.mousePosition.x / Screen.width).ToString();
- string y = ((Screen.height - Input.mousePosition.y) / Screen.height).ToString();
- Debug.Log(x + "CCC" + y);
- WSHandler.Rtc.coordinate(10000, x, y, AgoraVideoAudioManager.Instance.GetUID(AgoraVideoAudioManager.Instance.mainViewPeerId).ToString());
- // CreateJT(ScreenPointToUILocalPoint(canvasRect, Input.mousePosition));
- }
- #endif
- }
- public Vector2 ScreenPointToUILocalPoint(RectTransform parentRT, Vector2 screenPoint)
- {
- Vector2 localPos;
- RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRT, screenPoint, uiCamera, out localPos);
- // 转换后的 localPos 使用下面方法赋值
- // target 为需要使用的 UI RectTransform
- // parentRT 是 target.parent.GetComponent<RectTransform>()
- // 最后赋值 target.anchoredPosition = localPos;
- return localPos;
- }
- void CreateJT( Vector2 pos )
- {
- RawImage obj = Instantiate(Jt, bg);
- obj.GetComponent<RectTransform>().anchoredPosition = pos;
- obj.gameObject.SetActive(true);
- }
- }
|