2026-07-02 16:07:02 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class PingClickHandler : MonoBehaviour
|
|
|
|
|
{
|
2026-07-05 02:53:50 +08:00
|
|
|
public GameObject chanraoObject, nextPing;
|
2026-07-03 00:59:20 +08:00
|
|
|
public int pingIndex;
|
2026-07-05 02:53:50 +08:00
|
|
|
private MeshRenderer _modelRenderer, _outlineMesh;
|
2026-07-06 04:49:03 +08:00
|
|
|
private bool _modelShown;
|
2026-07-09 06:14:48 +08:00
|
|
|
private bool _isBlinking;
|
2026-07-08 09:38:42 +08:00
|
|
|
|
2026-07-12 06:03:55 +08:00
|
|
|
// 全局静态变量,防止同一帧内有多个 PingClickHandler 被同时触发
|
|
|
|
|
private static int _lastClickedFrame = -1;
|
|
|
|
|
// 实例变量,记录自己被启用(或重新闪烁)的那一帧
|
|
|
|
|
private int _enableFrame = -1;
|
|
|
|
|
|
2026-07-12 07:03:36 +08:00
|
|
|
// 添加一个全局静态标志位,记录“语音是否已经被播放过了”,防止重复播放
|
|
|
|
|
private static bool _hasPlayedFinishVoice = false;
|
|
|
|
|
|
2026-07-09 06:14:48 +08:00
|
|
|
public bool CanBeClicked() { return _isBlinking; }
|
2026-07-02 16:07:02 +08:00
|
|
|
|
|
|
|
|
void OnEnable()
|
|
|
|
|
{
|
2026-07-12 06:03:55 +08:00
|
|
|
_enableFrame = Time.frameCount;
|
|
|
|
|
|
2026-07-12 07:03:36 +08:00
|
|
|
// 每次重新启用时(如果是第一轮的第一步),重置语音播放标志位
|
|
|
|
|
// 通过判断是否是第一轮的第一个 ping,或者直接在第一轮就重置
|
|
|
|
|
if (name == "ping.004" && !_modelShown)
|
|
|
|
|
{
|
|
|
|
|
_hasPlayedFinishVoice = false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-05 02:53:50 +08:00
|
|
|
_modelRenderer = GetComponent<MeshRenderer>();
|
|
|
|
|
if (_modelRenderer == null) _modelRenderer = GetComponentInChildren<MeshRenderer>();
|
|
|
|
|
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = false;
|
2026-07-12 03:43:07 +08:00
|
|
|
|
2026-07-12 07:03:36 +08:00
|
|
|
var colliders = GetComponents<Collider>();
|
|
|
|
|
foreach (var c in colliders)
|
|
|
|
|
{
|
|
|
|
|
if (c is BoxCollider || c is CapsuleCollider)
|
|
|
|
|
{
|
|
|
|
|
Destroy(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sphere = GetComponent<SphereCollider>();
|
|
|
|
|
if (sphere == null) sphere = gameObject.AddComponent<SphereCollider>();
|
|
|
|
|
|
|
|
|
|
float maxScale = Mathf.Max(Mathf.Abs(transform.lossyScale.x), Mathf.Abs(transform.lossyScale.y), Mathf.Abs(transform.lossyScale.z));
|
|
|
|
|
if (maxScale < 0.001f) maxScale = 1f;
|
|
|
|
|
|
|
|
|
|
sphere.radius = 0.15f / maxScale;
|
|
|
|
|
sphere.center = Vector3.zero;
|
|
|
|
|
sphere.isTrigger = false;
|
|
|
|
|
sphere.enabled = true;
|
|
|
|
|
|
2026-07-05 02:53:50 +08:00
|
|
|
CreateOutlineMesh();
|
2026-07-06 04:49:03 +08:00
|
|
|
_modelShown = false;
|
2026-07-09 06:14:48 +08:00
|
|
|
_isBlinking = true;
|
2026-07-02 16:07:02 +08:00
|
|
|
StopAllCoroutines();
|
|
|
|
|
StartCoroutine(BlinkOutline());
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-05 02:53:50 +08:00
|
|
|
void CreateOutlineMesh()
|
2026-07-02 16:22:17 +08:00
|
|
|
{
|
2026-07-05 02:53:50 +08:00
|
|
|
if (_outlineMesh != null) return;
|
|
|
|
|
var mf = GetComponent<MeshFilter>();
|
|
|
|
|
if (mf == null) mf = GetComponentInChildren<MeshFilter>();
|
|
|
|
|
if (mf == null || mf.sharedMesh == null) return;
|
2026-07-06 04:49:03 +08:00
|
|
|
var shader = Shader.Find("Custom/OutlineUnlit");
|
|
|
|
|
if (shader == null) return;
|
|
|
|
|
var mat = new Material(shader);
|
2026-07-05 02:53:50 +08:00
|
|
|
mat.SetColor("_OutlineColor", new Color(0.2f, 0.5f, 1f, 1f));
|
|
|
|
|
mat.SetColor("_MainColor", new Color(0, 0, 0, 0));
|
2026-07-08 09:38:42 +08:00
|
|
|
mat.SetFloat("_OutlineWidth", OutlineConfig.Width);
|
2026-07-05 02:53:50 +08:00
|
|
|
var go = new GameObject("Outline");
|
|
|
|
|
go.transform.SetParent(transform, false);
|
2026-07-08 09:38:42 +08:00
|
|
|
go.transform.position += Vector3.up * 0.05f;
|
2026-07-05 02:53:50 +08:00
|
|
|
go.AddComponent<MeshFilter>().sharedMesh = mf.sharedMesh;
|
|
|
|
|
_outlineMesh = go.AddComponent<MeshRenderer>();
|
|
|
|
|
_outlineMesh.material = mat;
|
2026-07-02 16:22:17 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-02 16:07:02 +08:00
|
|
|
System.Collections.IEnumerator BlinkOutline()
|
|
|
|
|
{
|
2026-07-05 02:53:50 +08:00
|
|
|
if (_outlineMesh != null) _outlineMesh.enabled = true;
|
2026-07-05 02:32:58 +08:00
|
|
|
yield return new WaitForSeconds(0.4f);
|
2026-07-06 04:49:03 +08:00
|
|
|
while (true) { if (_outlineMesh != null) _outlineMesh.enabled = !_outlineMesh.enabled; yield return new WaitForSeconds(0.4f); }
|
2026-07-02 16:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-12 04:46:14 +08:00
|
|
|
void Update()
|
|
|
|
|
{
|
2026-07-12 05:55:44 +08:00
|
|
|
if (!_isBlinking) return;
|
|
|
|
|
if (!Input.GetMouseButtonDown(0)) return;
|
|
|
|
|
|
2026-07-12 06:03:55 +08:00
|
|
|
if (Time.frameCount == _enableFrame) return;
|
|
|
|
|
if (Time.frameCount == _lastClickedFrame) return;
|
|
|
|
|
|
2026-07-12 05:55:44 +08:00
|
|
|
if (UnityEngine.EventSystems.EventSystem.current != null &&
|
|
|
|
|
UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Camera activeCamera = null;
|
|
|
|
|
foreach (var c in Camera.allCameras)
|
|
|
|
|
{
|
|
|
|
|
if (c.enabled && c.gameObject.activeInHierarchy)
|
|
|
|
|
{
|
|
|
|
|
activeCamera = c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (activeCamera == null) return;
|
|
|
|
|
|
2026-07-12 05:58:22 +08:00
|
|
|
Vector3 screenPos = activeCamera.WorldToScreenPoint(transform.position);
|
2026-07-12 07:03:36 +08:00
|
|
|
if (screenPos.z < 0) return;
|
2026-07-12 05:55:44 +08:00
|
|
|
|
|
|
|
|
Vector2 mousePos = Input.mousePosition;
|
2026-07-12 05:58:22 +08:00
|
|
|
float myDist = Vector2.Distance(mousePos, new Vector2(screenPos.x, screenPos.y));
|
2026-07-12 05:55:44 +08:00
|
|
|
|
2026-07-12 05:58:22 +08:00
|
|
|
var allHandlers = FindObjectsOfType<PingClickHandler>();
|
2026-07-12 05:55:44 +08:00
|
|
|
foreach (var h in allHandlers)
|
|
|
|
|
{
|
2026-07-12 05:58:22 +08:00
|
|
|
if (h == this) continue;
|
2026-07-12 05:55:44 +08:00
|
|
|
if (!h._isBlinking) continue;
|
|
|
|
|
if (!h.gameObject.activeInHierarchy) continue;
|
2026-07-12 04:46:14 +08:00
|
|
|
|
2026-07-12 05:58:22 +08:00
|
|
|
Vector3 otherScreen = activeCamera.WorldToScreenPoint(h.transform.position);
|
|
|
|
|
if (otherScreen.z < 0) continue;
|
2026-07-12 05:55:44 +08:00
|
|
|
|
2026-07-12 05:58:22 +08:00
|
|
|
float otherDist = Vector2.Distance(mousePos, new Vector2(otherScreen.x, otherScreen.y));
|
2026-07-12 07:03:36 +08:00
|
|
|
if (otherDist < myDist) return;
|
2026-07-12 04:46:14 +08:00
|
|
|
}
|
2026-07-12 05:55:44 +08:00
|
|
|
|
2026-07-12 07:03:36 +08:00
|
|
|
if (myDist < 120f)
|
2026-07-12 05:55:44 +08:00
|
|
|
{
|
2026-07-12 05:58:22 +08:00
|
|
|
Debug.Log($"[PingClick] 屏幕坐标命中: {name} (距离={myDist:F1}px)");
|
2026-07-12 07:03:36 +08:00
|
|
|
_lastClickedFrame = Time.frameCount;
|
2026-07-12 05:58:22 +08:00
|
|
|
_isBlinking = false;
|
|
|
|
|
PerformClick();
|
2026-07-12 05:55:44 +08:00
|
|
|
}
|
2026-07-12 04:46:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-08 09:38:42 +08:00
|
|
|
public void PerformClick()
|
2026-07-02 16:07:02 +08:00
|
|
|
{
|
2026-07-06 04:49:03 +08:00
|
|
|
if (!_modelShown)
|
2026-07-02 16:07:02 +08:00
|
|
|
{
|
2026-07-06 04:49:03 +08:00
|
|
|
_modelShown = true;
|
2026-07-09 06:14:48 +08:00
|
|
|
_isBlinking = false;
|
2026-07-09 03:16:00 +08:00
|
|
|
ActionHistory.Push(ActionHistory.ActionType.PingFirst, this);
|
2026-07-02 16:22:17 +08:00
|
|
|
StopAllCoroutines();
|
2026-07-05 02:53:50 +08:00
|
|
|
if (_modelRenderer != null) _modelRenderer.enabled = true;
|
2026-07-06 04:49:03 +08:00
|
|
|
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = true;
|
2026-07-05 02:53:50 +08:00
|
|
|
if (_outlineMesh != null) _outlineMesh.enabled = false;
|
|
|
|
|
if (nextPing != null) nextPing.SetActive(true);
|
2026-07-08 09:38:42 +08:00
|
|
|
else
|
|
|
|
|
{
|
2026-07-12 03:43:07 +08:00
|
|
|
var chanraotuibu = GameObject.Find("chanraotuibu");
|
|
|
|
|
if (chanraotuibu == null)
|
2026-07-08 09:38:42 +08:00
|
|
|
{
|
2026-07-12 03:43:07 +08:00
|
|
|
var mi = GameObject.Find("manInjury");
|
|
|
|
|
if (mi != null)
|
2026-07-09 18:28:08 +08:00
|
|
|
{
|
2026-07-12 03:43:07 +08:00
|
|
|
var t = mi.transform.Find("chanraotuibu");
|
|
|
|
|
if (t != null) chanraotuibu = t.gameObject;
|
2026-07-09 18:28:08 +08:00
|
|
|
}
|
2026-07-08 09:38:42 +08:00
|
|
|
}
|
2026-07-12 03:43:07 +08:00
|
|
|
if (chanraotuibu != null) chanraotuibu.SetActive(true);
|
|
|
|
|
Debug.Log($"[PingClick] {name} 是最后一个 ping 模型,现已激活 chanraotuibu");
|
2026-07-08 09:38:42 +08:00
|
|
|
}
|
2026-07-09 18:28:08 +08:00
|
|
|
|
|
|
|
|
if (nextPing != null) Debug.Log($"[PingClick] {name} 模型显示, 激活 {nextPing.name}");
|
|
|
|
|
else Debug.Log($"[PingClick] {name} 模型显示, 激活下一个");
|
|
|
|
|
|
2026-07-06 04:49:03 +08:00
|
|
|
var sm = FindObjectOfType<StepManager>();
|
|
|
|
|
if (sm != null) sm.RecordPingStep(pingIndex, false);
|
2026-07-02 16:07:02 +08:00
|
|
|
}
|
2026-07-02 16:22:17 +08:00
|
|
|
else
|
|
|
|
|
{
|
2026-07-06 04:49:03 +08:00
|
|
|
if (chanraoObject != null) { chanraoObject.SetActive(true); Debug.Log($"[PingClick] {name} → {chanraoObject.name}"); }
|
2026-07-09 03:16:00 +08:00
|
|
|
HideSelf();
|
|
|
|
|
ActionHistory.Push(ActionHistory.ActionType.PingSecond, this);
|
2026-07-06 04:49:03 +08:00
|
|
|
var sm = FindObjectOfType<StepManager>();
|
|
|
|
|
if (sm != null) sm.RecordPingStep(pingIndex, true);
|
2026-07-08 09:38:42 +08:00
|
|
|
|
2026-07-12 05:55:44 +08:00
|
|
|
var pings = Object.FindObjectsOfType<PingClickHandler>();
|
|
|
|
|
int remaining = 0;
|
|
|
|
|
foreach (var p in pings)
|
2026-07-08 09:38:42 +08:00
|
|
|
{
|
2026-07-12 05:58:22 +08:00
|
|
|
if (p.gameObject.activeInHierarchy && (p._isBlinking || p._modelShown))
|
|
|
|
|
remaining++;
|
2026-07-08 09:38:42 +08:00
|
|
|
}
|
2026-07-12 07:03:36 +08:00
|
|
|
|
2026-07-12 12:17:06 +08:00
|
|
|
if (remaining == 0)
|
2026-07-08 09:38:42 +08:00
|
|
|
{
|
2026-07-12 07:03:36 +08:00
|
|
|
if (!_hasPlayedFinishVoice)
|
2026-07-08 09:38:42 +08:00
|
|
|
{
|
2026-07-12 07:03:36 +08:00
|
|
|
_hasPlayedFinishVoice = true;
|
|
|
|
|
Debug.Log("[PingClick] 所有的 ping 都已经绑成 chanrao 了!播放完成语音。");
|
|
|
|
|
var am = FindObjectOfType<AudioManager>();
|
|
|
|
|
if (am != null)
|
|
|
|
|
{
|
2026-07-12 03:43:07 +08:00
|
|
|
#if UNITY_EDITOR
|
2026-07-12 07:03:36 +08:00
|
|
|
var finishClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>("Assets/Audio/people/伤员一切正常,包扎完毕.mp3");
|
|
|
|
|
if (finishClip != null) am.PlayVoice(finishClip);
|
2026-07-12 03:43:07 +08:00
|
|
|
#endif
|
2026-07-12 07:03:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var uiMgr = FindObjectOfType<UIManager>();
|
|
|
|
|
if (uiMgr != null)
|
|
|
|
|
{
|
|
|
|
|
uiMgr.HideVideo();
|
2026-07-12 12:17:06 +08:00
|
|
|
uiMgr.ShowEndPanel();
|
2026-07-12 07:03:36 +08:00
|
|
|
}
|
2026-07-10 03:50:39 +08:00
|
|
|
}
|
2026-07-08 09:38:42 +08:00
|
|
|
}
|
2026-07-05 02:30:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-09 18:28:08 +08:00
|
|
|
public void ReEnableBorder()
|
|
|
|
|
{
|
2026-07-12 03:45:35 +08:00
|
|
|
if (!_modelShown) return;
|
|
|
|
|
|
|
|
|
|
if (chanraoObject != null && chanraoObject.activeSelf)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-07-09 18:28:08 +08:00
|
|
|
|
|
|
|
|
_isBlinking = true;
|
2026-07-12 07:03:36 +08:00
|
|
|
_enableFrame = Time.frameCount;
|
2026-07-12 03:43:07 +08:00
|
|
|
|
2026-07-09 18:28:08 +08:00
|
|
|
if (_outlineMesh != null)
|
|
|
|
|
{
|
|
|
|
|
_outlineMesh.enabled = true;
|
|
|
|
|
StopAllCoroutines();
|
|
|
|
|
StartCoroutine(BlinkOutline());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DisableBorder()
|
|
|
|
|
{
|
|
|
|
|
_isBlinking = false;
|
|
|
|
|
if (_outlineMesh != null) _outlineMesh.enabled = false;
|
|
|
|
|
StopAllCoroutines();
|
|
|
|
|
}
|
2026-07-09 06:14:48 +08:00
|
|
|
|
2026-07-08 09:38:42 +08:00
|
|
|
public void UndoSecondClick()
|
|
|
|
|
{
|
2026-07-09 03:41:50 +08:00
|
|
|
if (chanraoObject != null) chanraoObject.SetActive(false);
|
2026-07-09 03:16:00 +08:00
|
|
|
ShowSelf();
|
2026-07-08 09:38:42 +08:00
|
|
|
_modelShown = true;
|
2026-07-09 06:14:48 +08:00
|
|
|
_isBlinking = true;
|
2026-07-12 07:03:36 +08:00
|
|
|
_enableFrame = Time.frameCount;
|
|
|
|
|
|
|
|
|
|
_hasPlayedFinishVoice = false;
|
|
|
|
|
|
2026-07-09 06:14:48 +08:00
|
|
|
if (_outlineMesh != null) _outlineMesh.enabled = true;
|
|
|
|
|
StopAllCoroutines();
|
|
|
|
|
StartCoroutine(BlinkOutline());
|
2026-07-09 03:16:00 +08:00
|
|
|
}
|
2026-07-10 02:11:11 +08:00
|
|
|
public void UndoShow()
|
|
|
|
|
{
|
|
|
|
|
_modelShown = false;
|
|
|
|
|
_isBlinking = false;
|
|
|
|
|
StopAllCoroutines();
|
|
|
|
|
if (chanraoObject != null) chanraoObject.SetActive(false);
|
|
|
|
|
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = false;
|
|
|
|
|
gameObject.SetActive(false);
|
|
|
|
|
}
|
2026-07-09 03:16:00 +08:00
|
|
|
|
|
|
|
|
private void HideSelf()
|
|
|
|
|
{
|
|
|
|
|
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = false;
|
2026-07-09 06:14:48 +08:00
|
|
|
_isBlinking = false;
|
2026-07-09 02:35:28 +08:00
|
|
|
StopAllCoroutines();
|
2026-07-12 05:55:44 +08:00
|
|
|
gameObject.SetActive(false);
|
2026-07-08 09:38:42 +08:00
|
|
|
}
|
2026-07-09 03:16:00 +08:00
|
|
|
|
|
|
|
|
private void ShowSelf()
|
|
|
|
|
{
|
|
|
|
|
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = true;
|
|
|
|
|
if (_outlineMesh != null) _outlineMesh.enabled = false;
|
|
|
|
|
}
|
2026-07-08 09:38:42 +08:00
|
|
|
|
|
|
|
|
public void ResetState()
|
2026-07-12 12:17:06 +08:00
|
|
|
{
|
|
|
|
|
ResetState(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetState(bool startBlink)
|
2026-07-08 09:38:42 +08:00
|
|
|
{
|
|
|
|
|
_modelShown = false;
|
2026-07-12 12:17:06 +08:00
|
|
|
_isBlinking = startBlink;
|
2026-07-12 07:03:36 +08:00
|
|
|
_enableFrame = Time.frameCount;
|
|
|
|
|
|
|
|
|
|
_hasPlayedFinishVoice = false;
|
|
|
|
|
|
2026-07-08 09:38:42 +08:00
|
|
|
if (_modelRenderer != null) _modelRenderer.enabled = false;
|
2026-07-12 12:17:06 +08:00
|
|
|
if (chanraoObject != null) chanraoObject.SetActive(false);
|
2026-07-08 09:38:42 +08:00
|
|
|
foreach (var r in GetComponentsInChildren<Renderer>(true)) { if (_outlineMesh != null && r == _outlineMesh) continue; r.enabled = false; }
|
2026-07-12 12:17:06 +08:00
|
|
|
if (_outlineMesh != null) _outlineMesh.enabled = startBlink;
|
2026-07-08 09:38:42 +08:00
|
|
|
StopAllCoroutines();
|
2026-07-12 12:17:06 +08:00
|
|
|
if (startBlink && isActiveAndEnabled)
|
|
|
|
|
{
|
|
|
|
|
StartCoroutine(BlinkOutline());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ForceHideOutline()
|
|
|
|
|
{
|
|
|
|
|
_isBlinking = false;
|
|
|
|
|
StopAllCoroutines();
|
|
|
|
|
if (_outlineMesh != null) _outlineMesh.enabled = false;
|
2026-07-08 09:38:42 +08:00
|
|
|
}
|
2026-07-06 04:52:27 +08:00
|
|
|
}
|