79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
![]() |
using UnityEngine;
|
|||
|
|
|||
|
public class MousePosHandle : MonoBehaviour
|
|||
|
{
|
|||
|
#region <EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//------------------------------------------------------------------------------------
|
|||
|
|
|||
|
//------------------------------------------------------------------------------------
|
|||
|
#endregion
|
|||
|
|
|||
|
#region ˽<EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//------------------------------------------------------------------------------------
|
|||
|
private Transform dragGameObject;
|
|||
|
private Vector3 offset;
|
|||
|
private bool isPicking;
|
|||
|
private Vector3 targetScreenPoint;
|
|||
|
//------------------------------------------------------------------------------------
|
|||
|
#endregion
|
|||
|
|
|||
|
#region <EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
#endregion
|
|||
|
|
|||
|
#region ˽<EFBFBD>з<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//------------------------------------------------------------------------------------
|
|||
|
private void Update()
|
|||
|
{
|
|||
|
if (Input.GetMouseButtonDown(0))
|
|||
|
{
|
|||
|
if (CheckGameObject())
|
|||
|
{
|
|||
|
offset = dragGameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetScreenPoint.z));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (isPicking)
|
|||
|
{
|
|||
|
//<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>
|
|||
|
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetScreenPoint.z);
|
|||
|
//<2F>ѵ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Vector3 curWorldPoint = Camera.main.ScreenToWorldPoint(curScreenPoint);
|
|||
|
Vector3 targetPos = curWorldPoint + offset;
|
|||
|
|
|||
|
dragGameObject.position = targetPos;
|
|||
|
}
|
|||
|
|
|||
|
if (Input.GetMouseButtonUp(0))
|
|||
|
{
|
|||
|
isPicking = false;
|
|||
|
if (dragGameObject != null)
|
|||
|
{
|
|||
|
dragGameObject = null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
//------------------------------------------------------------------------------------
|
|||
|
/// <summary>
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>cbue
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
bool CheckGameObject()
|
|||
|
{
|
|||
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|||
|
RaycastHit hitInfo;
|
|||
|
if (Physics.Raycast(ray, out hitInfo, 1000f))
|
|||
|
{
|
|||
|
isPicking = true;
|
|||
|
//<2F>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
dragGameObject = hitInfo.collider.gameObject.transform;
|
|||
|
|
|||
|
targetScreenPoint = Camera.main.WorldToScreenPoint(dragGameObject.position);
|
|||
|
return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
//------------------------------------------------------------------------------------
|
|||
|
#endregion
|
|||
|
}
|