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

99 lines
3.0 KiB
C#
Raw Normal View History

using UnityEngine;
public class Point2ClickHandler : MonoBehaviour
{
public GameObject nextObject; // This will be Shose
private MeshRenderer _outlineMesh;
private bool _clicked;
public bool CanBeClicked() { return !_clicked; }
void OnEnable()
{
if (GetComponent<Collider>() == null) gameObject.AddComponent<SphereCollider>().radius = 0.5f;
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = false;
CreateOutlineMesh();
_clicked = 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 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<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()
{
PerformClick();
}
public void PerformClick()
{
if (_clicked) return;
_clicked = true;
Debug.Log("[Point2Click] ★ 被点击 ★");
ActionHistory.Push(ActionHistory.ActionType.Point2, this);
StopAllCoroutines();
if (_outlineMesh != null) _outlineMesh.enabled = false;
var woman = GameObject.Find("woman");
if (woman != null)
{
var anim = woman.GetComponent<Animator>();
if (anim != null)
{
anim.SetBool("IsCheck", true);
}
}
if (nextObject != null) nextObject.SetActive(true);
gameObject.SetActive(false);
}
public void ResetState()
{
_clicked = false;
if (_outlineMesh != null) _outlineMesh.enabled = true;
if (nextObject != null) nextObject.SetActive(false);
var woman = GameObject.Find("woman");
if (woman != null)
{
var anim = woman.GetComponent<Animator>();
if (anim != null)
{
anim.SetBool("IsCheck", false);
}
}
gameObject.SetActive(true);
StopAllCoroutines();
StartCoroutine(BlinkOutline());
}
}