103 lines
3.8 KiB
C#
103 lines
3.8 KiB
C#
using UnityEngine;
|
|
|
|
public class ShoseClickHandler : MonoBehaviour
|
|
{
|
|
public GameObject nextObject;
|
|
public Transform targetPoint;
|
|
private MeshRenderer _outlineMesh;
|
|
private Bounds _meshBounds;
|
|
private bool _clicked;
|
|
|
|
void OnEnable()
|
|
{
|
|
// 用模型的精确 Bounds 设 BoxCollider
|
|
var mf = GetComponent<MeshFilter>();
|
|
if (mf == null) mf = GetComponentInChildren<MeshFilter>();
|
|
if (mf != null && mf.sharedMesh != null)
|
|
{
|
|
_meshBounds = mf.sharedMesh.bounds;
|
|
var col = GetComponent<BoxCollider>();
|
|
if (col == null) col = gameObject.AddComponent<BoxCollider>();
|
|
// 移除旧 SphereCollider
|
|
var oldCol = GetComponent<SphereCollider>();
|
|
if (oldCol != null) Destroy(oldCol);
|
|
col.center = _meshBounds.center;
|
|
col.size = _meshBounds.size * 1.2f; // 稍微扩大一点好点
|
|
col.enabled = true;
|
|
Debug.Log($"[ShoseClick] BoxCollider center={col.center} size={col.size} bounds={_meshBounds}");
|
|
}
|
|
else
|
|
{
|
|
// fallback
|
|
var col = gameObject.AddComponent<SphereCollider>();
|
|
col.radius = 3f;
|
|
col.enabled = true;
|
|
Debug.LogWarning("[ShoseClick] 无MeshFilter,用SphereCollider fallback");
|
|
}
|
|
|
|
CreateOutlineMesh();
|
|
StartCoroutine(BlinkOutline());
|
|
Debug.Log($"[ShoseClick] Start done. obj={gameObject.name} pos={transform.position} scale={transform.lossyScale}");
|
|
}
|
|
|
|
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()
|
|
{
|
|
Debug.Log($"[ShoseClick] OnMouseDown fired! clicked={_clicked} mousePos={Input.mousePosition}");
|
|
if (_clicked) return;
|
|
_clicked = true;
|
|
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} pos={targetPoint.position}"); }
|
|
if (nextObject != null) nextObject.SetActive(true);
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
void OnDrawGizmos()
|
|
{
|
|
var col = GetComponent<BoxCollider>();
|
|
if (col != null)
|
|
{
|
|
Gizmos.color = Color.green;
|
|
Gizmos.matrix = transform.localToWorldMatrix;
|
|
Gizmos.DrawWireCube(col.center, col.size);
|
|
}
|
|
else
|
|
{
|
|
var sc = GetComponent<SphereCollider>();
|
|
if (sc != null)
|
|
{
|
|
Gizmos.color = Color.green;
|
|
Gizmos.DrawWireSphere(transform.position, sc.radius);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
}
|