331 lines
9.3 KiB
C#
331 lines
9.3 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using DG.Tweening;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using UnityEngine.Networking;
|
||
using UnityEngine.UI;
|
||
|
||
public class ZX8Controller : MonoBehaviour
|
||
{
|
||
public static ZX8Controller Instance;
|
||
|
||
private void Awake()
|
||
{
|
||
Instance = this;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 数据文件
|
||
/// </summary>
|
||
public string DataFileName = "Books.json";
|
||
/// <summary>
|
||
/// 书籍字典
|
||
/// Key:书籍名称,Value:总页数
|
||
/// </summary>
|
||
public Dictionary<string, int> dicBooks;
|
||
/// <summary>
|
||
/// 图片路径
|
||
/// </summary>
|
||
string ImagePath;
|
||
/// <summary>
|
||
/// 书籍Book组件
|
||
/// </summary>
|
||
public Book bookContents;
|
||
/// <summary>
|
||
/// 书籍预设体
|
||
/// </summary>
|
||
public GameObject preBook1;
|
||
|
||
/// <summary>
|
||
/// 书籍预设体
|
||
/// </summary>
|
||
public GameObject preBook2;
|
||
|
||
/// <summary>
|
||
/// 书籍生成所在父对象
|
||
/// </summary>
|
||
public Transform bookParent;
|
||
/// <summary>
|
||
/// 书籍标题Text
|
||
/// </summary>
|
||
public Text txtTitle;
|
||
/// <summary>
|
||
/// 关闭按钮
|
||
/// </summary>
|
||
public Button btnClose;
|
||
|
||
/// <summary>
|
||
/// 显示选项按钮
|
||
/// </summary>
|
||
public Button btnShowOp;
|
||
|
||
public Sprite spShowOp1;
|
||
public Sprite spShowOp2;
|
||
|
||
public Animator anim;
|
||
|
||
string basePath;
|
||
|
||
void Start()
|
||
{
|
||
basePath = CommonData.DataServer + "/ZX8/";
|
||
|
||
StartCoroutine(JsonLoader.LoadJSON(basePath + "Data/" + DataFileName, OnGetBookListCompleted));
|
||
|
||
if (btnClose != null)
|
||
btnClose.onClick.AddListener(OnBtnCloseClick);
|
||
|
||
if (btnShowOp != null)
|
||
btnShowOp.onClick.AddListener(OnBtnShowOpClick);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取书籍列表结束
|
||
/// </summary>
|
||
public void OnGetBookListCompleted(string data)
|
||
{
|
||
dicBooks = JsonTools.DicFromJson<string, int>(data);
|
||
InitialBooks1();
|
||
|
||
#region
|
||
//dicBooks = new Dictionary<string, int>();
|
||
//dicBooks.Add("白鹿原", 7);
|
||
//dicBooks.Add("红与黑", 7);
|
||
//dicBooks.Add("狂人日记", 5);
|
||
//dicBooks.Add("年轮", 4);
|
||
|
||
//string data = JsonTools.DicToJson(dicBooks);
|
||
//StreamWriter writer = File.CreateText(DataFileName);
|
||
//writer.Write(data);
|
||
//writer.Close();
|
||
#endregion
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化书籍列表
|
||
/// </summary>
|
||
void InitialBooks1()
|
||
{
|
||
foreach (Transform child in bookParent)
|
||
{
|
||
Destroy(child.gameObject);
|
||
}
|
||
|
||
int i = 0;
|
||
// 遍历所有的书籍
|
||
foreach (string key in dicBooks.Keys)
|
||
{
|
||
// 创建书籍对象,并初始化名称、位置及封面(0.jpg)
|
||
GameObject obj = Instantiate(preBook1, bookParent);
|
||
obj.name = key;
|
||
obj.transform.position -= Vector3.up * i * 280f;
|
||
StartCoroutine(
|
||
ImageLoader<Image>.LoadImage(
|
||
string.Format("{0}Books/{1}/{2}.jpg", basePath, key, 0),
|
||
obj.transform.GetChild(0).GetComponent<Image>()));
|
||
obj.transform.GetChild(0).GetComponent<Button>().onClick.AddListener(OnImageBookClick);
|
||
obj.transform.GetChild(1).GetComponent<Text>().text = key;
|
||
|
||
if (currentSelect != null && key == currentSelect.name)
|
||
{
|
||
obj.GetComponent<Image>().color = Color.gray;
|
||
obj.transform.GetChild(1).GetComponent<Text>().color = Color.black;
|
||
obj.transform.GetChild(1).GetComponent<Text>().fontStyle = FontStyle.Bold;
|
||
currentSelect = obj;
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 初始化书籍列表
|
||
/// </summary>
|
||
void InitialBooks2()
|
||
{
|
||
foreach (Transform child in bookParent)
|
||
{
|
||
Destroy(child.gameObject);
|
||
}
|
||
|
||
int i = 0;
|
||
// 遍历所有的书籍
|
||
foreach (string key in dicBooks.Keys)
|
||
{
|
||
// 创建书籍对象,并初始化名称、位置及封面(0.jpg)
|
||
GameObject obj = Instantiate(preBook2, bookParent);
|
||
obj.name = key;
|
||
obj.transform.position -= Vector3.up * i * 280f;
|
||
StartCoroutine(
|
||
ImageLoader<Image>.LoadImage(
|
||
string.Format("{0}Books/{1}/{2}.jpg", basePath, key, 0),
|
||
obj.transform.GetChild(0).GetComponent<Image>()));
|
||
obj.transform.GetChild(1).GetComponent<Text>().text = key;
|
||
obj.transform.GetChild(1).GetComponent<Button>().onClick.AddListener(OnImageBookClick);
|
||
|
||
if (currentSelect != null && key == currentSelect.name)
|
||
{
|
||
obj.GetComponent<Image>().color = Color.gray;
|
||
obj.transform.GetChild(1).GetComponent<Text>().color = Color.black;
|
||
obj.transform.GetChild(1).GetComponent<Text>().fontStyle = FontStyle.Bold;
|
||
currentSelect = obj;
|
||
}
|
||
}
|
||
}
|
||
|
||
bool showOp1 = true;
|
||
public void OnBtnShowOpClick()
|
||
{
|
||
if (showOp1)
|
||
{
|
||
InitialBooks2();
|
||
}
|
||
else
|
||
{
|
||
InitialBooks1();
|
||
}
|
||
showOp1 = !showOp1;
|
||
btnShowOp.GetComponent<Image>().sprite = showOp1 ? spShowOp2 : spShowOp1;
|
||
}
|
||
|
||
bool isFirstClick = true;
|
||
/// <summary>
|
||
/// 书籍单击事件
|
||
/// </summary>
|
||
void OnImageBookClick()
|
||
{
|
||
GameObject obj = EventSystem.current.currentSelectedGameObject.transform.parent.gameObject;
|
||
|
||
if (isFirstClick)
|
||
{
|
||
isFirstClick = false;
|
||
|
||
anim.SetTrigger("OpenBook");
|
||
}
|
||
else
|
||
{
|
||
anim.SetTrigger("BookClick");
|
||
}
|
||
|
||
LoadBookContents(obj);
|
||
}
|
||
|
||
GameObject currentSelect;
|
||
/// <summary>
|
||
/// 加载书籍内容
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
void LoadBookContents(GameObject obj)
|
||
{
|
||
if (obj != currentSelect)
|
||
{
|
||
if (currentSelect != null)
|
||
{
|
||
string key = null;
|
||
for (int i = 1; i < dicBooks[currentSelect.name]; i++)
|
||
{
|
||
key = string.Format("{0}Books/{1}/{2}.jpg", basePath, currentSelect.name, i);
|
||
if (dicTmp.ContainsKey(key))
|
||
{
|
||
Destroy(dicTmp[key]);
|
||
dicTmp.Remove(key);
|
||
}
|
||
}
|
||
}
|
||
|
||
txtTitle.text = obj.name;
|
||
|
||
Sprite[] spPages = new Sprite[dicBooks[obj.name]]; // 根据页数创建Sprite数组
|
||
bookContents.bookPages = spPages;
|
||
bookContents.currentPage = 0;
|
||
bookContents.LeftNext.sprite = bookContents.bgLeft;
|
||
bookContents.RightNext.sprite = obj.transform.GetChild(0).GetComponent<Image>().sprite;
|
||
|
||
if (currentSelect != null)
|
||
{
|
||
currentSelect.GetComponent<Image>().color = Color.white;
|
||
currentSelect.transform.GetChild(1).GetComponent<Text>().color = Color.gray;
|
||
currentSelect.transform.GetChild(1).GetComponent<Text>().fontStyle = FontStyle.Normal;
|
||
}
|
||
obj.GetComponent<Image>().color = Color.gray;
|
||
obj.transform.GetChild(1).GetComponent<Text>().color = Color.black;
|
||
obj.transform.GetChild(1).GetComponent<Text>().fontStyle = FontStyle.Bold;
|
||
|
||
currentSelect = obj;
|
||
|
||
spPages[0] = bookContents.RightNext.sprite;
|
||
|
||
LoadNext2PageImage(0);
|
||
}
|
||
}
|
||
|
||
public void LoadNext2PageImage(int index)
|
||
{
|
||
if ((index + 1) < dicBooks[currentSelect.name])
|
||
{
|
||
StartCoroutine(GetImage(string.Format("{0}Books/{1}/{2}.jpg", basePath, currentSelect.name, index + 1), index + 1));
|
||
}
|
||
if ((index + 2) < dicBooks[currentSelect.name])
|
||
{
|
||
StartCoroutine(GetImage(string.Format("{0}Books/{1}/{2}.jpg", basePath, currentSelect.name, index + 2), index + 2));
|
||
}
|
||
}
|
||
|
||
Dictionary<string, Texture2D> dicTmp = new Dictionary<string, Texture2D>();
|
||
IEnumerator GetImage(string imgName, int index)
|
||
{
|
||
// Debug.Log(imgName);
|
||
Texture2D tex = null;
|
||
if (dicTmp.ContainsKey(imgName))
|
||
{
|
||
tex = dicTmp[imgName];
|
||
}
|
||
else
|
||
{
|
||
UnityWebRequest www = UnityWebRequestTexture.GetTexture(imgName);
|
||
yield return www.SendWebRequest();
|
||
if (www.result == UnityWebRequest.Result.ConnectionError)
|
||
{
|
||
Debug.Log(www.error);
|
||
}
|
||
tex = ((DownloadHandlerTexture)www.downloadHandler).texture;
|
||
dicTmp.Add(imgName, tex);
|
||
}
|
||
|
||
bookContents.bookPages[index] = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
|
||
}
|
||
|
||
public void OnBtnCloseClick()
|
||
{
|
||
BaseController.HideView();
|
||
|
||
Destroy(gameObject.transform.parent.gameObject);
|
||
|
||
ImageLoader<Image>.ClearImage();
|
||
foreach (var item in dicTmp.Keys)
|
||
{
|
||
Destroy(dicTmp[item]);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否全屏
|
||
/// </summary>
|
||
bool isFullScreen;
|
||
|
||
public RectTransform rectMain;
|
||
|
||
public void OnBtnFullScreenClick()
|
||
{
|
||
if (isFullScreen)
|
||
{
|
||
isFullScreen = false;
|
||
rectMain.localScale = Vector3.one * 0.6f;
|
||
}
|
||
else
|
||
{
|
||
isFullScreen = true;
|
||
rectMain.localScale = Vector3.one;
|
||
}
|
||
}
|
||
}
|