fix: ShoseClickHandler回归OnMouseDown+SphereCollider(3.0)最简方案

This commit is contained in:
yangbear
2026-07-06 04:29:52 +08:00
parent 7f846da1a7
commit 94ae2ffbb9
4 changed files with 99 additions and 35 deletions
@@ -7,17 +7,14 @@ public class ShoseClickHandler : MonoBehaviour
private MeshRenderer _outlineMesh;
private bool _clicked;
void OnEnable()
void Start()
{
var col = GetComponent<Collider>();
if (col == null) col = gameObject.AddComponent<SphereCollider>();
if (col == null) { col = gameObject.AddComponent<SphereCollider>(); ((SphereCollider)col).radius = 3f; }
col.enabled = true;
if (col is SphereCollider sc) { sc.radius = 1.5f; sc.isTrigger = false; }
CreateOutlineMesh();
StopAllCoroutines();
StartCoroutine(BlinkOutline());
_clicked = false;
Debug.Log($"[ShoseClick] OnEnable obj={gameObject.name} outline={_outlineMesh!=null} col enabled={col.enabled} radius={((SphereCollider)col).radius}");
Debug.Log($"[ShoseClick] Start obj={gameObject.name} col={col.enabled} radius={((SphereCollider)col).radius}");
}
void CreateOutlineMesh()
@@ -46,38 +43,14 @@ public class ShoseClickHandler : MonoBehaviour
while (true) { if (_outlineMesh != null) _outlineMesh.enabled = !_outlineMesh.enabled; yield return new WaitForSeconds(0.4f); }
}
void Update()
{
if (_clicked || !Input.GetMouseButtonDown(0)) return;
var cams = Object.FindObjectsOfType<Camera>();
foreach (var cam in cams)
{
if (!cam.enabled) continue;
var hits = Physics.RaycastAll(cam.ScreenPointToRay(Input.mousePosition), 200f);
if (hits.Length > 0)
{
var names = new System.Text.StringBuilder();
foreach (var h in hits) names.Append(h.collider?.gameObject?.name ?? "null").Append(", ");
Debug.Log($"[ShoseClick] ★ Update click! hits={hits.Length} — {names}");
}
foreach (var h in hits)
{
if (h.collider != null && (h.collider.gameObject == gameObject || h.collider.gameObject.name == gameObject.name))
{
OnClicked(); return;
}
}
}
}
void OnClicked()
void OnMouseDown()
{
if (_clicked) return;
_clicked = true;
Debug.Log($"[ShoseClick] ★ {gameObject.name} 被点击了 ★");
Debug.Log("[ShoseClick] ★ 模型被点击了! ★");
StopAllCoroutines();
if (_outlineMesh != null) _outlineMesh.enabled = false;
if (targetPoint != null) { transform.position = targetPoint.position; transform.rotation = targetPoint.rotation; Debug.Log($"[ShoseClick] 移动到 {targetPoint.name}"); }
else Debug.LogWarning("[ShoseClick] targetPoint 为空");
if (targetPoint != null) { transform.position = targetPoint.position; transform.rotation = targetPoint.rotation; }
if (nextObject != null) nextObject.SetActive(true);
}
}