diff --git a/Assets/Art/UIPrefab/UIBody3DMouse.prefab b/Assets/Art/UIPrefab/UIBody3DMouse.prefab index 2fb5d558..7da19ae4 100644 --- a/Assets/Art/UIPrefab/UIBody3DMouse.prefab +++ b/Assets/Art/UIPrefab/UIBody3DMouse.prefab @@ -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} diff --git a/Assets/Scripts/Item/Body3DOjbItem.cs b/Assets/Scripts/Item/Body3DOjbItem.cs index cac75d5a..a0a24ca0 100644 --- a/Assets/Scripts/Item/Body3DOjbItem.cs +++ b/Assets/Scripts/Item/Body3DOjbItem.cs @@ -10,7 +10,7 @@ public class Body3DOjbItem : MonoBehaviour { public Body3D.Body body; - ObjectToggle objToggle; + public ObjectToggle objToggle; ObjDrag objDrag; // 记录上一次鼠标按下的时间 private float lastClickTime; diff --git a/Assets/Scripts/UI/UIBody3DMouse.cs b/Assets/Scripts/UI/UIBody3DMouse.cs index a1dcdb70..05095ca5 100644 --- a/Assets/Scripts/UI/UIBody3DMouse.cs +++ b/Assets/Scripts/UI/UIBody3DMouse.cs @@ -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(); + 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(); + if (bodyItem != null) + { + BodyName.text = bodyItem.body.Name; + BodyName.gameObject.SetActive(true); } } } - + void OnExitBody(GameObject obj) + { + BodyName.gameObject.SetActive(false); + } protected override void OnShow() {