266 lines
9.6 KiB
C#
266 lines
9.6 KiB
C#
using UnityEngine;
|
|
|
|
public class PingClickHandler : MonoBehaviour
|
|
{
|
|
public GameObject chanraoObject, nextPing;
|
|
public int pingIndex;
|
|
private MeshRenderer _modelRenderer, _outlineMesh;
|
|
private bool _modelShown;
|
|
private bool _isBlinking;
|
|
|
|
// 静态变量,记录上一帧被点击的帧号,防止同一帧内多个 Ping 发生连锁反应
|
|
private static int _lastClickedFrame = -1;
|
|
|
|
// 新生成的 ping 在激活后的前2帧不允许被点击,防止被射线立刻打中
|
|
private int _enabledFrame = -1;
|
|
|
|
public bool CanBeClicked() { return _isBlinking; }
|
|
|
|
void OnEnable()
|
|
{
|
|
_enabledFrame = Time.frameCount; // 记录被激活的帧号
|
|
|
|
_modelRenderer = GetComponent<MeshRenderer>();
|
|
if (_modelRenderer == null) _modelRenderer = GetComponentInChildren<MeshRenderer>();
|
|
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = false;
|
|
|
|
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; // 缩小到 0.15 米,防止多个 ping 碰撞体重叠
|
|
sphere.center = Vector3.zero;
|
|
sphere.isTrigger = false;
|
|
sphere.enabled = true;
|
|
|
|
CreateOutlineMesh();
|
|
_modelShown = false;
|
|
_isBlinking = true;
|
|
StopAllCoroutines();
|
|
StartCoroutine(BlinkOutline());
|
|
}
|
|
|
|
void CreateOutlineMesh()
|
|
{
|
|
if (_outlineMesh != null) return;
|
|
var mf = GetComponent<MeshFilter>();
|
|
if (mf == null) mf = GetComponentInChildren<MeshFilter>();
|
|
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.transform.position += Vector3.up * 0.05f;
|
|
go.AddComponent<MeshFilter>().sharedMesh = mf.sharedMesh;
|
|
_outlineMesh = go.AddComponent<MeshRenderer>();
|
|
_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 Update()
|
|
{
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
if (!_isBlinking) return;
|
|
|
|
// 同一帧只能有一个 ping 被触发
|
|
if (Time.frameCount == _lastClickedFrame) return;
|
|
|
|
// 刚被激活的前2帧不允许被点击(防止 nextPing 被立即触发)
|
|
if (Time.frameCount - _enabledFrame < 2) return;
|
|
|
|
foreach (var c in Camera.allCameras)
|
|
{
|
|
if (!c.enabled) continue;
|
|
|
|
Ray ray = c.ScreenPointToRay(Input.mousePosition);
|
|
RaycastHit[] hits = Physics.RaycastAll(ray, 1000f);
|
|
foreach (var hit in hits)
|
|
{
|
|
if (hit.collider != null && hit.collider.gameObject == gameObject)
|
|
{
|
|
Debug.Log($"[PingClick] Update手动射线贯穿点中 (使用相机: {c.name}): {name}");
|
|
_lastClickedFrame = Time.frameCount;
|
|
_isBlinking = false;
|
|
PerformClick();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void PerformClick()
|
|
{
|
|
if (!_modelShown)
|
|
{
|
|
_modelShown = true;
|
|
_isBlinking = false;
|
|
ActionHistory.Push(ActionHistory.ActionType.PingFirst, this);
|
|
StopAllCoroutines();
|
|
if (_modelRenderer != null) _modelRenderer.enabled = true;
|
|
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = true;
|
|
if (_outlineMesh != null) _outlineMesh.enabled = false;
|
|
if (nextPing != null) nextPing.SetActive(true);
|
|
else
|
|
{
|
|
var chanraotuibu = GameObject.Find("chanraotuibu");
|
|
if (chanraotuibu == null)
|
|
{
|
|
var mi = GameObject.Find("manInjury");
|
|
if (mi != null)
|
|
{
|
|
var t = mi.transform.Find("chanraotuibu");
|
|
if (t != null) chanraotuibu = t.gameObject;
|
|
}
|
|
}
|
|
if (chanraotuibu != null) chanraotuibu.SetActive(true);
|
|
Debug.Log($"[PingClick] {name} 是最后一个 ping 模型,现已激活 chanraotuibu");
|
|
}
|
|
|
|
if (nextPing != null) Debug.Log($"[PingClick] {name} 模型显示, 激活 {nextPing.name}");
|
|
else Debug.Log($"[PingClick] {name} 模型显示, 激活下一个");
|
|
|
|
var sm = FindObjectOfType<StepManager>();
|
|
if (sm != null) sm.RecordPingStep(pingIndex, false);
|
|
}
|
|
else
|
|
{
|
|
if (chanraoObject != null) { chanraoObject.SetActive(true); Debug.Log($"[PingClick] {name} → {chanraoObject.name}"); }
|
|
HideSelf();
|
|
ActionHistory.Push(ActionHistory.ActionType.PingSecond, this);
|
|
var sm = FindObjectOfType<StepManager>();
|
|
if (sm != null) sm.RecordPingStep(pingIndex, true);
|
|
|
|
var remainingPings = Object.FindObjectsOfType<PingClickHandler>();
|
|
int activePingCount = 0;
|
|
foreach (var p in remainingPings)
|
|
{
|
|
var col = p.GetComponent<Collider>();
|
|
if (col != null && col.enabled) activePingCount++;
|
|
}
|
|
|
|
if (activePingCount == 0)
|
|
{
|
|
Debug.Log("[PingClick] 所有的 ping 都已经绑成 chanrao 了!播放完成语音。");
|
|
var am = FindObjectOfType<AudioManager>();
|
|
if (am != null)
|
|
{
|
|
#if UNITY_EDITOR
|
|
var finishClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>("Assets/Audio/people/伤员一切正常,包扎完毕.mp3");
|
|
if (finishClip != null) am.PlayVoice(finishClip);
|
|
#endif
|
|
}
|
|
|
|
var uiMgr = FindObjectOfType<UIManager>();
|
|
if (uiMgr != null)
|
|
{
|
|
uiMgr.HideVideo();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ReEnableBorder()
|
|
{
|
|
if (!_modelShown) return;
|
|
|
|
var col = GetComponent<Collider>();
|
|
if (chanraoObject != null && chanraoObject.activeSelf)
|
|
{
|
|
if (col != null) col.enabled = false;
|
|
return;
|
|
}
|
|
|
|
_isBlinking = true;
|
|
_enabledFrame = Time.frameCount; // 重新激活时也要保护2帧
|
|
if (col != null) col.enabled = true;
|
|
|
|
if (_outlineMesh != null)
|
|
{
|
|
_outlineMesh.enabled = true;
|
|
StopAllCoroutines();
|
|
StartCoroutine(BlinkOutline());
|
|
}
|
|
}
|
|
|
|
public void DisableBorder()
|
|
{
|
|
_isBlinking = false;
|
|
if (_outlineMesh != null) _outlineMesh.enabled = false;
|
|
StopAllCoroutines();
|
|
}
|
|
|
|
public void UndoSecondClick()
|
|
{
|
|
if (chanraoObject != null) chanraoObject.SetActive(false);
|
|
ShowSelf();
|
|
_modelShown = true;
|
|
_isBlinking = true;
|
|
if (_outlineMesh != null) _outlineMesh.enabled = true;
|
|
StopAllCoroutines();
|
|
StartCoroutine(BlinkOutline());
|
|
}
|
|
public void UndoShow()
|
|
{
|
|
_modelShown = false;
|
|
_isBlinking = false;
|
|
StopAllCoroutines();
|
|
if (chanraoObject != null) chanraoObject.SetActive(false);
|
|
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = false;
|
|
var col = GetComponent<Collider>(); if (col != null) col.enabled = false;
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
private void HideSelf()
|
|
{
|
|
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = false;
|
|
var col = GetComponent<Collider>(); if (col != null) col.enabled = false;
|
|
_isBlinking = false;
|
|
StopAllCoroutines();
|
|
}
|
|
|
|
private void ShowSelf()
|
|
{
|
|
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = true;
|
|
if (_outlineMesh != null) _outlineMesh.enabled = false;
|
|
var col = GetComponent<Collider>(); if (col != null) col.enabled = true;
|
|
}
|
|
|
|
public void ResetState()
|
|
{
|
|
_modelShown = false;
|
|
|
|
var col = GetComponent<Collider>();
|
|
if (col != null) col.enabled = true;
|
|
|
|
_isBlinking = true;
|
|
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());
|
|
}
|
|
}
|