场景存放规则
导出AssetBundle资源 文件夹路径信息 Assets文件夹 |------GResources | |-----Material | |-----Prefabs | |-----Scene | | └------场景名文件夹(例子:Main) | | └------------场景文件(例子:Main.unity) | |-----Prefabs
AssetBundle的加载
public class AssetBundleManager : MonoBehaviour
{
private AssetBundleManifest manifest;
private string mAssetBundlePath { get { return string.Format("{0}/{1}", Application.streamingAssetsPath, platform); } }
Dictionary<string, AssetBundle> All_Assetbundle = new Dictionary<string, AssetBundle>();
List<string> AllCurrentAssetBundle = new List<string>();
#if UNITY_STANDALONE_WIN
private static string platform = "Windows";
#elif UNITY_ANDROID
private static string platform = "Android";
#elif UNITY_IOS || UNITY_IPHONE
private static string platform = "IOS";
#else
private static string platform = "Windows";
#endif
private void GetManifest()
{
string assetbundlepath = $"{mAssetBundlePath}/{platform}";
AssetBundle assetBundle = AssetBundle.LoadFromFile(assetbundlepath);
if (assetBundle != null)
{
manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
}
else
{
}
}
#region 同步加载
public T Load<T>(string varPath) where T : Object
{
return LoadAssets<T>(varPath);
}
private T LoadAssets<T>(string varPath) where T : Object
{
string filename = Path.GetFileNameWithoutExtension(varPath);
string assetbundlename = GetAssetBundleName(varPath);
AssetBundle assetBundle = LoadAssetBundle(assetbundlename);
if (assetBundle == null)
{
return null;
}
else
{
T assets = assetBundle.LoadAsset<T>(filename);
return assets;
}
}
private AssetBundle LoadAssetBundle(string assetbundlename)
{
if (All_Assetbundle.ContainsKey(assetbundlename))
{
return All_Assetbundle[assetbundlename];
}
else
{
if (!AllCurrentAssetBundle.Contains(assetbundlename))
{
string assetbundlepath = $"{mAssetBundlePath}/{assetbundlename}";
AssetBundle assetBundle = AssetBundle.LoadFromFile(assetbundlepath);
All_Assetbundle.Add(assetbundlename, assetBundle);
LoadAssetBundleManifest(assetBundle, assetbundlename);
return assetBundle;
}
return null;
}
}
private void LoadAssetBundleManifest(AssetBundle assetBundle,string assetbundlename)
{
if (manifest == null)
{
GetManifest();
}
if (manifest != null)
{
string[] Dependencies = manifest.GetDirectDependencies(assetbundlename);
if (Dependencies != null)
{
for (int i = 0; i < Dependencies.Length; i++)
{
LoadAssetBundle(Dependencies[i]);
}
}
}
}
#endregion
#region 异步加载
public void LoadAsync<T>(string varPath, LoadCallBack<T> Callback) where T : Object
{
StartCoroutine(LoadAsyncAssets(varPath, Callback));
}
IEnumerator LoadAsyncAssets<T>(string varPath, LoadCallBack<T> Callback) where T : Object
{
yield return null;
string filename = Path.GetFileNameWithoutExtension(varPath);
string assetbundlename = GetAssetBundleName(varPath);
AssetBundle assetBundle = null;
yield return StartCoroutine(LoadAsyncAssetBundle(assetbundlename, (varassetbundle) =>
{
assetBundle = varassetbundle;
}));
if (assetBundle != null)
{
T assets = assetBundle.LoadAsset<T>(filename);
Callback.Invoke(varPath, assets);
}
else
{
Callback.Invoke(varPath, null);
}
}
IEnumerator LoadAsyncAssetBundle(string varAssetBundleName, LoadAssetBundleFinish assetBundleFinish)
{
varAssetBundleName = varAssetBundleName.Replace("\\", "/");
if (All_Assetbundle.ContainsKey(varAssetBundleName))
{
AssetBundle assetBundle = All_Assetbundle[varAssetBundleName];
assetBundleFinish?.Invoke(assetBundle);
}
else if (AllCurrentAssetBundle.Contains(varAssetBundleName))
{
while (AllCurrentAssetBundle.Contains(varAssetBundleName))
{
yield return null;
}
AssetBundle assetBundle = All_Assetbundle[varAssetBundleName];
assetBundleFinish?.Invoke(assetBundle);
}
else
{
AllCurrentAssetBundle.Add(varAssetBundleName);
string assetbundlepath = $"{mAssetBundlePath}/{varAssetBundleName}";
AssetBundle tempAssetBundle = null;
AssetBundleCreateRequest tempAssetbundleQuest = AssetBundle.LoadFromFileAsync(assetbundlepath);
while (!tempAssetbundleQuest.isDone)
{
yield return null;
}
tempAssetBundle = tempAssetbundleQuest.assetBundle;
All_Assetbundle.Add(varAssetBundleName, tempAssetBundle);
yield return StartCoroutine(LoadAsyncAssetBundleManifest(tempAssetBundle, varAssetBundleName));
assetBundleFinish?.Invoke(tempAssetBundle);
AllCurrentAssetBundle.Remove(varAssetBundleName);
}
}
IEnumerator LoadAsyncAssetBundleManifest(AssetBundle assetBundle, string filename)
{
if (manifest == null)
{
GetManifest();
}
if (manifest != null)
{
string[] Dependencies = manifest.GetDirectDependencies(filename);
if (Dependencies != null)
{
for (int i = 0; i < Dependencies.Length; i++)
{
yield return StartCoroutine(LoadAsyncAssetBundle(Dependencies[i], null));
}
}
}
}
#endregion
#region 加载场景
public void LoadScene(string varSceneName, LoadSceneCallBack varloadFinish, LoadSceneCallBack varLoadFail)
{
string assetsbundlename = $"gresources/scene/{varSceneName.ToLower()}.{AssetBundleExtension.Variant}";
StartCoroutine(LoadAsyncScene(assetsbundlename, varSceneName, varloadFinish, varLoadFail));
}
IEnumerator LoadAsyncScene(string assetsbundlename, string varScene, LoadSceneCallBack varloadFinish, LoadSceneCallBack varLoadFail)
{
AssetBundle.UnloadAllAssetBundles(false);
yield return null;
AssetBundle assetBundle = null;
yield return StartCoroutine(LoadAsyncAssetBundle(assetsbundlename, (varAssetBundle) => {
assetBundle = varAssetBundle;
}));
if (assetBundle != null)
{
AsyncOperation operation = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(varScene, UnityEngine.SceneManagement.LoadSceneMode.Single);
operation.allowSceneActivation = true;
while (!operation.isDone)
{
yield return null;
}
varloadFinish?.Invoke();
}
else
{
varLoadFail?.Invoke();
}
yield return null;
}
#endregion
private string GetAssetBundleName(string varPath)
{
string tempPath = Path.GetDirectoryName(varPath).ToLower();
tempPath = tempPath.Replace("\\", "/");
string assbundlename = "";
int index = tempPath.IndexOf("gresources/");
if (index >= 0)
{
assbundlename= tempPath.Remove(0, index);
return $"{assbundlename}.{AssetBundleExtension.Variant}";
}
else
{
return $"{varPath}.{AssetBundleExtension.Variant}";
}
}
}
AssetBundle拓展文件
public class AssetBundleExtension
{
public const string Variant = "bundle";
}
|