StreamingAssetsUtility.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. }
  53. public static async Task<Sprite> GetSpriteImage(string fileName, bool isLauncher = false)
  54. {
  55. string path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.jpg").Replace("\\", "/");
  56. if(!File.Exists(path))
  57. path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.png").Replace("\\", "/");
  58. if (File.Exists(path))
  59. {
  60. byte[] imageBuff = await ReadFile(path);
  61. Texture2D tex = BinaryToTexture(imageBuff);
  62. Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
  63. return sprite;
  64. }
  65. return null;
  66. }
  67. public static async Task<Sprite> GetTokenImageData(string path)
  68. {
  69. if (File.Exists(path))
  70. {
  71. byte[] imageBuff = await ReadFile(path);
  72. Texture2D tex = BinaryToTexture(imageBuff);
  73. Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
  74. return sprite;
  75. }
  76. return null;
  77. }
  78. public static async Task<Sprite> GetCardImageDataLocal(string path)
  79. {
  80. if (File.Exists(path))
  81. {
  82. byte[] imageBuff = await ReadFile(path);
  83. Texture2D texture = Texture2DExt.CreateTexture2DFromWebP(imageBuff, lMipmaps: true, lLinear: false, lError: out WebP.Error lError);
  84. if (lError == WebP.Error.Success)
  85. {
  86. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  87. return sprite;
  88. }
  89. else
  90. {
  91. Debug.Log(lError.ToString());
  92. }
  93. }
  94. return null;
  95. }
  96. public static async Task<Sprite> GetCardImageData(string fileName, string filePath)
  97. {
  98. string urlPath = $"https://raw.githubusercontent.com/TakaOtaku/Digimon-Card-App/main/src/assets/images/cards/{fileName}.webp";
  99. UnityWebRequest webReq_CardImage = UnityWebRequest.Get(urlPath);
  100. UnityWebRequestAsyncOperation operation = webReq_CardImage.SendWebRequest();
  101. while (!operation.isDone)
  102. {
  103. await Task.Yield();
  104. }
  105. Debug.Log($"WebRequest isDone: {fileName}");
  106. if (webReq_CardImage.result == UnityWebRequest.Result.ConnectionError)
  107. return null;
  108. else if (webReq_CardImage.result == UnityWebRequest.Result.ProtocolError)
  109. return null;
  110. else
  111. {
  112. if(!File.Exists(filePath))
  113. File.WriteAllBytes(filePath, webReq_CardImage.downloadHandler.data);
  114. Texture2D texture = Texture2DExt.CreateTexture2DFromWebP(webReq_CardImage.downloadHandler.data, lMipmaps: true, lLinear: false, lError: out WebP.Error lError);
  115. if (lError == WebP.Error.Success)
  116. {
  117. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  118. return sprite;
  119. }
  120. return null;
  121. }
  122. }
  123. #endregion
  124. public static bool IsCardExists(CEntity_Base cEntity_Base)
  125. {
  126. string path = Path.Combine(GetStreamingAssetPath(false), $"Card/{cEntity_Base.CardSpriteName}.webp").Replace("\\", "/");
  127. if (cEntity_Base.CardSpriteName.Contains("token"))
  128. path = Path.Combine(GetStreamingAssetPath(false), $"Card/{cEntity_Base.CardSpriteName}.png").Replace("\\", "/");
  129. return File.Exists(path);
  130. }
  131. #region テキストファイルの取得
  132. public static string GetText(string fileName)
  133. {
  134. string path = Path.Combine(GetStreamingAssetPath(false), $"{fileName}.txt").Replace("\\", "/");
  135. if (File.Exists(path))
  136. {
  137. return File.ReadAllText(path);
  138. }
  139. return "";
  140. }
  141. #endregion
  142. static string GetStreamingAssetPath(bool isLauncher)
  143. {
  144. if (isLauncher)
  145. {
  146. string path = Application.streamingAssetsPath;
  147. path = GetOneUpperDirectoryPath(path);
  148. path = Path.Combine(path, $"Textures").Replace("\\", "/");
  149. return path;
  150. }
  151. else
  152. {
  153. string path = Application.streamingAssetsPath;
  154. path = GetOneUpperDirectoryPath(path);
  155. path = GetOneUpperDirectoryPath(path);
  156. path = Path.Combine(path, $"Textures").Replace("\\", "/");
  157. return path;
  158. }
  159. }
  160. static string GetOneUpperDirectoryPath(string path)
  161. {
  162. if (String.IsNullOrEmpty(path)) return "";
  163. path = path.Replace("\\", "/");
  164. if (!path.Contains("/")) return path;
  165. path = path.Substring(0, path.LastIndexOf("/") + 1);
  166. if (path.Length >= 1)
  167. {
  168. if (path[path.Length - 1] == '/')
  169. {
  170. path = path.Substring(0, path.LastIndexOf("/"));
  171. }
  172. }
  173. return path.Substring(0, path.LastIndexOf("/") + 1);
  174. }
  175. }