using UnityEngine; public class PingClickHandler : MonoBehaviour { public GameObject chanraoObject, nextPing; public int pingIndex; private MeshRenderer _modelRenderer, _outlineMesh; private bool _modelShown; void OnEnable() { _modelRenderer = GetComponent(); if (_modelRenderer == null) _modelRenderer = GetComponentInChildren(); foreach (var r in GetComponentsInChildren(true)) r.enabled = false; if (GetComponent() == null) gameObject.AddComponent().radius = 0.5f; CreateOutlineMesh(); _modelShown = false; StopAllCoroutines(); StartCoroutine(BlinkOutline()); } 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)); 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() { foreach (var c in Object.FindObjectsOfType()) { if (!c.enabled) continue; foreach (var h in Physics.RaycastAll(c.ScreenPointToRay(Input.mousePosition))) if (h.collider?.transform.parent?.name == "touchArray") return; } if (!_modelShown) { _modelShown = true; StopAllCoroutines(); if (_modelRenderer != null) _modelRenderer.enabled = true; foreach (var r in GetComponentsInChildren(true)) r.enabled = true; if (_outlineMesh != null) _outlineMesh.enabled = false; if (nextPing != null) nextPing.SetActive(true); Debug.Log($"[PingClick] {name} 模型显示, 激活 {nextPing?.name}"); var sm = FindObjectOfType(); if (sm != null) sm.RecordPingStep(pingIndex, false); } else { if (chanraoObject != null) { chanraoObject.SetActive(true); Debug.Log($"[PingClick] {name} → {chanraoObject.name}"); } gameObject.SetActive(false); var sm = FindObjectOfType(); if (sm != null) sm.RecordPingStep(pingIndex, true); } } public void ReEnableBorder() { if (_outlineMesh != null) { _outlineMesh.enabled = true; StopAllCoroutines(); StartCoroutine(BlinkOutline()); } } public void UndoSecondClick() {} public void UndoShow() { gameObject.SetActive(false); } }