using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; /// /// 图像控制类 /// public class ImagePlayerEx : MonoBehaviour { public static ImagePlayerEx Instance; // public UnityWebRequest imgRequest; private void Awake() { Instance = this; } public Data info; /// /// BackImage的RectTransform组件 /// public RectTransform rectBackImage; /// /// 图像 /// public RawImage img; /// /// 音频播放图片 /// public Sprite SpritePlay; /// /// 音频停止图片 /// public Sprite SpriteStop; public Texture2D ErrorImage; /// /// 是否在播放 /// bool isPlaying; /// /// 全屏按钮 /// public Button btnFullscreen; /// /// 关闭按钮 /// public Button btnClose; /// /// 关闭按钮 /// public Button btnSound; /// /// 下一张图片按钮 /// public Button btnNext; /// /// 上一张图片按钮 /// public Button btnPriv; /// /// 放大图片按钮 /// public Button btnLarge; /// /// 缩小图片按钮 /// public Button btnSmall; /// /// 数据标题 /// public Text txtTitle; /// /// 数据描述 /// public Text txtDescription; /// /// 音乐名 /// string soundName; int currentIndex = 0; float currentLarge = 1f; float maxLarge = 10f; float minLarge = 0.5f; public bool IsShow; Vector2 v2; Animator anim; void Start() { // 关联最大化、关闭、播放音频按钮事件 if (btnFullscreen != null) btnFullscreen.onClick.AddListener(OnBtnFullScreenClick); if (btnClose != null) btnClose.onClick.AddListener(OnBtnCloseClick); if (btnSound != null) btnSound.onClick.AddListener(OnBtnSoundClick); if (btnNext != null) btnNext.onClick.AddListener(OnBtnNextClick); if (btnPriv != null) btnPriv.onClick.AddListener(OnBtnPrivClick); if (btnLarge != null) btnLarge.onClick.AddListener(OnBtnLargeClick); if (btnSmall != null) btnSmall.onClick.AddListener(OnBtnSmallClick); anim = GetComponent(); } /// /// 隐藏图像显示窗口 /// public void HideImage() { // 还原为正常窗口 if (isFullScreen) OnBtnFullScreenClick(); if (info.IsType(DataType.Image) || info.IsType(DataType.MultiImage)) { // 关闭 if (anim != null) anim.SetTrigger("CloseBg"); } else { HideComplete(); } currentIndex = 0; currentLarge = 1f; } /// /// 动画结束隐藏对象、暂停音频播放、还原播放按钮图标 /// void HideComplete() { // AudioManager.Instance.StopAudio(); btnSound.GetComponent().sprite = SpritePlay; IsShow = false; } /// /// 显示图像 /// /// 图像数据 public void ShowImage(int index) { BaseController.CanControl = false; currentIndex = index; IsShow = true; currentLarge = 1f; //txtTitle.text = info.Title; //if (txtDescription != null) //{ // txtDescription.text = info.DataDetails[0]; //} if (anim != null) anim.SetTrigger("OpenBg"); if (string.IsNullOrEmpty(info.AudioName)) { btnSound.gameObject.SetActive(false); // 无音频则隐藏播放按钮 } else { btnSound.gameObject.SetActive(true); // 有音频则隐藏播放按钮 soundName = info.AudioName; OnBtnSoundClick(); } if (info.IsType(DataType.Image) || info.IsType(DataType.ImageText)) { StartCoroutine(ImageLoader.LoadImage(CommonData.SpotImageFullPath + info.ImageName, img, ImageLoadCompleted)); btnNext.gameObject.SetActive(false); btnPriv.gameObject.SetActive(false); } else { StartCoroutine(ImageLoader.LoadImage(CommonData.SpotImageFullPath + info.DataDetails[currentIndex], img, ImageLoadCompleted)); ShowNextPrivButton(); } } void ImageLoadCompleted(Texture2D tex, RawImage img) { img.texture = tex; if (tex == null) { img.texture = ErrorImage; } Dofit(); img.rectTransform.sizeDelta *= currentLarge; img.color = Color.white; if (info.IsType(DataType.Image) || info.IsType(DataType.ImageText)) { txtTitle.text = info.Title; } else { txtTitle.text = TipPointCollider.Instance.GetImageShow(info, currentIndex); } if (txtDescription != null) { txtDescription.text = info.DataDetails[0]; } } /// /// 图像最大高度 /// public float maxHeight = 520.0f; /// /// 图像最大宽度 /// public float maxWidth = 1680.0f; /// /// 显示上半部分的默认宽度 /// public float defaultWidth = 800.0f; /// /// 显示上半部分的比例值 /// public float showTopScale = 2.0f; /// /// 图像太高时是否只显示上半部分 /// public bool showTop = true; /// /// 图像自适应 /// void Dofit() { float width, height; Texture tex = img.texture; float tw = tex.width; float th = tex.height; if ( showTop && th / tw > showTopScale) { width = defaultWidth; height = th * defaultWidth / tw; } else { width = maxWidth; height = th * maxWidth / tw; if (height > maxHeight) { height = maxHeight; width = tw * maxHeight / th; } } RectTransform tranImg = img.GetComponent(); tranImg.sizeDelta = new Vector2(width, height); tranImg.anchoredPosition = new Vector2(0, (maxHeight - height) / 2); } /// /// 播放|暂停音频按钮单击 /// public void OnBtnSoundClick() { isPlaying = !isPlaying; if (isPlaying) { if (soundName.Contains(".")) { soundName = soundName.Substring(0, soundName.IndexOf(".")); } //AudioManager.Instance.PlayAudio(soundName); } else { //AudioManager.Instance.StopAudio(); } btnSound.GetComponent().sprite = isPlaying ? SpriteStop : SpritePlay; } /// /// 关闭按钮单击 /// public void OnBtnCloseClick() { // ViewController.Instance.HideView(); if (anim != null) anim.SetTrigger("CloseBg"); img.texture = null; txtTitle.text = ""; if (txtDescription != null) { txtDescription.text = ""; } // BaseController.CanControl = true; // Invoke("SetControl", 0.2f); BtnController.Instance.SetCanControlDelay(); img.color = Color.clear; ImageLoader.ClearImage(); } /// /// 是否全屏 /// bool isFullScreen; /// /// 全屏按钮单击 /// public void OnBtnFullScreenClick() { if (isFullScreen) { isFullScreen = false; rectBackImage.localScale = Vector3.one * 0.6f; } else { isFullScreen = true; rectBackImage.localScale = Vector3.one; } } public void OnBtnLargeClick() { currentLarge *= 1.1f; if (currentLarge > maxLarge) { currentLarge = maxLarge; } else { img.rectTransform.sizeDelta *= 1.1f; } } public void OnBtnSmallClick() { currentLarge /= 1.1f; if (currentLarge < minLarge) { currentLarge = minLarge; } else { img.rectTransform.sizeDelta /= 1.1f; } } void ShowNextPrivButton() { currentLarge = 1f; if (currentIndex == 0) { btnPriv.gameObject.SetActive(false); if (info.DataDetails.Length > 1) { btnNext.gameObject.SetActive(true); } } else if (currentIndex == info.DataDetails.Length - 1) { btnNext.gameObject.SetActive(false); if (info.DataDetails.Length > 1) { btnPriv.gameObject.SetActive(true); } } else { btnPriv.gameObject.SetActive(true); btnNext.gameObject.SetActive(true); } } public void OnBtnNextClick() { currentIndex++; if (currentIndex >= info.DataDetails.Length) { currentIndex = info.DataDetails.Length - 1; } ShowNextPrivButton(); StartCoroutine(ImageLoader.LoadImage(CommonData.SpotImageFullPath + info.DataDetails[currentIndex], img, ImageLoadCompleted)); // StartCoroutine("GetImage", info.DataDetails[currentIndex]); txtTitle.text = TipPointCollider.Instance.GetImageShow(info, currentIndex); } public void OnBtnPrivClick() { currentIndex--; if (currentIndex < 0) { currentIndex = 0; } ShowNextPrivButton(); StartCoroutine(ImageLoader.LoadImage(CommonData.SpotImageFullPath + info.DataDetails[currentIndex], img, ImageLoadCompleted)); // StartCoroutine("GetImage", info.DataDetails[currentIndex]); txtTitle.text = TipPointCollider.Instance.GetImageShow(info, currentIndex); } private Vector2 finger1Position; private Vector2 finger2Position; float scroll; public float scrollSpeed = 50f; void Update() { // if (imgRequest != null) { // Debug.Log($"{imgRequest.downloadProgress}"); } if (!BaseController.CanControl) { if (Input.touchCount == 2) { Touch touch1 = Input.GetTouch(0); Touch touch2 = Input.GetTouch(1); // 更新手指位置 finger1Position = touch1.position; finger2Position = touch2.position; // 计算当前手指距离和上一帧手指距离的差值,来计算缩放比例 float distance = Vector2.Distance(finger1Position, finger2Position); float previousDistance = Vector2.Distance(finger1Position - touch1.deltaPosition, finger2Position - touch2.deltaPosition); scroll = (previousDistance - distance) / scrollSpeed; } else { scroll = Input.GetAxis("Mouse ScrollWheel"); } // Debug.Log(scroll); if (scroll >= 0.1f) { OnBtnLargeClick(); } else if (scroll <= -0.1f) { OnBtnSmallClick(); } } } }