From 424f07cce2c4a34c843cb8c85c50cb7d83c70a51 Mon Sep 17 00:00:00 2001 From: yangbear Date: Mon, 6 Jul 2026 05:07:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20collider=E6=8C=89localScale=E7=BC=A9?= =?UTF-8?q?=E6=94=BE+=E6=8F=8F=E8=BE=B9=E8=A1=A5=E5=81=BFscale(=E8=A7=A3?= =?UTF-8?q?=E5=86=B30.01scale=E4=B8=8B=E4=B8=8D=E5=8F=AF=E8=A7=81)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scenes/main.unity | 80 +++++++++---------- .../Interaction/ChanraoTuibuHandler.cs | 3 +- .../Scripts/Interaction/PingClickHandler.cs | 3 +- .../Scripts/Interaction/ShoseClickHandler.cs | 58 +++----------- 4 files changed, 57 insertions(+), 87 deletions(-) diff --git a/Assets/Scenes/main.unity b/Assets/Scenes/main.unity index 987d38e..3e31bc8 100644 --- a/Assets/Scenes/main.unity +++ b/Assets/Scenes/main.unity @@ -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} diff --git a/Assets/Scripts/Interaction/ChanraoTuibuHandler.cs b/Assets/Scripts/Interaction/ChanraoTuibuHandler.cs index 52f766c..b875082 100644 --- a/Assets/Scripts/Interaction/ChanraoTuibuHandler.cs +++ b/Assets/Scripts/Interaction/ChanraoTuibuHandler.cs @@ -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().sharedMesh = mf.sharedMesh; diff --git a/Assets/Scripts/Interaction/PingClickHandler.cs b/Assets/Scripts/Interaction/PingClickHandler.cs index 687acd3..fe90c2d 100644 --- a/Assets/Scripts/Interaction/PingClickHandler.cs +++ b/Assets/Scripts/Interaction/PingClickHandler.cs @@ -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().sharedMesh = mf.sharedMesh; diff --git a/Assets/Scripts/Interaction/ShoseClickHandler.cs b/Assets/Scripts/Interaction/ShoseClickHandler.cs index 128f70a..2017caa 100644 --- a/Assets/Scripts/Interaction/ShoseClickHandler.cs +++ b/Assets/Scripts/Interaction/ShoseClickHandler.cs @@ -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(); if (mf == null) mf = GetComponentInChildren(); if (mf != null && mf.sharedMesh != null) { - _meshBounds = mf.sharedMesh.bounds; + var mb = mf.sharedMesh.bounds; + Destroy(GetComponent()); var col = GetComponent(); if (col == null) col = gameObject.AddComponent(); - // 移除旧 SphereCollider - var oldCol = GetComponent(); - 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(); - col.radius = 3f; - col.enabled = true; - Debug.LogWarning("[ShoseClick] 无MeshFilter,用SphereCollider fallback"); - } - + else { if (GetComponent() == null) gameObject.AddComponent().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().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(); - if (col != null) - { - Gizmos.color = Color.green; - Gizmos.matrix = transform.localToWorldMatrix; - Gizmos.DrawWireCube(col.center, col.size); - } - else - { - var sc = GetComponent(); - if (sc != null) - { - Gizmos.color = Color.green; - Gizmos.DrawWireSphere(transform.position, sc.radius); - } - } - } -#endif }