ImageExtractor.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using TMPro;
  7. using WebSocketSharp;
  8. using System.IO;
  9. using UnityEngine.UI;
  10. public class ImageExtractor : MonoBehaviour
  11. {
  12. public TMP_InputField setIDField;
  13. public GameObject downloadButton;
  14. public GameObject stopButton;
  15. public TMP_Text downloadStatusText;
  16. public Image _img;
  17. private string _setID;
  18. private List<string> _setList = new List<string>(new string[]
  19. {
  20. "ST1",
  21. "ST2",
  22. "ST3",
  23. "ST4",
  24. "ST5",
  25. "ST6",
  26. "ST7",
  27. "ST8",
  28. "ST9",
  29. "ST10",
  30. "ST12",
  31. "ST13",
  32. "ST14",
  33. "ST15",
  34. "ST16",
  35. "ST17",
  36. "BT1",
  37. "BT2",
  38. "BT3",
  39. "BT4",
  40. "BT5",
  41. "BT6",
  42. "BT7",
  43. "BT8",
  44. "BT9",
  45. "BT10",
  46. "BT11",
  47. "BT12",
  48. "BT13",
  49. "BT14",
  50. "BT15",
  51. "BT16",
  52. "EX1",
  53. "EX2",
  54. "EX3",
  55. "EX4",
  56. "EX5",
  57. "RB1",
  58. "LM",
  59. "P"
  60. });
  61. private List<string> _downloadList = new List<string>();
  62. private int _cardID = 0;
  63. private string _cardIDString;
  64. private bool _gatheringSet = false;
  65. private bool _gatheringParrallel = false;
  66. private int _parallelID = 0;
  67. public void OnSetIDChanged(string value)
  68. {
  69. _setID = value.ToUpper();
  70. }
  71. public void OnClickGetEnglishCardImageButton()
  72. {
  73. StopGetCardImages();
  74. _downloadList.Clear();
  75. if (_setID.IsNullOrEmpty())
  76. {
  77. foreach (string set in _setList)
  78. _downloadList.Add(set);
  79. }
  80. else
  81. {
  82. _downloadList.Add(_setID);
  83. }
  84. StartCoroutine(GetSetImages());
  85. }
  86. IEnumerator GetSetImages()
  87. {
  88. while (Application.internetReachability == NetworkReachability.NotReachable)
  89. {
  90. UpdateStatus("Waiting for Internet connection....");
  91. yield return new WaitForSeconds(5f);
  92. }
  93. foreach (string SetID in _downloadList)
  94. {
  95. UpdateStatus($"Getting {SetID} Card Images....");
  96. _cardID = 0;
  97. _gatheringSet = true;
  98. while (_gatheringSet)
  99. {
  100. _cardID++;
  101. _cardIDString = string.Format("{0:000}", _cardID);
  102. if (SetID.Contains("ST"))
  103. _cardIDString = string.Format("{0:00}", _cardID);
  104. string cardImageURL = $"{SetID}-{_cardIDString}";
  105. yield return StartCoroutine(GetCardImage(cardImageURL));
  106. _parallelID = 0;
  107. _gatheringParrallel = true;
  108. while (_gatheringParrallel)
  109. {
  110. _parallelID++;
  111. yield return StartCoroutine(GetCardImage(cardImageURL + $"_P{_parallelID}"));
  112. }
  113. }
  114. UpdateStatus($"Completed {SetID} Images");
  115. }
  116. OnCompleteImages();
  117. UpdateStatus($"Completed ALL Images");
  118. yield return null;
  119. }
  120. IEnumerator GetCardImage(string cardID)
  121. {
  122. string picsURL = $"https://world.digimoncard.com/images/cardlist/card/{cardID}.png";
  123. string ImagePath = Path.GetFullPath(Path.Combine(Application.dataPath, @"..\..\Textures\Card\", $"{cardID}.png"));
  124. if(File.Exists(ImagePath))
  125. {
  126. Debug.Log($"File Exists: {cardID}");
  127. yield break;
  128. }
  129. UnityWebRequest webReq_CardImage = UnityWebRequestTexture.GetTexture(picsURL);
  130. yield return webReq_CardImage.SendWebRequest();
  131. if (webReq_CardImage.result == UnityWebRequest.Result.Success)
  132. {
  133. try
  134. {
  135. UpdateStatus($"Image Complete: {ImagePath}");
  136. File.WriteAllBytes(ImagePath, webReq_CardImage.downloadHandler.data);
  137. Texture2D texture = DownloadHandlerTexture.GetContent(webReq_CardImage);
  138. Sprite s = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),
  139. Vector2.zero, 1f);
  140. _img.sprite = s;
  141. }
  142. catch (Exception ex)
  143. {
  144. Debug.Log(ex.Message);
  145. }
  146. }
  147. else
  148. {
  149. //failed to load
  150. if (webReq_CardImage.error.Contains("404"))
  151. {
  152. if (cardID.Contains("_P"))
  153. _gatheringParrallel = false;
  154. else
  155. _gatheringSet = false;
  156. Debug.Log("Card does not exsist....moving on");
  157. yield break;
  158. }
  159. Debug.Log($"Failed to load: {webReq_CardImage.error}");
  160. }
  161. yield return null;
  162. }
  163. private void UpdateStatus(string status)
  164. {
  165. downloadStatusText.text = status;
  166. }
  167. private void OnCompleteImages()
  168. {
  169. downloadButton.SetActive(true);
  170. stopButton.SetActive(false);
  171. _img.sprite = null;
  172. UpdateStatus("");
  173. }
  174. public void StopGetCardImages()
  175. {
  176. StopAllCoroutines();
  177. _img.sprite = null;
  178. UpdateStatus("");
  179. }
  180. }