using UnityEngine; public class PingClickHandler : MonoBehaviour { public GameObject chanraoObject, nextPing; public int pingIndex; private MeshRenderer _modelRenderer, _outlineMesh; private bool _modelShown; private bool _isBlinking; public bool CanBeClicked() { return _isBlinking; } void OnEnable() { _modelRenderer = GetComponent(); if (_modelRenderer == null) _modelRenderer = GetComponentInChildren(); foreach (var r in GetComponentsInChildren(true)) r.enabled = false; // 彻底清掉旧的 Collider,重新添加一个绝对靠谱的 SphereCollider var colliders = GetComponents(); foreach(var c in colliders) { Destroy(c); } // 重新添加足够大、形状简单的球形碰撞器 var sphere = gameObject.AddComponent(); // 获取物体本身的渲染边界,如果没有则使用固定值 if (_modelRenderer != null) { // 通过 bounds 的大小来估算一个适合的半径 float maxDim = Mathf.Max(_modelRenderer.bounds.size.x, Mathf.Max(_modelRenderer.bounds.size.y, _modelRenderer.bounds.size.z)); sphere.radius = Mathf.Max(maxDim * 0.6f, 0.5f); } else { sphere.radius = 0.5f; } // 强制碰撞体处于启用状态 sphere.enabled = true; CreateOutlineMesh(); _modelShown = false; _isBlinking = true; 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)); mat.SetFloat("_OutlineWidth", OutlineConfig.Width); var go = new GameObject("Outline"); go.transform.SetParent(transform, false); go.transform.position += Vector3.up * 0.05f; 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() { PerformClick(); } public void PerformClick() { if (!_isBlinking) return; // 防止重复点击 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; _isBlinking = false; ActionHistory.Push(ActionHistory.ActionType.PingFirst, this); 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); else { var chanraotuibu = GameObject.Find("chanraotuibu"); if (chanraotuibu == null) { var mi = GameObject.Find("manInjury"); if (mi != null) { var t = mi.transform.Find("chanraotuibu"); if (t != null) chanraotuibu = t.gameObject; } } if (chanraotuibu != null) chanraotuibu.SetActive(true); Debug.Log($"[PingClick] {name} 是最后一个 ping 模型,现已激活 chanraotuibu"); } if (nextPing != null) Debug.Log($"[PingClick] {name} 模型显示, 激活 {nextPing.name}"); else Debug.Log($"[PingClick] {name} 模型显示, 激活下一个"); var sm = FindObjectOfType(); if (sm != null) sm.RecordPingStep(pingIndex, false); } else { if (chanraoObject != null) { chanraoObject.SetActive(true); Debug.Log($"[PingClick] {name} → {chanraoObject.name}"); } HideSelf(); ActionHistory.Push(ActionHistory.ActionType.PingSecond, this); var sm = FindObjectOfType(); if (sm != null) sm.RecordPingStep(pingIndex, true); var remainingPings = Object.FindObjectsOfType(); int activePingCount = 0; foreach (var p in remainingPings) { var col = p.GetComponent(); if (col != null && col.enabled) activePingCount++; } if (activePingCount == 0) { Debug.Log("[PingClick] 所有的 ping 都已经绑成 chanrao 了!播放完成语音。"); var am = FindObjectOfType(); if (am != null) { #if UNITY_EDITOR var finishClip = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Audio/people/伤员一切正常,包扎完毕.mp3"); if (finishClip != null) am.PlayVoice(finishClip); #endif } var uiMgr = FindObjectOfType(); if (uiMgr != null) { uiMgr.HideVideo(); } } } } public void ReEnableBorder() { if (!_modelShown) return; var col = GetComponent(); if (chanraoObject != null && chanraoObject.activeSelf) { if (col != null) col.enabled = false; return; } _isBlinking = true; if (col != null) col.enabled = true; if (_outlineMesh != null) { _outlineMesh.enabled = true; StopAllCoroutines(); StartCoroutine(BlinkOutline()); } } public void DisableBorder() { _isBlinking = false; if (_outlineMesh != null) _outlineMesh.enabled = false; StopAllCoroutines(); } public void UndoSecondClick() { if (chanraoObject != null) chanraoObject.SetActive(false); ShowSelf(); _modelShown = true; _isBlinking = true; if (_outlineMesh != null) _outlineMesh.enabled = true; StopAllCoroutines(); StartCoroutine(BlinkOutline()); } public void UndoShow() { _modelShown = false; _isBlinking = false; StopAllCoroutines(); if (chanraoObject != null) chanraoObject.SetActive(false); foreach (var r in GetComponentsInChildren(true)) r.enabled = false; var col = GetComponent(); if (col != null) col.enabled = false; gameObject.SetActive(false); } private void HideSelf() { foreach (var r in GetComponentsInChildren(true)) r.enabled = false; var col = GetComponent(); if (col != null) col.enabled = false; _isBlinking = false; StopAllCoroutines(); } private void ShowSelf() { foreach (var r in GetComponentsInChildren(true)) r.enabled = true; if (_outlineMesh != null) _outlineMesh.enabled = false; var col = GetComponent(); if (col != null) col.enabled = true; } public void ResetState() { _modelShown = false; // 【关键修复点:重置状态时确保重新添加或修复 Collider】 var col = GetComponent(); if (col == null) { col = gameObject.AddComponent(); ((SphereCollider)col).radius = 0.5f; } col.enabled = true; _isBlinking = true; if (_modelRenderer != null) _modelRenderer.enabled = false; foreach (var r in GetComponentsInChildren(true)) { if (_outlineMesh != null && r == _outlineMesh) continue; r.enabled = false; } if (_outlineMesh != null) _outlineMesh.enabled = true; StopAllCoroutines(); StartCoroutine(BlinkOutline()); } }