第一次提交
This commit is contained in:
84
Assets/Scripts/Utils/UIMouseOver.cs
Normal file
84
Assets/Scripts/Utils/UIMouseOver.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class UIMouseOver : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
|
||||
{
|
||||
Coroutine _trackCor;
|
||||
private RectTransform _canvasRectTransform;//<2F><><EFBFBD><EFBFBD>
|
||||
|
||||
//地图的宽高
|
||||
public int worldWidth;
|
||||
public int worldHeight;
|
||||
|
||||
//
|
||||
private float _initX;
|
||||
private float _initY;
|
||||
|
||||
private Vector3 _overPos;
|
||||
|
||||
public void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData)
|
||||
{
|
||||
if(UIWindow.Instance.uiMapPanel.CurOpenMapId < 0)
|
||||
{
|
||||
UIWindow.Instance.ShowMessage("先打开地图");
|
||||
return;
|
||||
}
|
||||
|
||||
worldWidth = MapManager.Instance.mapWidth;
|
||||
worldHeight = MapManager.Instance.mapHeight;
|
||||
|
||||
_canvasRectTransform = UIWindow.Instance.newMapTrans.parent.GetComponent<RectTransform>();
|
||||
|
||||
_trackCor = StartCoroutine(TrackPointer());
|
||||
}
|
||||
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
if (_trackCor == null)
|
||||
return;
|
||||
|
||||
StopCoroutine(_trackCor);
|
||||
|
||||
_trackCor = null;
|
||||
}
|
||||
|
||||
IEnumerator TrackPointer()
|
||||
{
|
||||
while (Application.isPlaying)
|
||||
{
|
||||
CurMousePosition();
|
||||
yield return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void CurMousePosition()
|
||||
{
|
||||
Vector2 pos;
|
||||
Vector2 screenPos = RectTransformUtility.WorldToScreenPoint(Camera.main, Input.mousePosition);
|
||||
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(_canvasRectTransform, Input.mousePosition, Camera.main, out pos))
|
||||
{
|
||||
float offsetX = UIWindow.Instance.newMapTrans.GetComponent<RectTransform>().anchoredPosition.x - _initX;
|
||||
float offsetY = UIWindow.Instance.newMapTrans.GetComponent<RectTransform>().anchoredPosition.y - _initY;
|
||||
|
||||
float x = pos.x - offsetX;
|
||||
float y = pos.y - offsetY;
|
||||
|
||||
Debug.Log($"坐标:{x + worldWidth / 10 / 2}, {y + worldHeight / 10 / 2}");
|
||||
_overPos = new Vector3(x + worldWidth / 10 / 2, 0, y + worldHeight / 10 / 2);
|
||||
}
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
_initX = UIWindow.Instance.newMapTrans.transform.GetComponent<RectTransform>().anchoredPosition.x;
|
||||
_initY = UIWindow.Instance.newMapTrans.transform.GetComponent<RectTransform>().anchoredPosition.y;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user