478 lines
12 KiB
C#
478 lines
12 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// 图像控制类
|
|
/// </summary>
|
|
public class ImagePlayerEx : MonoBehaviour
|
|
{
|
|
public static ImagePlayerEx Instance;
|
|
// public UnityWebRequest imgRequest;
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
public Data info;
|
|
|
|
/// <summary>
|
|
/// BackImage的RectTransform组件
|
|
/// </summary>
|
|
public RectTransform rectBackImage;
|
|
/// <summary>
|
|
/// 图像
|
|
/// </summary>
|
|
public RawImage img;
|
|
/// <summary>
|
|
/// 音频播放图片
|
|
/// </summary>
|
|
public Sprite SpritePlay;
|
|
/// <summary>
|
|
/// 音频停止图片
|
|
/// </summary>
|
|
public Sprite SpriteStop;
|
|
|
|
public Texture2D ErrorImage;
|
|
/// <summary>
|
|
/// 是否在播放
|
|
/// </summary>
|
|
bool isPlaying;
|
|
/// <summary>
|
|
/// 全屏按钮
|
|
/// </summary>
|
|
public Button btnFullscreen;
|
|
/// <summary>
|
|
/// 关闭按钮
|
|
/// </summary>
|
|
public Button btnClose;
|
|
/// <summary>
|
|
/// 关闭按钮
|
|
/// </summary>
|
|
public Button btnSound;
|
|
/// <summary>
|
|
/// 下一张图片按钮
|
|
/// </summary>
|
|
public Button btnNext;
|
|
/// <summary>
|
|
/// 上一张图片按钮
|
|
/// </summary>
|
|
public Button btnPriv;
|
|
/// <summary>
|
|
/// 放大图片按钮
|
|
/// </summary>
|
|
public Button btnLarge;
|
|
/// <summary>
|
|
/// 缩小图片按钮
|
|
/// </summary>
|
|
public Button btnSmall;
|
|
/// <summary>
|
|
/// 数据标题
|
|
/// </summary>
|
|
public Text txtTitle;
|
|
/// <summary>
|
|
/// 数据描述
|
|
/// </summary>
|
|
public Text txtDescription;
|
|
/// <summary>
|
|
/// 音乐名
|
|
/// </summary>
|
|
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<Animator>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 隐藏图像显示窗口
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 动画结束隐藏对象、暂停音频播放、还原播放按钮图标
|
|
/// </summary>
|
|
void HideComplete()
|
|
{
|
|
// AudioManager.Instance.StopAudio();
|
|
btnSound.GetComponent<Image>().sprite = SpritePlay;
|
|
IsShow = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 显示图像
|
|
/// </summary>
|
|
/// <param name="data">图像数据</param>
|
|
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<RawImage>.LoadImage(CommonData.SpotImageFullPath + info.ImageName, img, ImageLoadCompleted));
|
|
|
|
btnNext.gameObject.SetActive(false);
|
|
btnPriv.gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
StartCoroutine(ImageLoader<RawImage>.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];
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 图像最大高度
|
|
/// </summary>
|
|
public float maxHeight = 520.0f;
|
|
/// <summary>
|
|
/// 图像最大宽度
|
|
/// </summary>
|
|
public float maxWidth = 1680.0f;
|
|
|
|
/// <summary>
|
|
/// 显示上半部分的默认宽度
|
|
/// </summary>
|
|
public float defaultWidth = 800.0f;
|
|
|
|
/// <summary>
|
|
/// 显示上半部分的比例值
|
|
/// </summary>
|
|
public float showTopScale = 2.0f;
|
|
|
|
/// <summary>
|
|
/// 图像太高时是否只显示上半部分
|
|
/// </summary>
|
|
public bool showTop = true;
|
|
|
|
/// <summary>
|
|
/// 图像自适应
|
|
/// </summary>
|
|
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<RectTransform>();
|
|
tranImg.sizeDelta = new Vector2(width, height);
|
|
tranImg.anchoredPosition = new Vector2(0, (maxHeight - height) / 2);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 播放|暂停音频按钮单击
|
|
/// </summary>
|
|
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<Image>().sprite = isPlaying ? SpriteStop : SpritePlay;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭按钮单击
|
|
/// </summary>
|
|
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<RawImage>.ClearImage();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否全屏
|
|
/// </summary>
|
|
bool isFullScreen;
|
|
|
|
/// <summary>
|
|
/// 全屏按钮单击
|
|
/// </summary>
|
|
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<RawImage>.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<RawImage>.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();
|
|
}
|
|
}
|
|
}
|
|
}
|