using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 图片展示点
///
public class ImageShow : BaseShow
{
///
/// 显示的材质
///
Material matShow;
///
/// 当前图片索引(针对多图模式)
///
int currentIndex;
Texture textureDefault;
void Start()
{
Renderer renderer = GetComponent();
if (renderer == null && !string.IsNullOrEmpty(info.ParentID))
{
GameObject obj = GameObject.Find(info.ParentID);
if (obj != null)
{
renderer = obj.GetComponent();
}
}
if ( renderer != null && info.PlayOnObject)
{
// playOnObject = true;
matShow = renderer.material;
// matShow = renderer.sharedMaterial;
textureDefault = matShow.mainTexture;
LoadImage();
}
if (info.Clickable)
{
// 添加碰撞器以允许单击
if (GetComponent() == null)
{
gameObject.AddComponent();
}
}
currentIndex = info.Index;
// CanClickStatusChanged += ImageShow_CanClickStatusChanged;
}
private void ImageShow_CanClickStatusChanged()
{
if (info.PlayOnObject)
{
LoadImage();
}
}
string imgName = null;
///
/// 加载图片
///
public void LoadImage()
{
if (info.IsType(DataType.Image))
{
imgName = info.ImageName;
info.CurrentURL = info.URL;
}
else // if (info.Type == DataType.MultiImage) // 多图模式
{
if (info.DataDetails.Length < 0)
{
Debug.Log(string.Format("ObjectID:{0},DataDetails中没有数据", info.ObjectID));
return;
}
if (info.DisplayMode == DisplayMode.RandomPics) // 随机显示多图
{
currentIndex = Random.Range(0, info.DataDetails.Length);
}
// 设置显示的图片和超链接
imgName = info.DataDetails[currentIndex];
info.ImageName = imgName;
if (info.Clickable)
{
if (currentIndex < info.UrlDetails.Length)
{
info.CurrentURL = info.UrlDetails[currentIndex];
info.VideoName = info.CurrentURL;
}
else
{
info.CurrentURL = "";
info.VideoName = info.CurrentURL;
}
if (currentIndex < info.TitleDetails.Length)
{
info.Title = info.TitleDetails[currentIndex];
}
}
if (info.DisplayMode == DisplayMode.MultiPics) // 顺序显示多图
{
currentIndex++;
currentIndex = currentIndex % info.DataDetails.Length;
}
}
if (CanClick)
{
if (gameObject.activeInHierarchy)
{
StartCoroutine(
ImageLoader.LoadImage(CommonData.HighImageFullPath + imgName, matShow)
);
}
// 如果是多图则在设置的显示速度后再加载图片
if (info.IsType(DataType.MultiImage) && info.AutoPlay)
{
Invoke("LoadImage", info.DisplaySpeed);
}
}
else
{
matShow.mainTexture = textureDefault;
if (matShow.IsKeywordEnabled("_EMISSION"))
{
matShow.SetTexture("_EmissionMap", textureDefault);
}
ImageLoader.RemoveImage(CommonData.HighImageFullPath + imgName);
}
}
///
/// 鼠标单击事件
///
public override void OnClick()
{
if (BaseController.CanControl)
{
Invoke("ShowImageEx", 0.01f);
}
}
void ShowImageEx()
{
// 判断是否可以点击
//if (!BaseController.IsJoyStickOnControl
// && !BaseController.IsUIClicked
// && BaseController.CanControl
// && BaseController.IsMouseOnControl
// && CanClick)
if (BaseController.CanControl && CanClick)
{
if (info.PlayOnObject)
{
if (!string.IsNullOrEmpty(info.CurrentURL))
{
// Debug.Log(info.CurrentURL);
// H5Controller.OpenUrl(url);
}
}
else if ((info.IsType(DataType.Image) && !string.IsNullOrEmpty(info.ImageName)) || info.IsType(DataType.MultiImage))
{
ImagePlayerEx.Instance.info = info;
ImagePlayerEx.Instance.ShowImage(info.Index);
}
}
}
}