using System.Collections; using System.Collections.Generic; using UnityEngine; public class ClickToFocus : MonoBehaviour { [Tooltip("相机在目标物体位置的偏移量")] //public Vector3 cameraOffset = new Vector3(5f, 10f, 0f); private Dictionary targetObjectAndPosition = new Dictionary(); [Tooltip("平滑过渡所用时间(秒)")] public float transitionDuration = 1.0f; [Header("双击设置")] [Tooltip("双击间隔时间阈值(秒)")] public float doubleClickInterval = 0.2f; private float lastClickTime = 0f; private Camera mainCamera; public Vector3 core = new Vector3(1502f, 38f, 707f); void Start() { mainCamera = Camera.main; //targetObjectAndPosition.Add("upStreamLidarIndicator", new Vector3(1377.8f, 52f, 1048f)); //targetObjectAndPosition.Add("downStreamLidarIndicator", new Vector3(1414f, 52.7f, 1000.7f)); //targetObjectAndPosition.Add("weatherStationRainfallBarrel", new Vector3(1423.7f, 55.8f, 1042f)); //targetObjectAndPosition.Add("stoneMemorial", new Vector3(1386f, 51.54f, 1052f)); //targetObjectAndPosition.Add("bigBridge", new Vector3(1378.4f, 59f, 972.5f));这些是北溪桥闸的点位,凤凰水库大概40多个??????这手动加还得了 targetObjectAndPosition.Add("Qiao", new Vector3(1494f, 40.7f, 1555.3f)); targetObjectAndPosition.Add("Qiao_SHuiWeiJI", new Vector3(1520.6f, 39.3f, 1493f)); targetObjectAndPosition.Add("XieHongZhaShi_000", new Vector3(1513f, 41.5f, 690f)); targetObjectAndPosition.Add("一断面渗压计1-1", new Vector3(1800f, 37f, 877f)); targetObjectAndPosition.Add("一断面渗压计1-2", new Vector3(1814f, 34f, 843f)); targetObjectAndPosition.Add("一断面渗压计1-3", new Vector3(1819f, 27f, 833f)); targetObjectAndPosition.Add("一断面渗压计1-4", new Vector3(1822f, 23f, 829f)); targetObjectAndPosition.Add("一断面渗压计1-5", new Vector3(1832f, 10f, 806f)); targetObjectAndPosition.Add("二断面渗压计2-1", new Vector3(1739f, 37f, 848f)); targetObjectAndPosition.Add("二断面渗压计2-2", new Vector3(1752f, 33f, 820f)); targetObjectAndPosition.Add("二断面渗压计2-3", new Vector3(1757f, 26f, 808f)); targetObjectAndPosition.Add("二断面渗压计2-4", new Vector3(1762f, 21f, 797f)); targetObjectAndPosition.Add("二断面渗压计2-5", new Vector3(1772f, 10f, 778f)); targetObjectAndPosition.Add("三断面渗压计3-1", new Vector3(1686f, 39f, 822f)); targetObjectAndPosition.Add("三断面渗压计3-2", new Vector3(1699f, 34f, 795f)); targetObjectAndPosition.Add("三断面渗压计3-3", new Vector3(1705f, 27f, 784f)); targetObjectAndPosition.Add("三断面渗压计3-4", new Vector3(1709f, 22f, 774f)); targetObjectAndPosition.Add("三断面渗压计3-5", new Vector3(1717f, 13f, 758f)); targetObjectAndPosition.Add("四断面渗压计4-1", new Vector3(1766f, 37f, 856f)); targetObjectAndPosition.Add("四断面渗压计4-2", new Vector3(1782f, 27f, 824f)); targetObjectAndPosition.Add("四断面渗压计4-3", new Vector3(1791f, 17f, 802f)); targetObjectAndPosition.Add("四断面渗压计4-4", new Vector3(1801f, 5f, 781f)); targetObjectAndPosition.Add("四断面渗压计4-5", new Vector3(1807f, -1f, 769f)); targetObjectAndPosition.Add("五断面渗压计5-1", new Vector3(1712f, 37f, 842f)); targetObjectAndPosition.Add("五断面渗压计5-2", new Vector3(1738f, 27f, 803f)); targetObjectAndPosition.Add("五断面渗压计5-3", new Vector3(1749f, 16f, 782f)); targetObjectAndPosition.Add("五断面渗压计5-4", new Vector3(1759f, 5f, 762f)); targetObjectAndPosition.Add("五断面渗压计5-5", new Vector3(1763f, 1f, 753f)); targetObjectAndPosition.Add("数据采集仪", new Vector3(1687f, 44f, 805f)); targetObjectAndPosition.Add("LiangShuiYan_001", new Vector3(1783f, -2f, 724f)); targetObjectAndPosition.Add("LiangShuiYan_002", new Vector3(1792f, -3f, 743f)); targetObjectAndPosition.Add("LiangShuiYan_003", new Vector3(1754f, -2f, 723f)); targetObjectAndPosition.Add("LiangShuiYan_004", new Vector3(1711f, 10f, 734f)); targetObjectAndPosition.Add("LiangShuiYan_0019", new Vector3(1706f, 8f, 737f)); targetObjectAndPosition.Add("GNSS_001", new Vector3(1827f, 39f, 868f)); targetObjectAndPosition.Add("GNSS_002", new Vector3(1797f, 38f, 854f)); targetObjectAndPosition.Add("GNSS_003", new Vector3(1765f, 38f, 840f)); targetObjectAndPosition.Add("GNSS_004", new Vector3(1700f, 38f, 809f)); targetObjectAndPosition.Add("GNSS_005", new Vector3(1522f, 39f, 709f)); targetObjectAndPosition.Add("GNSS_006", new Vector3(1505f, 38f, 713f)); targetObjectAndPosition.Add("GNSS_007", new Vector3(1498f, 38f, 710f)); targetObjectAndPosition.Add("GNSS_008", new Vector3(1490f, 38f, 706f)); targetObjectAndPosition.Add("GNSS_009", new Vector3(1487f, 39f, 693f)); targetObjectAndPosition.Add("GNSS_基点", new Vector3(1614f, 39f, 742f)); //targetObjectAndPosition.Add("", new Vector3(f, f, f)); //targetObjectAndPosition.Add("", new Vector3(f, f, f)); //targetObjectAndPosition.Add("", new Vector3(f, f, f)); Debug.Log("双击功能已初始化,间隔时间: " + doubleClickInterval + "秒"); } void Update() { if (Input.GetMouseButtonDown(0)) { float timeSinceLastClick = Time.time - lastClickTime; // 检查是否是双击 if (timeSinceLastClick <= doubleClickInterval && timeSinceLastClick > 0) { Debug.Log("检测到双击,间隔时间: " + timeSinceLastClick); HandleClick(); } lastClickTime = Time.time; } } private void HandleClick() { Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity) && hit.collider.CompareTag("Target")) { Debug.Log("射线检测到目标对象: " + hit.transform.name); bool matched = false; foreach (KeyValuePair pair in targetObjectAndPosition) { if (hit.transform.gameObject.name == pair.Key) { Vector3 targetPos = pair.Value; Debug.Log("开始相机移动到: " + targetPos); StartCoroutine(MoveCamera(mainCamera, targetPos, transitionDuration, hit.transform)); core = hit.transform.position; matched = true; break; } } if (!matched) { Vector3 fallbackPos = hit.transform.position + new Vector3(5, 0, 0); Debug.Log("未匹配到,使用偏移位置: " + fallbackPos); StartCoroutine(MoveCamera(mainCamera, fallbackPos, transitionDuration, hit.transform)); core = hit.transform.position; } } else { Debug.Log("射线未击中带有Target标签的对象"); } } private IEnumerator MoveCamera(Camera cam, Vector3 targetPos, float duration, Transform focusTarget) { Vector3 startPos = cam.transform.position; float elapsed = 0f; Debug.Log("相机移动协程启动,目标位置: " + targetPos); while (elapsed < duration) { float t = Mathf.Lerp(0f, 1f, elapsed / duration); cam.transform.position = Vector3.Lerp(startPos, targetPos, t); if (focusTarget != null) cam.transform.LookAt(focusTarget); elapsed += Time.deltaTime; yield return null; } cam.transform.position = targetPos; if (focusTarget != null) cam.transform.LookAt(focusTarget); Debug.Log("相机移动完成"); } }