diff --git a/xiaofang/Assets/Scenes/Tmap 1.unity b/xiaofang/Assets/Scenes/Tmap 1.unity index 7e320cb3..94e2d123 100644 --- a/xiaofang/Assets/Scenes/Tmap 1.unity +++ b/xiaofang/Assets/Scenes/Tmap 1.unity @@ -205,52 +205,6 @@ MeshCollider: m_Convex: 0 m_CookingOptions: 30 m_Mesh: {fileID: -7087271138916236341, guid: 00003e04dc9a2534eb207f108c7b0cd3, type: 3} ---- !u!1 &12822992 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 12822994} - - component: {fileID: 12822993} - m_Layer: 0 - m_Name: BurstCollisionWorld - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!114 &12822993 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 12822992} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: f1a161c4294214a4fbcb7e9e94800494, type: 3} - m_Name: - m_EditorClassIdentifier: - cellSpans: - m_AlignBytes: 16 ---- !u!4 &12822994 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 12822992} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} - m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 - m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 38 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &14477921 GameObject: m_ObjectHideFlags: 0 @@ -357,14 +311,15 @@ RectTransform: m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 - m_Children: [] + m_Children: + - {fileID: 1368271978} m_Father: {fileID: 491544196} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5} m_AnchoredPosition: {x: -508, y: 0} - m_SizeDelta: {x: 100, y: 100} + m_SizeDelta: {x: 120, y: 120} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &18751074 MonoBehaviour: @@ -8605,6 +8560,9 @@ MonoBehaviour: targetIcon: {fileID: 14477922} distanceText: {fileID: 1414618302} arrowIcon: {fileID: 18751073} + arrowDistanceText: {fileID: 1368271979} + edgePadding: 50 + textOffset: {x: 0, y: -30} --- !u!1 &496425324 GameObject: m_ObjectHideFlags: 0 @@ -22321,6 +22279,86 @@ MeshCollider: m_Convex: 0 m_CookingOptions: 30 m_Mesh: {fileID: -4669020800935709084, guid: 00003e04dc9a2534eb207f108c7b0cd3, type: 3} +--- !u!1 &1368271977 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1368271978} + - component: {fileID: 1368271980} + - component: {fileID: 1368271979} + m_Layer: 5 + m_Name: Text (Legacy) + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1368271978 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1368271977} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 18751073} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 1, y: 1} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 0, y: 0} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &1368271979 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1368271977} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 0, b: 0, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_FontData: + m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0} + m_FontSize: 36 + m_FontStyle: 0 + m_BestFit: 0 + m_MinSize: 10 + m_MaxSize: 40 + m_Alignment: 4 + m_AlignByGeometry: 0 + m_RichText: 1 + m_HorizontalOverflow: 0 + m_VerticalOverflow: 0 + m_LineSpacing: 1 + m_Text: New Text +--- !u!222 &1368271980 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1368271977} + m_CullTransparentMesh: 1 --- !u!1001 &1379519406 PrefabInstance: m_ObjectHideFlags: 0 @@ -29621,6 +29659,52 @@ MeshCollider: m_Convex: 0 m_CookingOptions: 30 m_Mesh: {fileID: 8711619002567319047, guid: 0c9b084f2070bda45b87da961d2eb0f6, type: 3} +--- !u!1 &1955451351 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1955451353} + - component: {fileID: 1955451352} + m_Layer: 0 + m_Name: BurstCollisionWorld + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1955451352 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1955451351} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f1a161c4294214a4fbcb7e9e94800494, type: 3} + m_Name: + m_EditorClassIdentifier: + cellSpans: + m_AlignBytes: 16 +--- !u!4 &1955451353 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1955451351} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 38 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1001 &1955845891 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/xiaofang/Assets/Script/hylScripts/TargetIndicator.cs b/xiaofang/Assets/Script/hylScripts/TargetIndicator.cs index 223335db..8234d8c7 100644 --- a/xiaofang/Assets/Script/hylScripts/TargetIndicator.cs +++ b/xiaofang/Assets/Script/hylScripts/TargetIndicator.cs @@ -9,10 +9,12 @@ public class TargetIndicator : MonoBehaviour public Transform target; // 目标点 public RectTransform uiCanvas; // UI Canvas 的 RectTransform public RectTransform targetIcon; // 目标图标的 UI - public Text distanceText; // 显示距离的文本 + public Text distanceText; // 屏幕内显示的距离文本 public RectTransform arrowIcon; // 屏幕外箭头图标 + public Text arrowDistanceText; // 屏幕外箭头后面的距离文本 public float edgePadding = 50f; // 箭头距离屏幕边缘的间距 + public Vector2 textOffset = new Vector2(0, -30); // 文本相对箭头的偏移 void Update() { @@ -25,11 +27,12 @@ public class TargetIndicator : MonoBehaviour // 目标在屏幕内,显示目标图标 targetIcon.gameObject.SetActive(true); arrowIcon.gameObject.SetActive(false); // 隐藏箭头 + arrowDistanceText.gameObject.SetActive(false); // 隐藏箭头距离文本 // 更新图标位置 targetIcon.position = screenPoint; - // 更新距离文本 + // 更新屏幕内距离文本 float distance = Vector3.Distance(mainCamera.transform.position, target.position); distanceText.text = $"{distance:F1}m"; } @@ -37,7 +40,8 @@ public class TargetIndicator : MonoBehaviour { // 目标在屏幕外,显示箭头 targetIcon.gameObject.SetActive(false); - arrowIcon.gameObject.SetActive(true); + arrowIcon.gameObject.SetActive(true); // 显示箭头 + arrowDistanceText.gameObject.SetActive(true); // 显示箭头距离文本 // 如果目标在玩家后方,翻转方向 if (screenPoint.z < 0) @@ -49,19 +53,27 @@ public class TargetIndicator : MonoBehaviour // 计算目标点相对于屏幕中心的方向 Vector3 direction = (screenPoint - new Vector3(Screen.width / 2, Screen.height / 2, 0)).normalized; - // 修正:箭头指向的屏幕边界位置 + // 计算箭头在屏幕边界的位置 Vector3 edgePosition = GetScreenEdgePosition(direction); // 设置箭头位置 arrowIcon.position = edgePosition; + // 设置距离文本位置(在箭头下方或后面) + Vector3 textPosition = edgePosition + (Vector3)textOffset; + textPosition = ClampToScreenBounds(textPosition); // 限制文本在屏幕范围内 + arrowDistanceText.transform.position = textPosition; + + // 保持距离文本水平显示 + arrowDistanceText.transform.rotation = Quaternion.identity; + + // 更新距离文本内容 + float distance = Vector3.Distance(mainCamera.transform.position, target.position); + arrowDistanceText.text = $"{distance:F1}m"; + // 旋转箭头使其指向目标方向 float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; arrowIcon.rotation = Quaternion.Euler(0, 0, angle); - - // 更新距离文本 - float distance = Vector3.Distance(mainCamera.transform.position, target.position); - distanceText.text = $"{distance:F1}m"; } } @@ -89,4 +101,12 @@ public class TargetIndicator : MonoBehaviour return new Vector3(edgeX, edgeY, 0); } } + + // 限制文本在屏幕范围内 + private Vector3 ClampToScreenBounds(Vector3 position) + { + position.x = Mathf.Clamp(position.x, edgePadding, Screen.width - edgePadding); + position.y = Mathf.Clamp(position.y, edgePadding, Screen.height - edgePadding); + return position; + } }