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

65 lines
2.4 KiB
C#

using UnityEngine;
public class ShoseClickHandler : MonoBehaviour
{
public GameObject nextObject;
public Transform targetPoint;
private MeshRenderer _outlineMesh;
void OnEnable()
{
// 强制确保有足够大的 collider
var col = GetComponent<Collider>();
if (col == null) col = gameObject.AddComponent<SphereCollider>();
if (col is SphereCollider sc) { sc.radius = 1.2f; sc.isTrigger = false; }
// 干掉可能拦截点击的旧组件
var drag = GetComponent<DraggableObject>();
if (drag != null) Destroy(drag);
CreateOutlineMesh();
StopAllCoroutines();
StartCoroutine(BlinkOutline());
Debug.Log($"[ShoseClick] OnEnable outline={_outlineMesh!=null} col radius={(col is SphereCollider sc2?sc2.radius:-1)}");
}
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", 0.06f);
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()
{
Debug.Log("[ShoseClick] ★ 被点击了 ★");
StopAllCoroutines();
if (_outlineMesh != null) _outlineMesh.enabled = false;
if (targetPoint != null) { transform.position = targetPoint.position; transform.rotation = targetPoint.rotation; Debug.Log($"[ShoseClick] 移动到 {targetPoint.name}"); }
else Debug.LogWarning("[ShoseClick] targetPoint 为空!");
if (nextObject != null) nextObject.SetActive(true);
}
}