WXMC/.svn/pristine/fb/fbe555d18a5726f5dcfa6d99c7d5c38bf7836b95.svn-base
2024-12-04 16:18:46 +08:00

113 lines
3.6 KiB
Plaintext

using System.Collections;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class PuzzleExplainUI : MonoBehaviour
{
[SerializeField]
private PuzzleCardExplainItem m_cardExplainItemPrefab;
[SerializeField]
private Transform m_cardExplainItemParent;
[SerializeField]
private HorizontalLayoutGroup m_horizontalLayoutGroup;
[SerializeField]
private Transform m_bg;
[SerializeField]
public GameObject questionBoard;
private List<PuzzleCardExplainItem> puzzleCardItemList;
private List<PuzzleTarotCardItem> puzzleTarotCardList;
private JArray cards;
public void Init(List<PuzzleTarotCardItem> puzzleTarotCardItemList)
{
GetTextData();
puzzleTarotCardList = puzzleTarotCardItemList;
m_horizontalLayoutGroup.enabled = true;
gameObject.SetActive(true);
if(puzzleCardItemList==null)
{
puzzleCardItemList= new List<PuzzleCardExplainItem>();
}
for(int j=0;j<puzzleCardItemList.Count;j++)
{
Destroy(puzzleCardItemList[j].gameObject);
}
puzzleCardItemList.Clear();
for(int j=0;j<puzzleTarotCardItemList.Count;j++)
{
GameObject go = Instantiate(m_cardExplainItemPrefab.gameObject,m_cardExplainItemParent);
PuzzleCardExplainItem currItem = go.GetComponent<PuzzleCardExplainItem>();
string currContent = GetTextWithType(puzzleTarotCardItemList[j].TarotCard);
currItem.Init(puzzleTarotCardItemList[j],currContent,ShowQuestionUI);
puzzleCardItemList.Add(currItem);
}
StartCoroutine(DelaySetLayoutDisable());
}
IEnumerator DelaySetLayoutDisable()
{
yield return new WaitForSeconds(0.1f);
m_horizontalLayoutGroup.enabled = false;
for(int j=0;j<puzzleCardItemList.Count;j++)
{
puzzleCardItemList[j].transform.SetParent(m_bg);
}
}
void GetTextData()
{
JObject obj = JObject.Parse(ReadData("Tarots"));
cards = (JArray)obj["cards"];
}
string GetTextWithType(PuzzleTarotCard tarotType)
{
//var card = cards[(int)tarotType];
return "Main Representation:\n" + tarotType.MainRepresentations + "\n" +
"Advice:\n" + tarotType.Advice + "\n" +
"Description:\n" + tarotType.Description;
}
void ShowQuestionUI(PuzzleCardExplainItem cardExplainItem)
{
questionBoard.SetActive(true);
questionBoard.GetComponent<PuzzleQuestionBoard>().cardExplainItem =cardExplainItem;
}
public void Exit()
{
if(PHomeInventory.instance!=null)
{
for(int j=0;j<puzzleTarotCardList.Count;j++)
{
PHomeInventory.instance.Items.Add(puzzleTarotCardList[j]);
}
}
gameObject.SetActive(false);
}
public string ReadData(string fileName)
{
string readData;
string fileUrl = Application.streamingAssetsPath + "/" + fileName + ".json";
#if UNITY_EDITOR
using (StreamReader sr = File.OpenText(fileUrl))
{
readData = sr.ReadToEnd();
sr.Close();
return readData;
}
#endif
var loadingRequest = UnityWebRequest.Get(fileUrl);
loadingRequest.SendWebRequest();
while (!loadingRequest.isDone && !loadingRequest.isNetworkError && !loadingRequest.isHttpError);
readData = System.Text.Encoding.UTF8.GetString(loadingRequest.downloadHandler.data);
return readData;
}
}