181 lines
4.9 KiB
C#
181 lines
4.9 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
/// <summary>
|
||
/// 图片展示点
|
||
/// </summary>
|
||
public class ImageShow : BaseShow
|
||
{
|
||
/// <summary>
|
||
/// 显示的材质
|
||
/// </summary>
|
||
Material matShow;
|
||
|
||
/// <summary>
|
||
/// 当前图片索引(针对多图模式)
|
||
/// </summary>
|
||
int currentIndex;
|
||
|
||
Texture textureDefault;
|
||
|
||
void Start()
|
||
{
|
||
Renderer renderer = GetComponent<Renderer>();
|
||
if (renderer == null && !string.IsNullOrEmpty(info.ParentID))
|
||
{
|
||
GameObject obj = GameObject.Find(info.ParentID);
|
||
if (obj != null)
|
||
{
|
||
renderer = obj.GetComponent<Renderer>();
|
||
}
|
||
}
|
||
|
||
if ( renderer != null && info.PlayOnObject)
|
||
{
|
||
// playOnObject = true;
|
||
matShow = renderer.material;
|
||
// matShow = renderer.sharedMaterial;
|
||
textureDefault = matShow.mainTexture;
|
||
LoadImage();
|
||
}
|
||
|
||
if (info.Clickable)
|
||
{
|
||
// 添加碰撞器以允许单击
|
||
if (GetComponent<Collider>() == null)
|
||
{
|
||
gameObject.AddComponent<MeshCollider>();
|
||
}
|
||
}
|
||
|
||
currentIndex = info.Index;
|
||
|
||
// CanClickStatusChanged += ImageShow_CanClickStatusChanged;
|
||
}
|
||
|
||
private void ImageShow_CanClickStatusChanged()
|
||
{
|
||
if (info.PlayOnObject)
|
||
{
|
||
LoadImage();
|
||
}
|
||
}
|
||
|
||
string imgName = null;
|
||
|
||
/// <summary>
|
||
/// 加载图片
|
||
/// </summary>
|
||
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<Material>.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<Material>.RemoveImage(CommonData.HighImageFullPath + imgName);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 鼠标单击事件
|
||
/// </summary>
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
} |