204 lines
8.8 KiB
C#
204 lines
8.8 KiB
C#
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<string, List<GameObject>> _pdfDic = new Dictionary<string, List<GameObject>>();
|
||
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>().text = pdfArray[i];
|
||
cataGeo.SetActive(true);
|
||
cataGeo.GetComponent<Toggle>().onValueChanged.AddListener((bool isOn) =>
|
||
{
|
||
string pdfPath = Application.streamingAssetsPath + ConstCtrl.FOLDER_PDF_PATH + cataGeo.name + ".pdf";
|
||
if (isOn)
|
||
{
|
||
if (!_pdfDic.ContainsKey(pdfPath))
|
||
{
|
||
List<GameObject> temp = new List<GameObject>();
|
||
WDebug.Log("url:" + pdfPath);
|
||
MonoManager.Instance.StartCoroutine(ReadPDF(pdfPath,(List<Texture2D> 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<RawImage>().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<ScrollRect>().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<Toggle>().isOn = true;
|
||
}
|
||
}
|
||
#if !UNITY_WEBGL
|
||
private IEnumerator ReadPDF(string pdfFilePath, System.Action<List<Texture2D>> action)
|
||
{
|
||
List<Texture2D> pdfQueue = new List<Texture2D>();
|
||
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<List<Texture2D>> call)
|
||
{
|
||
List<Texture2D> pdfQueue = new List<Texture2D>();
|
||
PDFDocument pdfDocument = null;
|
||
Paroxe.PdfRenderer.WebGL.PDFJS_Promise<PDFDocument> 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("һ<><D2BB><EFBFBD>У<EFBFBD>" + pageCount);
|
||
for (int i = 0; i < pageCount; i++)
|
||
{
|
||
Paroxe.PdfRenderer.WebGL.PDFJS_Promise<PDFPage> 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<Texture2D> 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("<22><><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD>" + url);
|
||
}
|
||
}
|
||
#endif
|
||
|
||
}
|
||
} |