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-09 06:14:48 +08:00
|
|
|
public bool CanBeClicked() { return _isBlinking; }
|
2026-07-02 16:07:02 +08:00
|
|
|
|
|
|
|
|
void OnEnable()
|
|
|
|
|
{
|
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-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 05:55:44 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 全新的点击检测方式:由一个静态方法统一管理,不再依赖碰撞体。
|
|
|
|
|
/// 在 Update 里,遍历所有活跃的 PingClickHandler,找到离鼠标最近的那个,只触发它。
|
|
|
|
|
/// </summary>
|
2026-07-12 04:46:14 +08:00
|
|
|
void Update()
|
|
|
|
|
{
|
2026-07-12 05:55:44 +08:00
|
|
|
// 只让 pingIndex 最小的那个实例来做全局检测,避免多个实例重复执行
|
|
|
|
|
if (!_isBlinking) return;
|
|
|
|
|
|
|
|
|
|
// 只有一个实例负责全局判断
|
|
|
|
|
var allHandlers = FindObjectsOfType<PingClickHandler>();
|
|
|
|
|
bool iAmFirst = true;
|
|
|
|
|
foreach (var h in allHandlers)
|
2026-07-12 04:46:14 +08:00
|
|
|
{
|
2026-07-12 05:55:44 +08:00
|
|
|
if (!h._isBlinking) continue;
|
|
|
|
|
if (h.GetInstanceID() < this.GetInstanceID())
|
|
|
|
|
{
|
|
|
|
|
iAmFirst = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!iAmFirst) return;
|
|
|
|
|
|
|
|
|
|
if (!Input.GetMouseButtonDown(0)) return;
|
|
|
|
|
|
|
|
|
|
// 不在 UI 上才检测
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 遍历所有正在闪烁的 ping,找到距离鼠标最近的那个
|
|
|
|
|
float bestDist = float.MaxValue;
|
|
|
|
|
PingClickHandler bestPing = null;
|
|
|
|
|
|
|
|
|
|
Vector2 mousePos = Input.mousePosition;
|
|
|
|
|
|
|
|
|
|
foreach (var h in allHandlers)
|
|
|
|
|
{
|
|
|
|
|
if (!h._isBlinking) continue;
|
|
|
|
|
if (!h.gameObject.activeInHierarchy) continue;
|
2026-07-12 04:46:14 +08:00
|
|
|
|
2026-07-12 05:55:44 +08:00
|
|
|
Vector3 screenPos = activeCamera.WorldToScreenPoint(h.transform.position);
|
2026-07-12 05:52:29 +08:00
|
|
|
|
2026-07-12 05:55:44 +08:00
|
|
|
// 如果在相机背后,跳过
|
|
|
|
|
if (screenPos.z < 0) continue;
|
|
|
|
|
|
|
|
|
|
float dist = Vector2.Distance(mousePos, new Vector2(screenPos.x, screenPos.y));
|
|
|
|
|
if (dist < bestDist)
|
2026-07-12 04:46:14 +08:00
|
|
|
{
|
2026-07-12 05:55:44 +08:00
|
|
|
bestDist = dist;
|
|
|
|
|
bestPing = h;
|
2026-07-12 04:46:14 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-07-12 05:55:44 +08:00
|
|
|
|
|
|
|
|
// 点击半径阈值:80像素以内算点中(可以根据需要调整)
|
|
|
|
|
if (bestPing != null && bestDist < 80f)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"[PingClick] 屏幕坐标命中: {bestPing.name} (距离={bestDist:F1}px)");
|
|
|
|
|
bestPing._isBlinking = false;
|
|
|
|
|
bestPing.PerformClick();
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
var remainingPings = Object.FindObjectsOfType<PingClickHandler>();
|
|
|
|
|
int activePingCount = 0;
|
|
|
|
|
foreach (var p in remainingPings)
|
2026-07-12 05:55:44 +08:00
|
|
|
{
|
|
|
|
|
if (p._isBlinking || p._modelShown) activePingCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 自己刚刚隐藏了,所以要减 1 看看还剩几个
|
|
|
|
|
// 其实更准确的是检查 collider enabled,但现在我们不用 collider 了
|
|
|
|
|
// 直接看有没有剩余的活跃 ping
|
|
|
|
|
bool allDone = true;
|
|
|
|
|
foreach (var p in Object.FindObjectsOfType<PingClickHandler>())
|
|
|
|
|
{
|
|
|
|
|
var c = p.GetComponent<Collider>();
|
|
|
|
|
// 如果还有没被隐藏的 ping(依然可见),就说明没完
|
|
|
|
|
if (p.gameObject.activeInHierarchy && p._modelShown && !p._isBlinking)
|
|
|
|
|
{
|
|
|
|
|
// 这个 ping 已经完成了第一阶段但还没进入第二阶段
|
|
|
|
|
// 不算完成
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 简单判定:遍历所有的 ping,看看是否还有 collider enabled 的
|
|
|
|
|
var pings = Object.FindObjectsOfType<PingClickHandler>();
|
|
|
|
|
int remaining = 0;
|
|
|
|
|
foreach (var p in pings)
|
2026-07-08 09:38:42 +08:00
|
|
|
{
|
2026-07-10 02:51:52 +08:00
|
|
|
var col = p.GetComponent<Collider>();
|
2026-07-12 05:55:44 +08:00
|
|
|
if (col != null && col.enabled) remaining++;
|
2026-07-08 09:38:42 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-12 05:55:44 +08:00
|
|
|
if (remaining == 0)
|
2026-07-08 09:38:42 +08:00
|
|
|
{
|
2026-07-12 03:43:07 +08:00
|
|
|
Debug.Log("[PingClick] 所有的 ping 都已经绑成 chanrao 了!播放完成语音。");
|
|
|
|
|
var am = FindObjectOfType<AudioManager>();
|
|
|
|
|
if (am != null)
|
2026-07-08 09:38:42 +08:00
|
|
|
{
|
2026-07-12 03:43:07 +08:00
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
var finishClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>("Assets/Audio/people/伤员一切正常,包扎完毕.mp3");
|
|
|
|
|
if (finishClip != null) am.PlayVoice(finishClip);
|
|
|
|
|
#endif
|
2026-07-08 09:38:42 +08:00
|
|
|
}
|
2026-07-10 03:50:39 +08:00
|
|
|
|
2026-07-10 05:01:29 +08:00
|
|
|
var uiMgr = FindObjectOfType<UIManager>();
|
|
|
|
|
if (uiMgr != null)
|
2026-07-10 04:41:26 +08:00
|
|
|
{
|
2026-07-10 05:01:29 +08:00
|
|
|
uiMgr.HideVideo();
|
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 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;
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
_modelShown = false;
|
2026-07-12 03:47:42 +08:00
|
|
|
|
2026-07-09 06:14:48 +08:00
|
|
|
_isBlinking = true;
|
2026-07-08 09:38:42 +08:00
|
|
|
if (_modelRenderer != null) _modelRenderer.enabled = false;
|
|
|
|
|
foreach (var r in GetComponentsInChildren<Renderer>(true)) { if (_outlineMesh != null && r == _outlineMesh) continue; r.enabled = false; }
|
|
|
|
|
if (_outlineMesh != null) _outlineMesh.enabled = true;
|
|
|
|
|
StopAllCoroutines();
|
|
|
|
|
StartCoroutine(BlinkOutline());
|
|
|
|
|
}
|
2026-07-06 04:52:27 +08:00
|
|
|
}
|