61 lines
2.0 KiB
C#
61 lines
2.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace ZXKFramework
|
|
{
|
|
public class MouseMoveObj : MonoBehaviour
|
|
{
|
|
public Action<GameObject> down;
|
|
public Action<GameObject> up;
|
|
public bool isMove = false;
|
|
|
|
private Vector3 screenPoint;
|
|
private Vector3 offset;
|
|
|
|
|
|
public LayerMask layerMask;
|
|
|
|
GameObject go;
|
|
|
|
void Update()
|
|
{
|
|
if (Input.GetMouseButtonDown(0) && isMove/*&& UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()*/)
|
|
{
|
|
if (!EventSystem.current.IsPointerOverGameObject())
|
|
{
|
|
RaycastHit hit;
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
|
|
if (Physics.Raycast(ray, out hit))
|
|
{
|
|
if (hit.collider.name.Equals(gameObject.name))
|
|
{
|
|
|
|
go = gameObject;
|
|
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
|
|
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
|
|
down?.Invoke(gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (Input.GetMouseButton(0) && isMove && go != null/*&& UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()*/)
|
|
{
|
|
Vector3 curScreenPoint = new(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
|
|
Vector3 curWorldPoint = Camera.main.ScreenToWorldPoint(curScreenPoint);
|
|
Vector3 p = curWorldPoint + offset;
|
|
transform.position = p;
|
|
}
|
|
if (Input.GetMouseButtonUp(0) && isMove && go != null)
|
|
{
|
|
up?.Invoke(gameObject);
|
|
go = null;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
} |