using UnityEngine; public class ShoseClickHandler : MonoBehaviour { public GameObject nextObject; public Transform targetPoint; private MeshRenderer _outlineMesh; private bool _clicked; void OnEnable() { var col = GetComponent(); if (col == null) col = gameObject.AddComponent(); 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}"); } void CreateOutlineMesh() { if (_outlineMesh != null) return; var mf = GetComponent(); if (mf == null) mf = GetComponentInChildren(); if (mf == null || mf.sharedMesh == null) return; var shader = Shader.Find("Custom/OutlineUnlit"); if (shader == null) return; var mat = new Material(shader); mat.SetColor("_OutlineColor", new Color(0.2f, 0.5f, 1f, 1f)); mat.SetColor("_MainColor", new Color(0, 0, 0, 0)); mat.SetFloat("_OutlineWidth", 0.06f); var go = new GameObject("Outline"); go.transform.SetParent(transform, false); go.AddComponent().sharedMesh = mf.sharedMesh; _outlineMesh = go.AddComponent(); _outlineMesh.material = mat; } System.Collections.IEnumerator BlinkOutline() { if (_outlineMesh != null) _outlineMesh.enabled = true; yield return new WaitForSeconds(0.4f); 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(); 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() { _clicked = true; Debug.Log($"[ShoseClick] ★ {gameObject.name} 被点击了 ★"); 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 (nextObject != null) nextObject.SetActive(true); } }