StreamingAssetsUtility.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using UnityEngine;
  2. using System.IO;
  3. using System;
  4. using System.Threading.Tasks;
  5. using UnityEngine.Networking;
  6. using WebP;
  7. public class StreamingAssetsUtility
  8. {
  9. public static async Task<byte[]> ReadFile(string path)
  10. {
  11. using (FileStream fileStream = new FileStream(
  12. path, FileMode.Open, FileAccess.Read))
  13. {
  14. var resultBytes = new byte[fileStream.Length];
  15. await fileStream.ReadAsync(resultBytes, 0, (int)fileStream.Length);
  16. return resultBytes;
  17. }
  18. }
  19. #region 画像の取得
  20. public static Texture2D BinaryToTexture(byte[] bytes)
  21. {
  22. Texture2D texture = new Texture2D(1, 1);
  23. texture.LoadImage(bytes);
  24. return texture;
  25. }
  26. public static async Task<Sprite> GetSprite(string fileName, bool isCard = false, bool isLauncher = false)
  27. {
  28. string path = "";
  29. if (isCard)
  30. {
  31. if (fileName.Contains("-token"))
  32. {
  33. return await GetTokenImageData(Path.Combine(GetStreamingAssetPath(isLauncher), $"Card/{fileName}.png").Replace("\\", "/"));
  34. }
  35. else
  36. {
  37. path = Path.Combine(GetStreamingAssetPath(isLauncher), $"Card/{fileName}.webp").Replace("\\", "/");
  38. if (!File.Exists(path))
  39. {
  40. return await GetCardImageData(fileName, path);
  41. }
  42. else
  43. {
  44. return await GetCardImageDataLocal(path);
  45. }
  46. }
  47. }
  48. else
  49. {
  50. return await GetSpriteImage(fileName, isLauncher);
  51. }
  52. /* if (isCard)
  53. {
  54. if (fileName.Contains("-token"))
  55. {
  56. return await GetTokenImageData(Path.Combine(GetStreamingAssetPath(isLauncher), $"Card/{fileName}.png").Replace("\\", "/"));
  57. }
  58. else
  59. {
  60. }
  61. }
  62. else
  63. {
  64. path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.jpg").Replace("\\", "/");
  65. if (!File.Exists(path))
  66. path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.png").Replace("\\", "/");
  67. if (File.Exists(path))
  68. {
  69. byte[] imageBuff = await ReadFile(path);
  70. Texture2D tex = BinaryToTexture(imageBuff);
  71. Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
  72. return sprite;
  73. }
  74. }
  75. await Task.Yield();
  76. if (File.Exists(path))
  77. {
  78. byte[] imageBuff = await ReadFile(path);
  79. Texture2D tex;
  80. Sprite sprite = null;
  81. if (isCard)
  82. {
  83. tex = Texture2DExt.CreateTexture2DFromWebP(imageBuff, lMipmaps: true, lLinear: false, lError: out WebP.Error lError);
  84. if (lError == WebP.Error.Success)
  85. {
  86. sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
  87. }
  88. else
  89. {
  90. Debug.LogError("Webp Load Error : " + lError.ToString());
  91. }
  92. }
  93. else
  94. {
  95. tex = BinaryToTexture(imageBuff);
  96. sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
  97. }
  98. return sprite;
  99. }*/
  100. return null;
  101. }
  102. public static async Task<Sprite> GetSpriteImage(string fileName, bool isLauncher = false)
  103. {
  104. string path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.jpg").Replace("\\", "/");
  105. if(!File.Exists(path))
  106. path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.png").Replace("\\", "/");
  107. if (File.Exists(path))
  108. {
  109. byte[] imageBuff = await ReadFile(path);
  110. Texture2D tex = BinaryToTexture(imageBuff);
  111. Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
  112. return sprite;
  113. }
  114. return null;
  115. }
  116. public static async Task<Sprite> GetTokenImageData(string path)
  117. {
  118. if (File.Exists(path))
  119. {
  120. byte[] imageBuff = await ReadFile(path);
  121. Texture2D tex = BinaryToTexture(imageBuff);
  122. Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
  123. return sprite;
  124. }
  125. return null;
  126. }
  127. public static async Task<Sprite> GetCardImageDataLocal(string path)
  128. {
  129. if (File.Exists(path))
  130. {
  131. byte[] imageBuff = await ReadFile(path);
  132. Texture2D texture = Texture2DExt.CreateTexture2DFromWebP(imageBuff, lMipmaps: true, lLinear: false, lError: out WebP.Error lError);
  133. if (lError == WebP.Error.Success)
  134. {
  135. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  136. return sprite;
  137. }
  138. }
  139. return null;
  140. }
  141. public static async Task<Sprite> GetCardImageData(string fileName, string filePath)
  142. {
  143. string urlPath = $"https://raw.githubusercontent.com/TakaOtaku/Digimon-Card-App/main/src/assets/images/cards/{fileName}.webp";
  144. UnityWebRequest webReq_CardImage = UnityWebRequest.Get(urlPath);
  145. UnityWebRequestAsyncOperation operation = webReq_CardImage.SendWebRequest();
  146. while (!operation.isDone)
  147. {
  148. await Task.Yield();
  149. }
  150. Debug.Log($"WebRequest isDone: {fileName}");
  151. if (webReq_CardImage.result == UnityWebRequest.Result.ConnectionError)
  152. return null;
  153. else if (webReq_CardImage.result == UnityWebRequest.Result.ProtocolError)
  154. return null;
  155. else
  156. {
  157. File.WriteAllBytes(filePath, webReq_CardImage.downloadHandler.data);
  158. Texture2D texture = Texture2DExt.CreateTexture2DFromWebP(webReq_CardImage.downloadHandler.data, lMipmaps: true, lLinear: false, lError: out WebP.Error lError);
  159. if (lError == WebP.Error.Success)
  160. {
  161. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  162. return sprite;
  163. }
  164. return null;
  165. }
  166. }
  167. #endregion
  168. public static bool IsCardExists(CEntity_Base cEntity_Base)
  169. {
  170. string path = Path.Combine(GetStreamingAssetPath(false), $"Card/{cEntity_Base.CardSpriteName}.webp").Replace("\\", "/");
  171. if (cEntity_Base.CardSpriteName.Contains("token"))
  172. path = Path.Combine(GetStreamingAssetPath(false), $"Card/{cEntity_Base.CardSpriteName}.png").Replace("\\", "/");
  173. return File.Exists(path);
  174. }
  175. #region テキストファイルの取得
  176. public static string GetText(string fileName)
  177. {
  178. string path = Path.Combine(GetStreamingAssetPath(false), $"{fileName}.txt").Replace("\\", "/");
  179. if (File.Exists(path))
  180. {
  181. return File.ReadAllText(path);
  182. }
  183. return "";
  184. }
  185. #endregion
  186. static string GetStreamingAssetPath(bool isLauncher)
  187. {
  188. if (isLauncher)
  189. {
  190. string path = Application.streamingAssetsPath;
  191. path = GetOneUpperDirectoryPath(path);
  192. path = Path.Combine(path, $"Textures").Replace("\\", "/");
  193. return path;
  194. }
  195. else
  196. {
  197. string path = Application.streamingAssetsPath;
  198. path = GetOneUpperDirectoryPath(path);
  199. path = GetOneUpperDirectoryPath(path);
  200. path = Path.Combine(path, $"Textures").Replace("\\", "/");
  201. return path;
  202. }
  203. }
  204. static string GetOneUpperDirectoryPath(string path)
  205. {
  206. if (String.IsNullOrEmpty(path)) return "";
  207. path = path.Replace("\\", "/");
  208. if (!path.Contains("/")) return path;
  209. path = path.Substring(0, path.LastIndexOf("/") + 1);
  210. if (path.Length >= 1)
  211. {
  212. if (path[path.Length - 1] == '/')
  213. {
  214. path = path.Substring(0, path.LastIndexOf("/"));
  215. }
  216. }
  217. return path.Substring(0, path.LastIndexOf("/") + 1);
  218. }
  219. }