Files
VFSUnity/Assets/Scripts/Interaction/PingClickHandler.cs
T

95 lines
4.1 KiB
C#

using UnityEngine;
public class PingClickHandler : MonoBehaviour
{
public GameObject chanraoObject, nextPing;
public int pingIndex;
private MeshRenderer _modelRenderer, _outlineMesh;
private bool _clickedOnce;
private static int _firstClickCount;
void OnEnable()
{
_modelRenderer = GetComponent<MeshRenderer>();
if (_modelRenderer == null) _modelRenderer = GetComponentInChildren<MeshRenderer>();
if (_modelRenderer != null) _modelRenderer.enabled = false;
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = false;
if (GetComponent<Collider>() == null) gameObject.AddComponent<SphereCollider>().radius = 0.5f;
CreateOutlineMesh();
_clickedOnce = false;
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 mat = new Material(Shader.Find("Custom/OutlineUnlit"));
mat.SetColor("_OutlineColor", new Color(0.2f, 0.5f, 1f, 1f));
mat.SetColor("_MainColor", new Color(0, 0, 0, 0));
mat.SetFloat("_OutlineWidth", 0.1f);
var go = new GameObject("Outline");
go.transform.SetParent(transform, false);
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 OnMouseDown()
{
foreach (var c in Object.FindObjectsOfType<Camera>())
{
if (!c.enabled) continue;
foreach (var h in Physics.RaycastAll(c.ScreenPointToRay(Input.mousePosition)))
if (h.collider != null && h.collider.transform.parent != null && h.collider.transform.parent.name == "touchArray") return;
}
if (_firstClickCount < 4)
{
if (_clickedOnce) return;
_clickedOnce = true; _firstClickCount++;
StopAllCoroutines();
if (_modelRenderer != null) _modelRenderer.enabled = true;
if (_outlineMesh != null) _outlineMesh.enabled = false;
if (nextPing != null) nextPing.SetActive(true);
Debug.Log($"[PingClick] {name} 第1击 ({_firstClickCount}/4)");
if (_firstClickCount >= 4)
{
var mi = GameObject.Find("manInjury");
if (mi != null) { var ct = mi.transform.Find("chanraotuibu"); if (ct != null) { ct.gameObject.SetActive(true); Debug.Log("[PingClick] chanraotuibu 激活"); } }
}
var sm = FindObjectOfType<StepManager>(); if (sm != null) sm.RecordPingStep(pingIndex, false);
}
else
{
if (!_clickedOnce) return;
if (chanraoObject != null) chanraoObject.SetActive(true);
StopAllCoroutines();
if (_modelRenderer != null) _modelRenderer.enabled = false;
if (_outlineMesh != null) _outlineMesh.enabled = false;
gameObject.SetActive(false);
Debug.Log($"[PingClick] {name} 第2击");
var sm = FindObjectOfType<StepManager>(); if (sm != null) sm.RecordPingStep(pingIndex, true);
}
}
public void ReEnableBorder() { if (_outlineMesh != null) { _outlineMesh.enabled = true; StopAllCoroutines(); StartCoroutine(BlinkOutline()); } }
public void UndoSecondClick() { if (!_clickedOnce) return; _clickedOnce = false; _firstClickCount--; gameObject.SetActive(true); if (_modelRenderer != null) _modelRenderer.enabled = false; StopAllCoroutines(); StartCoroutine(BlinkOutline()); }
public void UndoShow() { gameObject.SetActive(false); }
}