AppRuntimesStreamingAssets.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. public static class AppRuntimesStreamingAssets
  5. {
  6. public static string GetPath(string relativePath)
  7. {
  8. if (string.IsNullOrWhiteSpace(relativePath))
  9. throw new ArgumentException("A package-relative file path is required.", nameof(relativePath));
  10. relativePath = relativePath.Replace('\\', '/');
  11. if (relativePath.StartsWith("/", StringComparison.Ordinal) || relativePath.Contains(":")
  12. || Array.Exists(relativePath.Split('/'), part => part == ".." || part == "." || part.Length == 0))
  13. throw new ArgumentException("Use a relative file path without parent directory segments.", nameof(relativePath));
  14. #if UNITY_EDITOR
  15. var package = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(AppSettings).Assembly);
  16. if (package == null)
  17. throw new InvalidOperationException("AppRuntimes must be installed through Package Manager.");
  18. return Path.Combine(package.resolvedPath, "StreamingAssets~", relativePath).Replace('\\', '/');
  19. #else
  20. return Application.streamingAssetsPath.TrimEnd('/') + "/" + relativePath;
  21. #endif
  22. }
  23. }