StreamingAssetsUtility.cs 3.7 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. Debug.Log(path);
  29. if (!File.Exists(path))
  30. {
  31. path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.png").Replace("\\", "/");
  32. }
  33. if (isCard)
  34. {
  35. path = Path.Combine(GetStreamingAssetPath(isLauncher), $"Card/{fileName}.png").Replace("\\", "/");
  36. if (!File.Exists(path))
  37. {
  38. path = Path.Combine(GetStreamingAssetPath(isLauncher), $"Card/{fileName}.jpg").Replace("\\", "/");
  39. }
  40. }
  41. if (File.Exists(path))
  42. {
  43. byte[] imageBuff = await ReadFile(path);
  44. Texture2D tex = BinaryToTexture(imageBuff);
  45. Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
  46. return sprite;
  47. }
  48. return null;
  49. }
  50. #endregion
  51. public static bool IsCardExists(CEntity_Base cEntity_Base)
  52. {
  53. string path = Path.Combine(GetStreamingAssetPath(false), $"Card/{cEntity_Base.CardSpriteName}.png").Replace("\\", "/");
  54. if (File.Exists(path))
  55. {
  56. return true;
  57. }
  58. path = Path.Combine(GetStreamingAssetPath(false), $"Card/{cEntity_Base.CardSpriteName}.jpg").Replace("\\", "/");
  59. if (File.Exists(path))
  60. {
  61. return true;
  62. }
  63. return false;
  64. }
  65. #region テキストファイルの取得
  66. public static string GetText(string fileName)
  67. {
  68. string path = Path.Combine(GetStreamingAssetPath(false), $"{fileName}.txt").Replace("\\", "/");
  69. if (File.Exists(path))
  70. {
  71. return File.ReadAllText(path);
  72. }
  73. return "";
  74. }
  75. #endregion
  76. static string GetStreamingAssetPath(bool isLauncher)
  77. {
  78. if (isLauncher)
  79. {
  80. string path = Application.streamingAssetsPath;
  81. path = GetOneUpperDirectoryPath(path);
  82. path = Path.Combine(path, $"Textures").Replace("\\", "/");
  83. return path;
  84. }
  85. else
  86. {
  87. string path = Application.streamingAssetsPath;
  88. path = GetOneUpperDirectoryPath(path);
  89. path = GetOneUpperDirectoryPath(path);
  90. path = Path.Combine(path, $"Textures").Replace("\\", "/");
  91. return path;
  92. }
  93. }
  94. static string GetOneUpperDirectoryPath(string path)
  95. {
  96. if (String.IsNullOrEmpty(path)) return "";
  97. path = path.Replace("\\", "/");
  98. if (!path.Contains("/")) return path;
  99. path = path.Substring(0, path.LastIndexOf("/") + 1);
  100. if (path.Length >= 1)
  101. {
  102. if (path[path.Length - 1] == '/')
  103. {
  104. path = path.Substring(0, path.LastIndexOf("/"));
  105. }
  106. }
  107. return path.Substring(0, path.LastIndexOf("/") + 1);
  108. }
  109. }