using UnityEngine; public class ChanraoTuibuHandler : MonoBehaviour { public GameObject nextObject; private MeshRenderer _outlineMesh; private float _lastClickTime; private bool _isFinished; public bool CanBeClicked() { return !_isFinished; } void OnEnable() { if (GetComponent() == null) gameObject.AddComponent().radius = 0.5f; foreach (var r in GetComponentsInChildren(true)) r.enabled = false; CreateOutlineMesh(); _isFinished = false; StopAllCoroutines(); StartCoroutine(BlinkOutline()); Debug.Log($"[ChanraoTuibu] OnEnable outline={_outlineMesh!=null}"); } void CreateOutlineMesh() { if (_outlineMesh != null) return; var mf = GetComponent(); if (mf == null) mf = GetComponentInChildren(); Mesh mesh = null; if (mf != null && mf.sharedMesh != null) { mesh = mf.sharedMesh; } else { var smr = GetComponent(); if (smr == null) smr = GetComponentInChildren(); if (smr != null && smr.sharedMesh != null) mesh = smr.sharedMesh; } 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; } // 显示模型 // 如果由于意外 nextObject 丢失了,手动把它指向 ping.004 if (nextObject == null) { var mi = GameObject.Find("manInjury"); if (mi != null) { var p004 = mi.transform.Find("ping.004"); if (p004 != null) nextObject = p004.gameObject; } } // 激活下一个(ping.004) if (nextObject != null) { nextObject.SetActive(true); Debug.Log($"[ChanraoTuibu] → {nextObject.name}"); } else Debug.LogError("[ChanraoTuibu] 致命错误:nextObject 依然是 null!找不到 ping.004"); } public void ResetState() { _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 = true; if (nextObject != null) { nextObject.SetActive(false); var p = nextObject.GetComponent(); if (p != null) p.UndoShow(); } StopAllCoroutines(); StartCoroutine(BlinkOutline()); } }