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
+40 -40
View File
@@ -349,32 +349,32 @@ PrefabInstance:
- target: {fileID: -8679921383154817045, guid: ef5fbbeda893849a285d196510e10634,
type: 3}
propertyPath: m_LocalScale.x
value: 1
value: 0.011254501
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: ef5fbbeda893849a285d196510e10634,
type: 3}
propertyPath: m_LocalScale.y
value: 1
value: 0.011254501
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: ef5fbbeda893849a285d196510e10634,
type: 3}
propertyPath: m_LocalScale.z
value: 1
value: 0.011254501
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: ef5fbbeda893849a285d196510e10634,
type: 3}
propertyPath: m_LocalPosition.x
value: -8.242
value: -8.246
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: ef5fbbeda893849a285d196510e10634,
type: 3}
propertyPath: m_LocalPosition.y
value: 0.14941
value: 0.16927
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: ef5fbbeda893849a285d196510e10634,
type: 3}
propertyPath: m_LocalPosition.z
value: -8.2853
value: -8.2733
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: ef5fbbeda893849a285d196510e10634,
type: 3}
@@ -452,7 +452,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
nextObject: {fileID: 1880212782}
targetPoint: {fileID: 672771961}
targetPoint: {fileID: 0}
--- !u!135 &12263700
SphereCollider:
m_ObjectHideFlags: 0
@@ -3466,6 +3466,37 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 458363765}
m_CullTransparentMesh: 1
--- !u!1 &485512480
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 485512481}
m_Layer: 0
m_Name: ShosePoint
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &485512481
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 485512480}
serializedVersion: 2
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: -9.123, y: 0.031, z: -7.962}
m_LocalScale: {x: 0.01123915, y: 0.01123915, z: 0.01123915}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 90, y: 0, z: 0}
--- !u!1 &498395086
GameObject:
m_ObjectHideFlags: 0
@@ -4120,37 +4151,6 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 617526392}
m_CullTransparentMesh: 1
--- !u!1 &672771960
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 672771961}
m_Layer: 0
m_Name: ShosePoint
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &672771961
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 672771960}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -9.262, y: 0.01, z: -8.122}
m_LocalScale: {x: 0.011181515, y: 0.011181515, z: 0.011181515}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &717077354
GameObject:
m_ObjectHideFlags: 0
@@ -11332,7 +11332,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2cf6b55fd9d5146c8a2ba5d06dc8466d, type: 3}
m_Name:
m_EditorClassIdentifier:
nextObject: {fileID: 2088997100}
nextObject: {fileID: 0}
--- !u!1 &1888357254
GameObject:
m_ObjectHideFlags: 0
@@ -12466,6 +12466,7 @@ SceneRoots:
m_Roots:
- {fileID: 414549209}
- {fileID: 12263693}
- {fileID: 485512481}
- {fileID: 37271319}
- {fileID: 96184422}
- {fileID: 1364120480}
@@ -12489,4 +12490,3 @@ SceneRoots:
- {fileID: 1381048278}
- {fileID: 754012933}
- {fileID: 48410318}
- {fileID: 672771961}
@@ -25,7 +25,8 @@ public class ChanraoTuibuHandler : 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);
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;
@@ -30,7 +30,8 @@ public class PingClickHandler : 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);
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;
+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
}