using UnityEngine; public class ShoseClickHandler : MonoBehaviour { public GameObject nextObject; public Transform targetPoint; private MeshRenderer _outlineMesh; private bool _clicked; void OnEnable() { var mf = GetComponent(); if (mf == null) mf = GetComponentInChildren(); if (mf != null && mf.sharedMesh != null) { var mb = mf.sharedMesh.bounds; Destroy(GetComponent()); var col = GetComponent(); if (col == null) col = gameObject.AddComponent(); // 按 localScale 缩放 collider 匹配模型实际大小 col.center = Vector3.Scale(mb.center, transform.localScale); col.size = Vector3.Scale(mb.size, transform.localScale) * 1.2f; col.enabled = true; } else { if (GetComponent() == null) gameObject.AddComponent().radius = 0.3f; } CreateOutlineMesh(); StopAllCoroutines(); StartCoroutine(BlinkOutline()); _clicked = false; } 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)); // 描边宽度补偿 scale float s = transform.lossyScale.magnitude / Mathf.Sqrt(3); mat.SetFloat("_OutlineWidth", s > 0.001f ? OutlineConfig.Width / s : OutlineConfig.Width); 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() { 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; } if (nextObject != null) nextObject.SetActive(true); } }