StreamingAssetsUtility.cs 8.3 KB

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