using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System; using Cinemachine; using System.IO; using DG.Tweening; using UnityEngine.Networking; public class mapDrag : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler { //public Image BGmap; public RectTransform BGmap; Vector3 offset; Vector3 originPos; Vector3 outPos; public Slider sliderScale; public GameObject S1; public GameObject S2; public GameObject S3; public GameObject S4; public GameObject M1; public GameObject M2; public GameObject M3; public GameObject M4; public GameObject Q1; public GameObject Q2; public GameObject Q3; public GameObject Q4; private Vector2? initialTouch1Pos = null; private Vector2? initialTouch2Pos = null; private float initialSliderValue; public float minScale; public float maxScale; public Animator XX; public string buildingName; public TMPro.TMP_Text zi; public Image noMN; public Image gu; public Image jin; public Image jin2; public Image wz; public AudioSource AS; // Start is called before the first frame update void Start() { sliderScale.onValueChanged.AddListener(OnScaleChanged); } void Update() { if (Input.touchCount == 2) { HandlePinchZoom(); } } private void HandlePinchZoom() { Touch touch0 = Input.GetTouch(0); Touch touch1 = Input.GetTouch(1); // Calculate the distance between the two touches float currentDistance = (touch0.position - touch1.position).magnitude; if (initialTouch1Pos == null || initialTouch2Pos == null) { // Set initial positions initialTouch1Pos = touch0.position; initialTouch2Pos = touch1.position; initialSliderValue = sliderScale.value; } else { // Calculate the initial distance float initialDistance = (initialTouch1Pos.Value - initialTouch2Pos.Value).magnitude; // Calculate the distance change float distanceChange = currentDistance - initialDistance; // Map the distance change to Slider value float valueChange = distanceChange * 0.01f; // Adjust sensitivity if needed sliderScale.value = Mathf.Clamp(initialSliderValue + valueChange, minScale, maxScale); } } public void OnPointerUp() { // Reset initial touch positions when touches are released initialTouch1Pos = null; initialTouch2Pos = null; } private void OnScaleChanged(float arg0) { BGmap.localScale = Vector3.one * arg0; } public void OnDrag(PointerEventData eventData) { BGmap.anchoredPosition = Input.mousePosition + offset; } public void OnBeginDrag(PointerEventData eventData) { if (RectTransformUtility.ScreenPointToWorldPointInRectangle(BGmap,Input.mousePosition,eventData.enterEventCamera,out outPos)) { originPos = BGmap.anchoredPosition; //BGmap.SetAsLastSibling(); offset = (Vector3)BGmap.anchoredPosition - outPos; } } public void OnEndDrag(PointerEventData eventData) { } public void DisS() { S1.SetActive(true); S2.SetActive(true); S3.SetActive(true); S4.SetActive(true); } public void DisM() { M1.SetActive(true); M2.SetActive(true); M3.SetActive(true); M4.SetActive(true); } public void DisQ() { Q1.SetActive(true); Q2.SetActive(true); Q3.SetActive(true); Q4.SetActive(true); } public void UNDisAll() { S1.SetActive(false); S2.SetActive(false); S3.SetActive(false); S4.SetActive(false); Q1.SetActive(false); Q2.SetActive(false); Q3.SetActive(false); Q4.SetActive(false); M1.SetActive(false); M2.SetActive(false); M3.SetActive(false); M4.SetActive(false); } public void getName(string name)//放在要跳转的建筑的按钮上,在name里输入这个建筑的代号,方便在下个场景里实例化出来对应建筑 { buildingName = name; } public void disXX() { if (!File.Exists(Application.streamingAssetsPath + "/XinXi.txt")) { Debug.LogError("文件不存在!"); return; } string[] lines = File.ReadAllLines(Application.streamingAssetsPath + "/XinXi.txt"); for (int i = 0; i < lines.Length; i++) { string[] data = lines[i].Split('@'); if (data.Length >= 2) { string itemName = data[0]; string description = data[1]; } if (data[0] == buildingName) { zi.text = data[1]; zi.text = zi.text.Replace("\\n", "\n"); } } string g = "noModels/gu/"; string j = "noModels/jin/"; string w = "noModels/weizhi/"; string n = "noModels/name/"; //Sprite spriteName = Resources.Load("noModels/gu/" + buildingName); Sprite spriteName = loadSpriteFromSA(buildingName, g); gu.sprite = spriteName; imageHD(spriteName, gu); //gu.preserveAspect = true; //Sprite jin1 = Resources.Load("noModels/jin/" + buildingName) as Sprite; Sprite jin1 = loadSpriteFromSA(buildingName, j); jin.sprite = jin1; jin2.sprite = jin1; //imageHD(jin1, jin); imageHD(jin1, jin2); //Sprite weizhi = Resources.Load("noModels/weizhi/" + buildingName); Sprite weizhi = loadSpriteFromSA(buildingName, w); wz.sprite = weizhi; imageHD(weizhi, wz); //Sprite Name = Resources.Load("noModels/name/" + buildingName); Sprite Name = loadSpriteFromSA(buildingName, n); noMN.sprite = Name; noMN.preserveAspect = true; Debug.Log("noModels/name/" + buildingName); // Debug.Log(jin1+"-----"+Name+"-----"+noMN.sprite.name); jin.DOFade(0, 2).From(); jin.transform.DOLocalMoveY((gu.rectTransform.rect.height) + 120, 2).From(); zi.DOFade(0, 2).From(); //AS.clip = Resources.Load("sound/" + buildingName); loadSound(); } void imageHD(Sprite sprite,Image image) { float targetWidth = 920; float widthRatio = sprite.rect.width / sprite.rect.height; float targetHeight = targetWidth / widthRatio; image.sprite = sprite; image.rectTransform.sizeDelta = new Vector2(targetWidth, targetHeight); } public void XXback() { XX.GetComponent().Play("back"); //Content.transform.DORestart(); float delayTime = 1f; // 延时2秒后执行回调函数 DOTween.To(() => 0, x => { }, 0, delayTime) .OnComplete(() => { zi.text = null; }); //gu.sprite = null; //jin.sprite = null; //wz.sprite = null; //zi.text = null; } public Sprite loadSpriteFromSA(string buildingName, string ML) { // 构建完整的文件路径,将文件名和StreamingAssets下的具体文件夹路径拼接起来 string filePath = Path.Combine(Application.streamingAssetsPath, ML + buildingName + ".png"); // 读取文件的字节数据 byte[] fileData = File.ReadAllBytes(filePath); // 创建一个Texture2D对象,用于后续加载图片数据 Texture2D texture = new Texture2D(2, 2); // 从字节数据中加载图片信息到Texture2D对象,如果加载成功 if (texture.LoadImage(fileData)) { // 根据加载好的Texture2D创建Sprite对象 return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); } // 如果加载失败,返回null return null; } public void LoadSoundFromSA(string buildingName, string ML, Action onAudioLoaded) { // 构建完整的文件路径 string filePath = Path.Combine(Application.streamingAssetsPath, ML, buildingName + ".wav"); // 判断文件是否存在 if (!File.Exists(filePath)) { Debug.LogError($"音频文件 {filePath} 不存在!"); onAudioLoaded(null); // 回调并传递null return; } // 启动协程加载音频 StartCoroutine(LoadAudioClipCoroutine(filePath, onAudioLoaded)); } private IEnumerator LoadAudioClipCoroutine(string filePath, Action onAudioLoaded) { // 创建UnityWebRequest来加载音频 UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file://" + filePath, AudioType.WAV); yield return www.SendWebRequest(); // 检查加载是否成功 if (www.result != UnityWebRequest.Result.Success) { Debug.LogError($"音频加载失败: {www.error}"); onAudioLoaded(null); // 回调并传递null } else { // 获取并返回加载的音频 AudioClip audioClip = DownloadHandlerAudioClip.GetContent(www); onAudioLoaded(audioClip); // 回调并传递加载的音频 } } public void loadSound() { // 调用加载音频的方法,并传递回调函数 this.LoadSoundFromSA(buildingName, "sound/", (audioClip) => { if (audioClip != null) { Debug.Log("音频加载成功,播放音频!"); // 在这里播放音频 AS.clip = audioClip; } else { Debug.LogError("音频加载失败!"); } }); } }