using UnityEngine; public class ShoseClickHandler : MonoBehaviour { public GameObject nextObject; public Transform targetPoint; private MeshRenderer _outlineMesh; private bool _clicked; private float _lastClickTime; public bool CanBeClicked() { return !_clicked; } private Vector3 _initialPosition; private Quaternion _initialRotation; private bool _hasRecordedInitialTransform = false; void OnDisable() { StopAllCoroutines(); if (_outlineMesh != null) _outlineMesh.enabled = false; } void OnEnable() { var mf = GetComponent(); if (mf == null) mf = GetComponentInChildren(); if (mf != null && mf.sharedMesh != null) { var mb = mf.sharedMesh.bounds; var sc = GetComponent(); if(sc != null) sc.enabled = false; var col = GetComponent(); if (col == null) col = gameObject.AddComponent(); col.center = mb.center; col.size = mb.size * 1.2f; col.enabled = true; } else { if (GetComponent() == null) gameObject.AddComponent().radius = 0.3f; } if (!_hasRecordedInitialTransform) { _initialPosition = transform.position; _initialRotation = transform.rotation; _hasRecordedInitialTransform = true; } CreateOutlineMesh(); StopAllCoroutines(); bool isExam = GameManager.Instance != null && GameManager.Instance.CurrentMode == ExperimentMode.Exam; if (!isExam) { 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)); mat.SetFloat("_OutlineWidth", 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() { PerformClick(); } public void PerformClick() { if (Time.time - _lastClickTime < 0.3f) return; _lastClickTime = Time.time; if (_clicked) return; _clicked = true; Debug.Log("[ShoseClick] ★ 被点击 ★"); ActionHistory.Push(ActionHistory.ActionType.Shose, this); var examMgr = FindObjectOfType(); if (examMgr != null) examMgr.OnObjectClicked(this); StopAllCoroutines(); if (_outlineMesh != null) _outlineMesh.enabled = false; if (targetPoint != null) { transform.position = targetPoint.position; transform.rotation = targetPoint.rotation; } if (nextObject != null) nextObject.SetActive(true); var woman = GameObject.Find("woman"); if (woman != null) { var anim = woman.GetComponent(); if (anim != null) { anim.SetBool("isCrouch", true); anim.CrossFade("womanCrouch", 0.1f); } var point3 = GameObject.Find("womanPointArray/point3"); if (point3 != null) { woman.transform.position = point3.transform.position; woman.transform.rotation = point3.transform.rotation; } } var manInjury = GameObject.Find("manInjury"); if (manInjury != null) { var anim = manInjury.GetComponent(); if (anim != null) { anim.SetBool("CheckFoot", true); anim.CrossFade("manInjuryFoot", 0.1f); StartCoroutine(ResetCheckFoot(anim)); } } var touchArray = GameObject.Find("touchArray"); if (touchArray != null) { var touchPoint3 = touchArray.transform.Find("point3"); if (touchPoint3 != null) { touchPoint3.gameObject.SetActive(false); } } var uiMgr = FindObjectOfType(); if (uiMgr != null) { uiMgr.ShowVideo("bandage_demo.mp4"); } } public void ResetState() { ResetState(true); } public void ResetState(bool startBlink) { _clicked = false; if (_outlineMesh != null) { bool isExam = GameManager.Instance != null && GameManager.Instance.CurrentMode == ExperimentMode.Exam; _outlineMesh.enabled = startBlink && !isExam; } if (_hasRecordedInitialTransform) { transform.position = _initialPosition; transform.rotation = _initialRotation; } if (nextObject != null) nextObject.SetActive(false); var uiMgr = FindObjectOfType(); if (uiMgr != null) { uiMgr.HideVideo(); } var woman = GameObject.Find("woman"); if (woman != null) { var anim = woman.GetComponent(); if (anim != null) { anim.SetBool("isCrouch", false); } var point1 = GameObject.Find("womanPointArray/point1"); if (point1 != null) { woman.transform.position = point1.transform.position; woman.transform.rotation = point1.transform.rotation; } } var manInjury = GameObject.Find("manInjury"); if (manInjury != null) { var anim = manInjury.GetComponent(); if (anim != null) { anim.SetBool("CheckFoot", false); } } var touchArray = GameObject.Find("touchArray"); if (touchArray != null) { var touchPoint3 = touchArray.transform.Find("point3"); if (touchPoint3 != null) { touchPoint3.gameObject.SetActive(true); } } StopAllCoroutines(); bool examMode = GameManager.Instance != null && GameManager.Instance.CurrentMode == ExperimentMode.Exam; if (startBlink && isActiveAndEnabled && !examMode) { StartCoroutine(BlinkOutline()); } } public void ForceHideOutline() { StopAllCoroutines(); if (_outlineMesh != null) _outlineMesh.enabled = false; } private System.Collections.IEnumerator ResetCheckFoot(Animator anim) { yield return new WaitForSeconds(3.5f); if (anim != null) { anim.SetBool("CheckFoot", false); Debug.Log("[ShoseClick] CheckFoot 已被重置为 false"); } } }