using XGame.Framework.Define;
using System.IO;
using UnityEngine;
namespace XGame.Framework.Utils
{
public static class FileUtils
{
//public static string PlatformName
//{
// get
// {
// switch (Application.platform)
// {
// case RuntimePlatform.OSXEditor:
// case RuntimePlatform.OSXPlayer:
// return "Mac";
// case RuntimePlatform.IPhonePlayer:
// return "iOS";
// case RuntimePlatform.Android:
// return "Android";
// //case RuntimePlatform.WindowsEditor:
// //case RuntimePlatform.WindowsPlayer:
// default:
// return "Windows";
// }
// }
//}
///
/// 获取游戏包里的文件路径
///
///
///
///
public static string GetFilePath(string fileName, bool includeUpdate = true)
{
//先取热更新的资源
//var path = Path.Combine(UpdatePath, fileName);
if (TryGetFilePath(fileName, includeUpdate, out var path))
{
return path;
}
//找不到再从内置资源里取
return $"{PathDefine.WorkPath}/{fileName}";
}
///
/// 获取游戏包里的文件URL
///
/// 文件名
///
///
public static string GetFileUrl(string fileName, bool includeUpdate = true)
{
//先取热更新的资源
//string path = Path.Combine(UpdatePath, fileName);
if (TryGetFilePath(fileName, includeUpdate, out var path))
{
return "file://" + path;
}
//找不到再从内置资源里取
if (Application.platform == RuntimePlatform.Android)
return $"jar:file://{PathDefine.WorkPath}/{fileName}";
return $"file://{PathDefine.WorkPath}/{fileName}";
}
private static bool TryGetFilePath(string fileName, bool includeUpdate, out string path)
{
if (includeUpdate)
{
//先取热更新的资源
path = $"{PathDefine.UpdatePath}/{fileName}";
if (System.IO.File.Exists(path))
{
return true;
}
}
else
{
path = string.Empty;
}
#if UNITY_EDITOR
//编辑器下多做一步平台判断
path = $"{PathDefine.WorkPath}/{fileName}";
if (System.IO.File.Exists(path))
{
return true;
}
var platform = PlatformType.Windows;
#if UNITY_ANDROID
platform = PlatformType.Android;
#elif UNITY_IOS
platform = PlatformType.iOS;
#elif UNITY_WEIXINMINIGAME
platform = PlatformType.Weixin;
#elif UNITY_WEBGL
platform = PlatformType.WebGL;
#endif
path = $"{PathDefine.WorkPath}/{platform}/{fileName}";
if (System.IO.File.Exists(path))
{
return true;
}
#endif
return false;
}
public static string GetAssetBundlePath(string bundleName)
{
return GetFilePath(Path.Combine("AssetBundles", bundleName));
}
}
}