完善鼠标功能

This commit is contained in:
shenjianxing 2025-02-19 17:23:39 +08:00
parent fa7ccb6aac
commit 10f624a776
3 changed files with 61 additions and 7 deletions

View File

@ -98,7 +98,7 @@ MonoBehaviour:
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 0
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}

View File

@ -10,7 +10,7 @@ public class Body3DOjbItem : MonoBehaviour
{
public Body3D.Body body;
ObjectToggle objToggle;
public ObjectToggle objToggle;
ObjDrag objDrag;
// 记录上一次鼠标按下的时间
private float lastClickTime;

View File

@ -11,6 +11,8 @@ namespace QFramework.Example
public partial class UIBody3DMouse : UIPanel
{
UIDragItem dragItem;
private GameObject currentHitObject; // 当前被击中的物体
private bool isObjectHit; // 标记是否有物体被击中
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UIBody3DMouseData ?? new UIBody3DMouseData();
@ -18,7 +20,14 @@ namespace QFramework.Example
SelectBtn.onClick.AddListener(() =>
{
if (currentHitObject != null)
{
var item = currentHitObject.GetComponent<Body3DOjbItem>();
if (item != null)
{
item.objToggle.OnValueChanged.Invoke(!item.objToggle.isOn);
}
}
});
}
@ -42,7 +51,7 @@ namespace QFramework.Example
{
if (Point != null && Camera.main != null)
{
// 获取Image组件的中心点屏幕坐标
// 获取 Image 组件的中心点屏幕坐标
Vector2 imageCenter = new Vector2(Point.rectTransform.position.x, Point.rectTransform.position.y);
// 将屏幕坐标转换为射线
@ -52,12 +61,57 @@ namespace QFramework.Example
// 进行射线检测
if (Physics.Raycast(ray, out hit))
{
// 打印被击中物体的名字
Debug.LogError("击中的物体名字: " + hit.collider.gameObject.name);
GameObject hitObject = hit.collider.gameObject;
if (!isObjectHit)
{
// 射线首次进入物体
currentHitObject = hitObject;
isObjectHit = true;
Debug.Log("射线进入物体: " + hitObject.name);
OnEnterBody(currentHitObject);
}
else if (currentHitObject != hitObject)
{
// 射线从之前的物体移除并击中了新物体
Debug.Log("射线移除物体: " + currentHitObject.name);
OnExitBody(currentHitObject);
currentHitObject = hitObject;
OnEnterBody(currentHitObject);
Debug.Log("射线进入物体: " + hitObject.name);
}
}
else
{
if (isObjectHit)
{
// 射线移除当前物体
OnExitBody(currentHitObject);
Debug.Log("射线移除物体: " + currentHitObject.name);
currentHitObject = null;
isObjectHit = false;
}
}
}
}
void OnEnterBody(GameObject obj)
{
if (obj != null)
{
var bodyItem = currentHitObject.GetComponent<Body3DOjbItem>();
if (bodyItem != null)
{
BodyName.text = bodyItem.body.Name;
BodyName.gameObject.SetActive(true);
}
}
}
void OnExitBody(GameObject obj)
{
BodyName.gameObject.SetActive(false);
}
protected override void OnShow()
{