fix: collider按localScale缩放+描边补偿scale(解决0.01scale下不可见)

This commit is contained in:
yangbear
2026-07-06 05:07:15 +08:00
parent a8e6b2084b
commit 424f07cce2
4 changed files with 57 additions and 87 deletions
+13 -45
View File
@@ -5,39 +5,28 @@ 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 mb = mf.sharedMesh.bounds;
Destroy(GetComponent<SphereCollider>());
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; // 稍微扩大一点好点
// 按 localScale 缩放 collider 匹配模型实际大小
col.center = Vector3.Scale(mb.center, transform.localScale);
col.size = Vector3.Scale(mb.size, transform.localScale) * 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");
}
else { if (GetComponent<Collider>() == null) gameObject.AddComponent<SphereCollider>().radius = 0.3f; }
CreateOutlineMesh();
StopAllCoroutines();
StartCoroutine(BlinkOutline());
Debug.Log($"[ShoseClick] Start done. obj={gameObject.name} pos={transform.position} scale={transform.lossyScale}");
_clicked = false;
}
void CreateOutlineMesh()
@@ -51,7 +40,9 @@ public class ShoseClickHandler : MonoBehaviour
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);
// 描边宽度补偿 scale
float s = transform.lossyScale.magnitude / Mathf.Sqrt(3);
mat.SetFloat("_OutlineWidth", s > 0.001f ? OutlineConfig.Width / s : OutlineConfig.Width);
var go = new GameObject("Outline");
go.transform.SetParent(transform, false);
go.AddComponent<MeshFilter>().sharedMesh = mf.sharedMesh;
@@ -68,35 +59,12 @@ public class ShoseClickHandler : MonoBehaviour
void OnMouseDown()
{
Debug.Log($"[ShoseClick] OnMouseDown fired! clicked={_clicked} mousePos={Input.mousePosition}");
if (_clicked) return;
_clicked = true;
Debug.Log("[ShoseClick] ★ 模型被点击了! ★");
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 (targetPoint != null) { transform.position = targetPoint.position; transform.rotation = targetPoint.rotation; }
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
}