StreamingAssetsUtility.cs 7.1 KB

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