using UnityEngine; public class ChanraoTuibuHandler : MonoBehaviour { private MeshRenderer _outlineMesh; private float _lastClickTime; private bool _isFinished; public bool CanBeClicked() { return !_isFinished; } void OnEnable() { // 尝试以 ping.004 为中心生成碰撞体,因为如果它是 SkinnedMeshRenderer,其自身原点可能在脚底 var col = GetComponent(); if (col == null) col = gameObject.AddComponent(); var ping004 = GameObject.Find("ping.004"); if (ping004 == null) { var mi = GameObject.Find("manInjury"); if (mi != null) { var t = mi.transform.Find("ping.004"); if (t != null) ping004 = t.gameObject; } } if (ping004 != null) { col.center = transform.InverseTransformPoint(ping004.transform.position); col.radius = 0.8f; col.enabled = true; Debug.Log($"[ChanraoTuibu] 碰撞体已定位到 ping.004 的位置: {col.center}"); } else { col.center = Vector3.zero; col.radius = 0.8f; } // 不要在代码中直接 Destroy BoxCollider,避免 Unity Editor 抛出异常 var box = GetComponent(); if (box != null) box.enabled = false; foreach (var r in GetComponentsInChildren(true)) r.enabled = false; CreateOutlineMesh(); _isFinished = false; StopAllCoroutines(); StartCoroutine(BlinkOutline()); Debug.Log($"[ChanraoTuibu] OnEnable outline={_outlineMesh!=null}"); } private Mesh GetMesh() { var mf = GetComponent(); if (mf == null) mf = GetComponentInChildren(); if (mf != null && mf.sharedMesh != null) return mf.sharedMesh; var smr = GetComponent(); if (smr == null) smr = GetComponentInChildren(); if (smr != null && smr.sharedMesh != null) return smr.sharedMesh; return null; } void CreateOutlineMesh() { if (_outlineMesh != null) return; Mesh mesh = GetMesh(); if (mesh == null) { Debug.LogWarning("[ChanraoTuibu] 无Mesh数据"); return; } var shader = Shader.Find("Custom/OutlineUnlit"); if (shader == null) { Debug.LogWarning("[ChanraoTuibu] Shader缺失"); 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.AddComponent().sharedMesh = mesh; _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 (Time.time - _lastClickTime < 0.3f) return; // 防止重复点击 _lastClickTime = Time.time; if (_isFinished) return; Debug.Log($"[ChanraoTuibu] {name} 被点击"); ActionHistory.Push(ActionHistory.ActionType.ChanraoTuibu, this); _isFinished = true; StopAllCoroutines(); if (_outlineMesh != null) _outlineMesh.enabled = false; var mr = GetComponent(); if (mr != null) mr.enabled = true; foreach (var r in GetComponentsInChildren(true)) { if (_outlineMesh != null && r == _outlineMesh) continue; r.enabled = true; } // 显示模型 // 激活所有的 ping 进行第二轮包扎 var allPings = Object.FindObjectsOfType(); foreach (var p in allPings) { if (p.gameObject.activeInHierarchy) { p.ReEnableBorder(); } } Debug.Log("[ChanraoTuibu] chanraotuibu 已显示,所有 ping 边框已重新激活!"); } public void ResetState() { ResetState(true); } public void ResetState(bool startBlink) { _isFinished = false; var mr = GetComponent(); if (mr != null) mr.enabled = false; var smr = GetComponent(); if (smr != null) smr.enabled = false; foreach (var r in GetComponentsInChildren(true)) { if (_outlineMesh != null && r == _outlineMesh) continue; r.enabled = false; } if (_outlineMesh != null) _outlineMesh.enabled = startBlink; StopAllCoroutines(); if (startBlink && isActiveAndEnabled) { StartCoroutine(BlinkOutline()); } } public void ForceHideOutline() { StopAllCoroutines(); if (_outlineMesh != null) _outlineMesh.enabled = false; } }