StreamingAssetsUtility.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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("Textures", isLauncher), $"Card/{fileName}.png").Replace("\\", "/"));
  34. }
  35. else
  36. {
  37. path = Path.Combine(GetStreamingAssetPath("Textures", 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("Textures", isLauncher), $"{fileName}.jpg").Replace("\\", "/");
  56. if(!File.Exists(path))
  57. path = Path.Combine(GetStreamingAssetPath("Textures", 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. Debug.Log($"File Exists Locally: {path}");
  83. byte[] imageBuff = await ReadFile(path);
  84. Debug.Log($"Grabbing image bytes: {imageBuff}");
  85. Texture2D texture = Texture2DExt.CreateTexture2DFromWebP(imageBuff, lMipmaps: true, lLinear: false, lError: out WebP.Error lError);
  86. Debug.Log($"Converting WebP to Texture2D: {texture}");
  87. if (lError == WebP.Error.Success)
  88. {
  89. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  90. return sprite;
  91. }
  92. else
  93. {
  94. Debug.Log(lError.ToString());
  95. }
  96. }
  97. return null;
  98. }
  99. public static async Task<Sprite> GetCardImageData(string fileName, string filePath)
  100. {
  101. string urlPath = $"https://raw.githubusercontent.com/TakaOtaku/Digimon-Card-App/main/src/assets/images/cards/{fileName}.webp";
  102. UnityWebRequest webReq_CardImage = UnityWebRequest.Get(urlPath);
  103. UnityWebRequestAsyncOperation operation = webReq_CardImage.SendWebRequest();
  104. while (!operation.isDone)
  105. {
  106. await Task.Yield();
  107. }
  108. Debug.Log($"WebRequest isDone: {fileName}");
  109. if (webReq_CardImage.result == UnityWebRequest.Result.ConnectionError)
  110. return null;
  111. else if (webReq_CardImage.result == UnityWebRequest.Result.ProtocolError)
  112. return null;
  113. else
  114. {
  115. Debug.Log($"WebRequest Successful: Checking local file - {File.Exists(filePath)}");
  116. if(!File.Exists(filePath))
  117. File.WriteAllBytes(filePath, webReq_CardImage.downloadHandler.data);
  118. Texture2D texture = Texture2DExt.CreateTexture2DFromWebP(webReq_CardImage.downloadHandler.data, lMipmaps: true, lLinear: false, lError: out WebP.Error lError);
  119. if (lError == WebP.Error.Success)
  120. {
  121. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  122. return sprite;
  123. }
  124. else
  125. {
  126. Debug.Log($"Failed to convert: {lError.ToString()}");
  127. }
  128. return null;
  129. }
  130. }
  131. #endregion
  132. public static bool IsCardExists(CEntity_Base cEntity_Base)
  133. {
  134. string path = Path.Combine(GetStreamingAssetPath("Textures", false), $"Card/{cEntity_Base.CardSpriteName}.webp").Replace("\\", "/");
  135. if (cEntity_Base.CardSpriteName.Contains("token"))
  136. path = Path.Combine(GetStreamingAssetPath("Textures", false), $"Card/{cEntity_Base.CardSpriteName}.png").Replace("\\", "/");
  137. return File.Exists(path);
  138. }
  139. #region テキストファイルの取得
  140. public static string GetText(string fileName)
  141. {
  142. string path = Path.Combine(GetStreamingAssetPath("", false), $"{fileName}.txt").Replace("\\", "/");
  143. if (File.Exists(path))
  144. {
  145. return File.ReadAllText(path);
  146. }
  147. return "";
  148. }
  149. #endregion
  150. public static string GetStreamingAssetPath(string subPath, bool isLauncher)
  151. {
  152. if (isLauncher)
  153. {
  154. string path = Application.streamingAssetsPath;
  155. path = GetOneUpperDirectoryPath(path);
  156. path = Path.Combine(path, $"Assets/{subPath}").Replace("\\", "/");
  157. return path;
  158. }
  159. else
  160. {
  161. string path = Application.streamingAssetsPath;
  162. path = GetOneUpperDirectoryPath(path);
  163. path = GetOneUpperDirectoryPath(path);
  164. path = Path.Combine(path, $"Assets/{subPath}").Replace("\\", "/");
  165. return path;
  166. }
  167. }
  168. static string GetOneUpperDirectoryPath(string path)
  169. {
  170. if (String.IsNullOrEmpty(path)) return "";
  171. path = path.Replace("\\", "/");
  172. if (!path.Contains("/")) return path;
  173. path = path.Substring(0, path.LastIndexOf("/") + 1);
  174. if (path.Length >= 1)
  175. {
  176. if (path[path.Length - 1] == '/')
  177. {
  178. path = path.Substring(0, path.LastIndexOf("/"));
  179. }
  180. }
  181. return path.Substring(0, path.LastIndexOf("/") + 1);
  182. }
  183. }