Files
TaiWan/Assets/ZX/ZX8/Scripts/ZX8Controller.cs

331 lines
9.3 KiB
C#
Raw Normal View History

2025-10-31 15:20:38 +08:00
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>
/// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
/// </summary>
public string DataFileName = "Books.json";
/// <summary>
/// <20><EFBFBD>ֵ<EFBFBD>
/// Key:<3A><EFBFBD><E9BCAE><EFBFBD>ƣ<EFBFBD>Value:<3A><>ҳ<EFBFBD><D2B3>
/// </summary>
public Dictionary<string, int> dicBooks;
/// <summary>
/// ͼƬ·<C6AC><C2B7>
/// </summary>
string ImagePath;
/// <summary>
/// <20>鼮Book<6F><6B><EFBFBD><EFBFBD>
/// </summary>
public Book bookContents;
/// <summary>
/// <20>鼮Ԥ<E9BCAE><D4A4><EFBFBD><EFBFBD>
/// </summary>
public GameObject preBook1;
/// <summary>
/// <20>鼮Ԥ<E9BCAE><D4A4><EFBFBD><EFBFBD>
/// </summary>
public GameObject preBook2;
/// <summary>
/// <20><EFBFBD><E9BCAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public Transform bookParent;
/// <summary>
/// <20><EFBFBD><E9BCAE><EFBFBD><EFBFBD>Text
/// </summary>
public Text txtTitle;
/// <summary>
/// <20>رհ<D8B1>ť
/// </summary>
public Button btnClose;
/// <summary>
/// <20><>ʾѡ<CABE>ť
/// </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>
/// <20><>ȡ<EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public void OnGetBookListCompleted(string data)
{
dicBooks = JsonTools.DicFromJson<string, int>(data);
InitialBooks1();
#region
//dicBooks = new Dictionary<string, int>();
//dicBooks.Add("<22><>¹ԭ", 7);
//dicBooks.Add("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 7);
//dicBooks.Add("<22><><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD>", 5);
//dicBooks.Add("<22><><EFBFBD><EFBFBD>", 4);
//string data = JsonTools.DicToJson(dicBooks);
//StreamWriter writer = File.CreateText(DataFileName);
//writer.Write(data);
//writer.Close();
#endregion
}
/// <summary>
/// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>б<EFBFBD>
/// </summary>
void InitialBooks1()
{
foreach (Transform child in bookParent)
{
Destroy(child.gameObject);
}
int i = 0;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>
foreach (string key in dicBooks.Keys)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BCAE><EFBFBD>󣬲<EFBFBD><F3A3ACB2><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ơ<EFBFBD>λ<EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD>棨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>
/// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>б<EFBFBD>
/// </summary>
void InitialBooks2()
{
foreach (Transform child in bookParent)
{
Destroy(child.gameObject);
}
int i = 0;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>
foreach (string key in dicBooks.Keys)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BCAE><EFBFBD>󣬲<EFBFBD><F3A3ACB2><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ơ<EFBFBD>λ<EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD>棨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>
/// <20><EFBFBD><E9BCAE><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
/// </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>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BCAE><EFBFBD><EFBFBD>
/// </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]]; // <20><><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Sprite<74><65><EFBFBD><EFBFBD>
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>
/// <20>Ƿ<EFBFBD>ȫ<EFBFBD><C8AB>
/// </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;
}
}
}