一 、 Overlay渲染模式下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CanvasMouseMove : MonoBehaviour
{
private Transform targetTrans;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//射线检测
RaycastHit2D hit = (Physics2D.Raycast(Input.mousePosition, Vector3.forward));
if (hit == false)
{
return;
}
else
{
targetTrans = hit.transform;
}
}
else if (Input.GetMouseButtonUp(0))
{
targetTrans = null;
}
if (targetTrans != null)
{
targetTrans.position = Input.mousePosition;
}
}
}
二、Camera与WorldSpace渲染模式
1.使用前摄像机模式调整为Orthograghic
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CanvasMouse2 : MonoBehaviour
{
public Camera mCamera;
private Vector2 rayPos;
private RaycastHit2D mHitInfo;
public RectTransform canvasRect;
Vector3 mPos;
public Transform mMouse;
private bool Isigontmove = false;
void Update()
{
//如果canvas坐标是(0,0,0) 可以直接使用世界坐标
//mPos = mCamera.ScreenToWorldPoint(Input.mousePosition);
//mPos = new Vector3(mPos.x, mPos.y, 0)
//mMouse.transform.position = mPos;
//物体跟随移动,坐标不是(0,0,0)时
Vector2 uiPosition1;
RectTransformUtility.ScreenPointToLocalPointInRectangle(this.GetComponent<RectTransform>(), Input.mousePosition, mCamera, out uiPosition1);
mMouse.transform.localPosition = new Vector3( uiPosition1.x, uiPosition1.y,0);
//点击跟随移动
if (Input.GetMouseButtonDown(0))
{
rayPos = Input.touchCount > 0 ? mCamera.ScreenToWorldPoint(Input.GetTouch(0).position) : mCamera.ScreenToWorldPoint(Input.mousePosition);
mHitInfo = Physics2D.Raycast(rayPos, -Vector2.up, 0.001f);
if (mHitInfo.collider != null && mHitInfo.collider.transform.tag == "test")
{
//mHitInfo.collider.transform.position = mCamera.ScreenToWorldPoint(Input.mousePosition);
Debug.Log(mHitInfo.collider.name);
Isigontmove = true;
}
}
if (Input.GetMouseButtonUp(0))
{
//rayPos = mMouse.transform.position;
Isigontmove = false;
}
if (Isigontmove)
{
Vector2 uiPosition;
Vector2 screenPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
// 使用 RectTransformUtility.ScreenPointToLocalPointInRectangle 将屏幕坐标转换为UI坐标
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(this.GetComponent<RectTransform>(), screenPosition, mCamera, out uiPosition))
{
Debug.Log(uiPosition.x + "uiPosition.y===" + uiPosition.y);
mHitInfo.collider.transform.localPosition = uiPosition;
}
}
}
}
三、摄像机为透视Perspective时 Camera与WorldSpace渲染模式
射线检测需要使用3D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CanvasMouse3 : MonoBehaviour
{
public Camera mCamera;
public RectTransform canvasRect;
public Transform mMouse;
private bool Isigontmove = false;
private GameObject curGo;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
// 从鼠标位置发射一条射线
Ray ray = mCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// 进行射线检测,判断是否与UI物体相交
if (Physics.Raycast(ray, out hit))
{
// 检查是否击中了Canvas上的UI物体
if (hit.collider.gameObject.tag=="test")
{
curGo = hit.collider.gameObject;
// 这里可以添加处理UI物体的代码
Debug.Log("击中了UI物体:" + hit.collider.gameObject.name);
Isigontmove = true;
}
}
}
if (Input.GetMouseButtonUp(0))
{
Isigontmove = false;
}
if (Isigontmove)
{
Vector2 uiPosition;
Vector2 screenPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
// 使用 RectTransformUtility.ScreenPointToLocalPointInRectangle 将屏幕坐标转换为UI坐标
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(this.GetComponent<RectTransform>(), screenPosition, mCamera, out uiPosition))
{
Debug.Log(uiPosition.x + "uiPosition.y===" + uiPosition.y);
curGo.transform.localPosition = uiPosition;
}
}
}
}