using CG.Framework; using CG.UTility; using Paroxe.PdfRenderer; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; /******************************************************************************* *Create By CG *Function *******************************************************************************/ namespace ZXK.GYJQR { public class DataShowPanel : UIBase { private Transform _catalogueParent = null; private GameObject _cataItemPrefab = null; private Transform _pdfRawParent = null; private GameObject _pdfItemPrefab = null; private Dictionary> _pdfDic = new Dictionary>(); protected override void Awake() { base.Awake(); _catalogueParent = GetWedage("Catalogue_N").transform; _cataItemPrefab = GetWedage("calaItemPrefab_N"); _pdfRawParent = GetWedage("pdfRawContent_N").transform; _pdfItemPrefab = GetWedage("pdfRawPrefab_N"); AddEventListener("ExitDataShowBtn_N", UIEventType.OnButtonClick, () => { UI_Manage.Instance.ClosePanel("DataShowPanel"); GameManager.Instance._StateContext.SetState( new ModelSelectState(GameManager.Instance._StateContext)); }); string[] pdfArray = new string[2] { "Documentation", "wcl" }; //string[] pdfArray = Directory.GetFiles(Application.streamingAssetsPath + ConstCtrl.FOLDER_PDF_PATH, "*.pdf"); if (pdfArray.Length > 0) { for (int i = 0; i < pdfArray.Length; i++) { GameObject cataGeo = Instantiate(_cataItemPrefab, _catalogueParent); //string pdfName = Path.GetFileNameWithoutExtension(pdfArray[i]); cataGeo.name = pdfArray[i]; cataGeo.transform.Find("calaTxt").GetComponent().text = pdfArray[i]; cataGeo.SetActive(true); cataGeo.GetComponent().onValueChanged.AddListener((bool isOn) => { string pdfPath = Application.streamingAssetsPath + ConstCtrl.FOLDER_PDF_PATH + cataGeo.name + ".pdf"; if (isOn) { if (!_pdfDic.ContainsKey(pdfPath)) { List temp = new List(); WDebug.Log("url:" + pdfPath); MonoManager.Instance.StartCoroutine(ReadPDF(pdfPath,(List tempPDFArray) => { WDebug.Log("tempPDFArray:" + tempPDFArray.Count); for (int i = 0; i < tempPDFArray.Count; i++) { Texture2D itemPic = tempPDFArray[i]; GameObject pdfItemGeo = Instantiate(_pdfItemPrefab, _pdfRawParent); pdfItemGeo.GetComponent().texture = itemPic; pdfItemGeo.name = i.ToString(); temp.Add(pdfItemGeo); } _pdfDic.Add(pdfPath, temp); foreach (GameObject item in _pdfDic[pdfPath]) { item.SetActive(true); } _pdfRawParent.parent.parent.GetComponent().normalizedPosition = new Vector2(0, 1); })); } else { foreach (GameObject item in _pdfDic[pdfPath]) { item.SetActive(true); } } } else { if (_pdfDic.ContainsKey(pdfPath)) { foreach (GameObject item in _pdfDic[pdfPath]) { item.SetActive(false); } } } }); } Transform firstTran = _catalogueParent.Find(Path.GetFileNameWithoutExtension(pdfArray[0])); if(firstTran) firstTran.GetComponent().isOn = true; } } #if !UNITY_WEBGL private IEnumerator ReadPDF(string pdfFilePath, System.Action> action) { List pdfQueue = new List(); PDFDocument pdfDocument; byte[] bytesDocument = null; UnityWebRequest request = UnityWebRequest.Get(pdfFilePath); yield return request.SendWebRequest(); if (!request.isNetworkError && request.isDone) { bytesDocument = request.downloadHandler.data; } pdfDocument = new PDFDocument(bytesDocument); //pdfDocument = new PDFDocument(pdfFilePath); if (pdfDocument.IsValid) { int pageCount = pdfDocument.GetPageCount(); PDFRenderer renderer = new PDFRenderer(); for (int i = 0; i < pageCount; i++) { PDFPage page = pdfDocument.GetPage(i); Texture2D tex = renderer.RenderPageToTexture(page, (int)page.GetPageSize().x, (int)page.GetPageSize().y); tex.filterMode = FilterMode.Bilinear; tex.anisoLevel = 8; pdfQueue.Add(tex); } } action?.Invoke(pdfQueue); } #else private IEnumerator ReadPDF(string url, System.Action> call) { List pdfQueue = new List(); PDFDocument pdfDocument = null; Paroxe.PdfRenderer.WebGL.PDFJS_Promise documentPromise = null; documentPromise = PDFDocument.LoadDocumentFromUrlAsync(url); while (!documentPromise.HasFinished) yield return null; WDebug.Log("documentPromise.HasFinished"); if (documentPromise.HasSucceeded) { WDebug.Log("pagePromise.HasSucceeded"); pdfDocument = documentPromise.Result as PDFDocument; if (pdfDocument.IsValid) { int pageCount = pdfDocument.GetPageCount(); PDFRenderer renderer = new PDFRenderer(); WDebug.Log("一共有:" + pageCount); for (int i = 0; i < pageCount; i++) { Paroxe.PdfRenderer.WebGL.PDFJS_Promise pagePromise = pdfDocument.GetPageAsync(i); WDebug.Log("pagePromise.Start"); while (!pagePromise.HasFinished) yield return null; if (pagePromise.HasSucceeded) { WDebug.Log("pagePromise.HasSucceeded"); PDFPage page = pagePromise.Result; Vector2 size = page.GetPageSize(1.0f); Paroxe.PdfRenderer.WebGL.PDFJS_Promise renderPromise = PDFRenderer.RenderPageToTextureAsync(pagePromise.Result, (int)size.x, (int)size.y); while (!renderPromise.HasFinished) yield return null; if (renderPromise.HasSucceeded) { Texture2D tex = renderPromise.Result; tex.filterMode = FilterMode.Bilinear; tex.anisoLevel = 8; pdfQueue.Add(tex); } } // PDFPage page = pdfDocument.GetPage(i); //Texture2D tex = renderer.RenderPageToTexture(page, (int)page.GetPageSize().x, (int)page.GetPageSize().y); //tex.filterMode = FilterMode.Bilinear; //tex.anisoLevel = 8; //pdfQueue.Add(tex); } } call?.Invoke(pdfQueue); } else { WDebug.Log("输出文档出错" + url); } } #endif } }