using UnityEngine;
///
/// Shose 交互:模型可见 + 外圈蓝色描边闪烁,点击后移动到 ShosePoint
///
public class ShoseClickHandler : MonoBehaviour
{
public GameObject nextObject;
public Transform targetPoint;
private MeshRenderer _outlineMesh;
void OnEnable()
{
// 不隐藏模型!只加描边子对象
if (GetComponent() == null)
{
var sc = gameObject.AddComponent();
sc.radius = 0.5f;
sc.isTrigger = false;
}
CreateOutlineMesh();
StopAllCoroutines();
StartCoroutine(BlinkOutline());
Debug.Log($"[ShoseClick] OnEnable outline={_outlineMesh!=null} collider={GetComponent()!=null}");
}
void CreateOutlineMesh()
{
if (_outlineMesh != null) return;
var mf = GetComponent();
if (mf == null) mf = GetComponentInChildren();
if (mf == null || mf.sharedMesh == null) { Debug.LogWarning("[ShoseClick] 无Mesh"); return; }
var shader = Shader.Find("Custom/OutlineUnlit");
if (shader == null) { Debug.LogWarning("[ShoseClick] Shader缺失"); 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.03f);
var go = new GameObject("Outline");
go.transform.SetParent(transform, false);
go.AddComponent().sharedMesh = mf.sharedMesh;
_outlineMesh = go.AddComponent();
_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; }
if (nextObject != null) nextObject.SetActive(true);
}
}