StreamingAssetsUtility.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using UnityEngine;
  2. using System.IO;
  3. using System;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. public class StreamingAssetsUtility
  7. {
  8. public static async Task<byte[]> ReadFile(string path)
  9. {
  10. using (FileStream fileStream = new FileStream(
  11. path, FileMode.Open, FileAccess.Read))
  12. {
  13. var resultBytes = new byte[fileStream.Length];
  14. await fileStream.ReadAsync(resultBytes, 0, (int)fileStream.Length);
  15. return resultBytes;
  16. }
  17. }
  18. #region 画像の取得
  19. public static Texture2D BinaryToTexture(byte[] bytes)
  20. {
  21. Texture2D texture = new Texture2D(1, 1);
  22. texture.LoadImage(bytes);
  23. return texture;
  24. }
  25. public static async Task<Sprite> GetSprite(string fileName, bool isCard = false, bool isLauncher = false)
  26. {
  27. string path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.jpg").Replace("\\", "/");
  28. if (!File.Exists(path))
  29. {
  30. path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.png").Replace("\\", "/");
  31. }
  32. if (isCard)
  33. {
  34. path = Path.Combine(GetStreamingAssetPath(isLauncher), $"Card/{fileName}.png").Replace("\\", "/");
  35. if (!File.Exists(path))
  36. {
  37. path = Path.Combine(GetStreamingAssetPath(isLauncher), $"Card/{fileName}.jpg").Replace("\\", "/");
  38. }
  39. }
  40. if (File.Exists(path))
  41. {
  42. byte[] imageBuff = await ReadFile(path);
  43. Texture2D tex = BinaryToTexture(imageBuff);
  44. Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
  45. return sprite;
  46. }
  47. return null;
  48. }
  49. #endregion
  50. public static bool IsCardExists(CEntity_Base cEntity_Base)
  51. {
  52. string path = Path.Combine(GetStreamingAssetPath(false), $"Card/{cEntity_Base.CardSpriteName}.png").Replace("\\", "/");
  53. if (File.Exists(path))
  54. {
  55. return true;
  56. }
  57. path = Path.Combine(GetStreamingAssetPath(false), $"Card/{cEntity_Base.CardSpriteName}.jpg").Replace("\\", "/");
  58. if (File.Exists(path))
  59. {
  60. return true;
  61. }
  62. return false;
  63. }
  64. #region テキストファイルの取得
  65. public static string GetText(string fileName)
  66. {
  67. string path = Path.Combine(GetStreamingAssetPath(false), $"{fileName}.txt").Replace("\\", "/");
  68. if (File.Exists(path))
  69. {
  70. return File.ReadAllText(path);
  71. }
  72. return "";
  73. }
  74. #endregion
  75. static string GetStreamingAssetPath(bool isLauncher)
  76. {
  77. if (isLauncher)
  78. {
  79. string path = Application.streamingAssetsPath;
  80. path = GetOneUpperDirectoryPath(path);
  81. path = Path.Combine(path, $"Textures").Replace("\\", "/");
  82. return path;
  83. }
  84. else
  85. {
  86. string path = Application.streamingAssetsPath;
  87. path = GetOneUpperDirectoryPath(path);
  88. path = GetOneUpperDirectoryPath(path);
  89. path = Path.Combine(path, $"Textures").Replace("\\", "/");
  90. return path;
  91. }
  92. }
  93. static string GetOneUpperDirectoryPath(string path)
  94. {
  95. if (String.IsNullOrEmpty(path)) return "";
  96. path = path.Replace("\\", "/");
  97. if (!path.Contains("/")) return path;
  98. path = path.Substring(0, path.LastIndexOf("/") + 1);
  99. if (path.Length >= 1)
  100. {
  101. if (path[path.Length - 1] == '/')
  102. {
  103. path = path.Substring(0, path.LastIndexOf("/"));
  104. }
  105. }
  106. return path.Substring(0, path.LastIndexOf("/") + 1);
  107. }
  108. }