using UnityEngine; public class ShoseClickHandler : MonoBehaviour { public GameObject nextObject; public Transform targetPoint; private MeshRenderer _outlineMesh; private Bounds _meshBounds; private bool _clicked; void Start() { // 用模型的精确 Bounds 设 BoxCollider var mf = GetComponent(); if (mf == null) mf = GetComponentInChildren(); if (mf != null && mf.sharedMesh != null) { _meshBounds = mf.sharedMesh.bounds; var col = GetComponent(); if (col == null) col = gameObject.AddComponent(); // 移除旧 SphereCollider var oldCol = GetComponent(); if (oldCol != null) Destroy(oldCol); col.center = _meshBounds.center; col.size = _meshBounds.size * 1.2f; // 稍微扩大一点好点 col.enabled = true; Debug.Log($"[ShoseClick] BoxCollider center={col.center} size={col.size} bounds={_meshBounds}"); } else { // fallback var col = gameObject.AddComponent(); col.radius = 3f; col.enabled = true; Debug.LogWarning("[ShoseClick] 无MeshFilter,用SphereCollider fallback"); } CreateOutlineMesh(); StartCoroutine(BlinkOutline()); Debug.Log($"[ShoseClick] Start done. obj={gameObject.name} pos={transform.position} scale={transform.lossyScale}"); } 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.1f); 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 OnMouseDown() { Debug.Log($"[ShoseClick] OnMouseDown fired! clicked={_clicked} mousePos={Input.mousePosition}"); if (_clicked) return; _clicked = true; 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} pos={targetPoint.position}"); } if (nextObject != null) nextObject.SetActive(true); } #if UNITY_EDITOR void OnDrawGizmos() { var col = GetComponent(); if (col != null) { Gizmos.color = Color.green; Gizmos.matrix = transform.localToWorldMatrix; Gizmos.DrawWireCube(col.center, col.size); } else { var sc = GetComponent(); if (sc != null) { Gizmos.color = Color.green; Gizmos.DrawWireSphere(transform.position, sc.radius); } } } #endif }