using UnityEngine;
|
using System.Collections;
|
using System.Collections.Generic;
|
using Cysharp.Threading.Tasks;
|
using System;
|
using UnityEngine.U2D;
|
using LitJson;
|
using System.IO;
|
using UnityEngine.Networking;
|
using UnityEngine.Video;
|
|
|
#if UNITY_EDITOR
|
using UnityEditor;
|
#endif
|
|
using LaunchCommon;
|
|
public class ResManager : Singleton<ResManager>
|
{
|
|
|
//不下载时本地安卓测试路径
|
// public string assetBundlesPath = Application.dataPath + "/../AssetBundles/Android/";
|
// public static string bytesFolderName = "logicbytes/";
|
public string StreamingAssetPath
|
{
|
get
|
{
|
return ResourcesPath.Instance.StreamingAssetPath;
|
}
|
}
|
public string ExternalStorePath
|
{
|
get
|
{
|
return ResourcesPath.Instance.ExternalStorePath;
|
}
|
}
|
|
//用于editor 下的资源路径
|
public string ResourcesOutPath
|
{
|
get
|
{
|
return ResourcesPath.ResourcesOutPath;
|
}
|
}
|
|
public string CONFIG_FODLER
|
{
|
get
|
{
|
return ResourcesPath.CONFIG_FODLER;
|
}
|
}
|
|
public string ResourcesOutAssetPath
|
{
|
get
|
{
|
return ResourcesPath.ResourcesOutAssetPath;
|
}
|
}
|
|
private static readonly Dictionary<Type, string> fileExtensionDict = new Dictionary<Type, string>()
|
{
|
{typeof(GameObject), "prefab"},
|
{typeof(Sprite), "png"},
|
{typeof(Texture2D), "png"},
|
{typeof(Shader), "shader"},
|
{typeof(TextAsset), "txt"},
|
{typeof(AudioClip), "wav"},
|
{typeof(Font), "ttf"},
|
{typeof(Material), "mat"},
|
{typeof(VideoClip), "mp4"},
|
{typeof(SpriteAtlas), "spriteatlasv2"},
|
};
|
|
|
public void Init()
|
{
|
|
}
|
|
public void Release()
|
{
|
|
}
|
|
private string GetExtension(Type type)
|
{
|
if (fileExtensionDict.TryGetValue(type, out string extension))
|
return "." + extension;
|
else
|
{
|
Debug.LogErrorFormat("GetExtension() => 不支持的资源类型: {0}.", type.Name);
|
return "";
|
}
|
}
|
|
#if UNITY_EDITOR
|
public string GetFullDirectory(string directory)
|
{
|
string fullDirectory = Path.Combine(ResourcesOutPath, directory);
|
return fullDirectory;
|
}
|
|
public string FindFilePath(string directory, string fileName)
|
{
|
string[] files = Directory.GetFiles(GetFullDirectory(directory), fileName, SearchOption.AllDirectories);
|
if (files.Length > 0)
|
{
|
return files[0];
|
}
|
return string.Empty;
|
}
|
|
public string GetRelativePath(string absolutePath)
|
{
|
string relativePath = absolutePath.Replace(ResourcesOutPath, "Assets/ResourcesOut/");
|
return relativePath;
|
}
|
#endif
|
|
public T LoadAsset<T> (string directory, string name) where T : UnityEngine.Object
|
{
|
T asset = null;
|
|
// 特殊处理 因为有一层图集的关系 directory要传入的应该是atlas的名字
|
if (typeof(T) == typeof(Sprite))
|
{
|
return LoadSprite(directory, name) as T;
|
}
|
|
return LoadAssetInternal<T>(directory, name);
|
}
|
|
private T LoadAssetInternal<T>(string directory, string name) where T : UnityEngine.Object
|
{
|
T asset = null;
|
|
var path = string.Concat($"Assets/ResourcesOut/{directory}/{name}", GetExtension(typeof(T))).Replace("//", "/");
|
|
if (AssetSource.uiFromEditor)
|
{
|
#if UNITY_EDITOR
|
asset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
|
#endif
|
}
|
else
|
{
|
var assetInfo = new AssetInfo(directory.ToLower(), name.ToLower());
|
asset = AssetBundleUtility.Instance.Sync_LoadAsset(assetInfo, typeof(T)) as T;
|
}
|
|
if (asset == null)
|
{
|
Debug.LogErrorFormat("LoadAsset() => 加载不到资源: {0}.", name);
|
}
|
|
return asset;
|
}
|
|
private Sprite LoadSprite(string atlasName, string spriteName)
|
{
|
#if !UNITY_EDITOR
|
SpriteAtlas atlas = LoadAsset<SpriteAtlas>("Sprite", atlasName);
|
return atlas.GetSprite(spriteName);
|
#else
|
// 编辑器下可以直接加载没啥问题
|
return LoadAssetInternal<Sprite>("Sprite/" + atlasName, spriteName);
|
#endif
|
}
|
|
public void LoadAssetAsync<T>(string directory, string name, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object
|
{
|
// 特殊处理 因为有一层图集的关系 directory要传入的应该是atlas的名字
|
if (typeof(T) == typeof(Sprite))
|
{
|
LoadSpriteAsync<T>(directory, name, callBack);
|
return;
|
}
|
|
LoadAssetAsyncInternal<T>(directory, name, callBack);
|
}
|
|
private void LoadSpriteAsync<T>(string atlasName, string spriteName, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object
|
{
|
#if !UNITY_EDITOR
|
LoadAssetAsync<SpriteAtlas>("Sprite", atlasName, (isLoaded, atlas) => {
|
if (isLoaded)
|
{
|
callBack?.Invoke(isLoaded, atlas.GetSprite(spriteName));
|
}
|
else
|
{
|
callBack?.Invoke(false, null);
|
}
|
});
|
#else
|
// 编辑器下可以直接加载没啥问题
|
LoadAssetAsyncInternal<T>("Sprite/" + atlasName, spriteName, callBack);
|
#endif
|
}
|
|
private void LoadAssetAsyncInternal<T>(string directory, string name, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object
|
{
|
var path = string.Concat($"Assets/ResourcesOut/{directory}/{name}", GetExtension(typeof(T))).Replace("//", "/");
|
|
if (AssetSource.uiFromEditor)
|
{
|
#if UNITY_EDITOR
|
var asset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
|
callBack?.Invoke(asset != null, asset);
|
#endif
|
}
|
else
|
{
|
var assetInfo = new AssetInfo(directory.ToLower(), name.ToLower());
|
AssetBundleUtility.Instance.Co_LoadAsset(assetInfo, callBack);
|
}
|
}
|
|
public void UnloadAsset(string assetBundleName, string assetName)
|
{
|
if (AssetSource.uiFromEditor)
|
return;
|
|
AssetBundleUtility.Instance.UnloadAsset(assetBundleName, assetName);
|
}
|
|
public void UnloadAssetBundle(string assetBundleName, bool unloadAllLoadedObjects, bool includeDependenice)
|
{
|
if (AssetSource.uiFromEditor)
|
return;
|
AssetBundleUtility.Instance.UnloadAssetBundle(assetBundleName, unloadAllLoadedObjects, includeDependenice);
|
}
|
|
public string GetAssetFilePath(string _assetKey)
|
{
|
var path = Path.Combine(ExternalStorePath, _assetKey);
|
if (!File.Exists(path))
|
{
|
path = Path.Combine(StreamingAssetPath, _assetKey);
|
}
|
|
return path;
|
}
|
|
|
}
|