264 lines
8.1 KiB
C#
264 lines
8.1 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.U2D;
|
||
using YooAsset;
|
||
using Object = UnityEngine.Object;
|
||
|
||
|
||
public class AssetBundleManager : MonoBehaviour
|
||
{
|
||
public static AssetBundleManager ins;
|
||
public EPlayMode PlayMode = EPlayMode.EditorSimulateMode;
|
||
public string CDN = "https://qq.tencent.com/";
|
||
private ResourcePackage package;
|
||
private Dictionary<AtlasType, SpriteAtlas> _atlasData;
|
||
|
||
private void Awake()
|
||
{
|
||
}
|
||
/// <summary>
|
||
/// 初始化ab包
|
||
/// </summary>
|
||
public void InitAsset(Action action)
|
||
{
|
||
ins = this;
|
||
return;
|
||
YooAssets.Initialize();
|
||
// 设置默认的资源包
|
||
var gamePackage = YooAssets.TryGetPackage("DefaultPackage");
|
||
YooAssets.SetDefaultPackage(gamePackage);
|
||
ins = this;
|
||
StartCoroutine(InitPackage(action));
|
||
}
|
||
private IEnumerator InitPackage(Action action)
|
||
{
|
||
var playMode = PlayMode;
|
||
var packageName = "DefaultPackage";
|
||
var buildPipeline = EDefaultBuildPipeline.BuiltinBuildPipeline;
|
||
|
||
// 创建资源包裹类
|
||
package = YooAssets.TryGetPackage(packageName);
|
||
if (package == null)
|
||
package = YooAssets.CreatePackage(packageName);
|
||
|
||
// 编辑器下的模拟模式
|
||
InitializationOperation initializationOperation = null;
|
||
if (playMode == EPlayMode.EditorSimulateMode)
|
||
{
|
||
var simulateBuildResult = EditorSimulateModeHelper.SimulateBuild(buildPipeline, packageName);
|
||
var createParameters = new EditorSimulateModeParameters();
|
||
createParameters.EditorFileSystemParameters = FileSystemParameters.CreateDefaultEditorFileSystemParameters(simulateBuildResult);
|
||
initializationOperation = package.InitializeAsync(createParameters);
|
||
}
|
||
|
||
// 单机运行模式
|
||
if (playMode == EPlayMode.OfflinePlayMode)
|
||
{
|
||
var createParameters = new OfflinePlayModeParameters();
|
||
createParameters.BuildinFileSystemParameters = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
|
||
initializationOperation = package.InitializeAsync(createParameters);
|
||
}
|
||
Debug.LogError(CDN);
|
||
#if UNITY_WEBGL && WEIXINMINIGAME
|
||
// WebGL运行模式
|
||
if (playMode == EPlayMode.WebPlayMode)
|
||
{
|
||
var createParameters = new WebPlayModeParameters();
|
||
IRemoteServices sever = new RemoveServer(CDN);
|
||
createParameters.WebFileSystemParameters = WechatFileSystemCreater.CreateWechatFileSystemParameters(sever);
|
||
initializationOperation = package.InitializeAsync(createParameters);
|
||
}
|
||
#endif
|
||
//WebGL运行模式
|
||
// if (playMode == EPlayMode.WebPlayMode)
|
||
// {
|
||
// var createParameters = new WebPlayModeParameters();
|
||
// createParameters.WebFileSystemParameters = FileSystemParameters.CreateDefaultWebFileSystemParameters(CDN);
|
||
// initializationOperation = package.InitializeAsync(createParameters);
|
||
// }
|
||
yield return initializationOperation;
|
||
var operationver =package.RequestPackageVersionAsync();
|
||
yield return operationver;
|
||
if (operationver.Status != EOperationStatus.Succeed)
|
||
yield break;
|
||
|
||
// 3. 传入的版本信息更新资源清单
|
||
var operationManifestAsync =package.UpdatePackageManifestAsync(operationver.PackageVersion);
|
||
yield return operationManifestAsync;
|
||
if (operationManifestAsync.Status != EOperationStatus.Succeed)
|
||
yield break;
|
||
var download=package.CreateResourceDownloader(3, 10);
|
||
download.BeginDownload();
|
||
LoadAtlas();
|
||
action.Invoke();
|
||
}
|
||
/// <summary>
|
||
/// 加载ab资源
|
||
/// </summary>
|
||
/// <param name="path"></param>
|
||
/// <returns></returns>
|
||
public void LoadAsset(string path,Action<GameObject> action)
|
||
{
|
||
// var obj = package.LoadAssetSync<GameObject>(path);
|
||
var obj = Resources.Load<GameObject>(path);
|
||
StartCoroutine(LodObj(obj, action));
|
||
}
|
||
IEnumerator LodObj(GameObject obj,Action<GameObject> action)
|
||
{
|
||
yield return null;
|
||
obj.SetActive(false);
|
||
var lodObj = Instantiate(obj, transform);
|
||
action(lodObj);
|
||
}
|
||
|
||
public void LoadAtlas()
|
||
{
|
||
_atlasData = new Dictionary<AtlasType, SpriteAtlas>();
|
||
LoadAsset(AtlasPath(AtlasType.ItemIcon), (obj) =>
|
||
{
|
||
_atlasData.Add(AtlasType.ItemIcon,obj);
|
||
});
|
||
LoadAsset(AtlasPath(AtlasType.NpcIcon), (obj) =>
|
||
{
|
||
_atlasData.Add(AtlasType.NpcIcon,obj);
|
||
});
|
||
LoadAsset(AtlasPath(AtlasType.ButtleIcon), (obj) =>
|
||
{
|
||
_atlasData.Add(AtlasType.ButtleIcon,obj);
|
||
});
|
||
LoadAsset(AtlasPath(AtlasType.DrawingIcon), (obj) =>
|
||
{
|
||
_atlasData.Add(AtlasType.DrawingIcon,obj);
|
||
});
|
||
LoadAsset(AtlasPath(AtlasType.AtlasBattleBack), (obj) =>
|
||
{
|
||
_atlasData.Add(AtlasType.AtlasBattleBack,obj);
|
||
});
|
||
}
|
||
public void LoadAsset(string path, Action<SpriteAtlas> action)
|
||
{
|
||
// var obj = package.LoadAssetSync<SpriteAtlas>(path);
|
||
var obj = Resources.Load<SpriteAtlas>(path);
|
||
// StartCoroutine(LodObj(obj.AssetObject as SpriteAtlas, action));
|
||
action(obj);
|
||
}
|
||
|
||
|
||
private string AtlasPath(AtlasType atlasType)
|
||
{
|
||
switch (atlasType)
|
||
{
|
||
case AtlasType.ItemIcon:
|
||
return "UI/AtlasIcon";
|
||
case AtlasType.NpcIcon:
|
||
return "UI/AtlasNpc";
|
||
case AtlasType.ButtleIcon:
|
||
return "UI/AtlasButtle";
|
||
case AtlasType.DrawingIcon:
|
||
return "UI/AtlasDrawing";
|
||
case AtlasType.AtlasBattleBack:
|
||
return "UI/AtlasBattleBack";
|
||
default:
|
||
throw new ArgumentOutOfRangeException(nameof(atlasType), atlasType, null);
|
||
}
|
||
}
|
||
|
||
private SpriteAtlas targetAtlas;
|
||
public Sprite Sprite(string spriteName,AtlasType type)
|
||
{
|
||
if (_atlasData.TryGetValue(type, out targetAtlas))
|
||
{
|
||
return targetAtlas.GetSprite(spriteName);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
public void LoadAsset(string path, Action<Sprite> action)
|
||
{
|
||
SubAssetsHandle handle = package.LoadSubAssetsAsync<Sprite>(path);
|
||
var sprite = handle.GetSubAssetObject<Sprite>("path");
|
||
action(sprite);
|
||
}
|
||
|
||
public void LoadAsset<T0>(string path, Action<T0> action)
|
||
{
|
||
|
||
}
|
||
|
||
public void LoadAsset(string path, Action<AudioClip> action)
|
||
{
|
||
var obj = package.LoadAssetSync<AudioClip>(path);
|
||
action(obj.AssetObject as AudioClip);
|
||
}
|
||
|
||
public void GetPoolItem(string path, Action<AudioClip> action)
|
||
{
|
||
|
||
}
|
||
|
||
class RemoveServer : IRemoteServices
|
||
{
|
||
//注意CDN地址与Yoo远端加载地址需一致,才会触发缓存
|
||
//https://wechat-miniprogram.github.io/minigame-unity-webgl-transform/Design/FileCache.html
|
||
|
||
string CDN;
|
||
public RemoveServer(string cdn)
|
||
{
|
||
CDN = cdn;
|
||
}
|
||
|
||
//如果不一致,需要修改缓存目录,_fileCacheRoot
|
||
|
||
//远端目录结构为:
|
||
//CDN:
|
||
//webgl:
|
||
// StreamingAssets
|
||
// xxwebgl.wasm.code.unityweb.wasm.br
|
||
|
||
// xxx.version
|
||
// xxx.hash
|
||
// xx/bundle
|
||
public string GetRemoteFallbackURL(string fileName)
|
||
{
|
||
return CDN + fileName;
|
||
}
|
||
|
||
public string GetRemoteMainURL(string fileName)
|
||
{
|
||
return CDN + fileName;
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 运行模式
|
||
/// </summary>
|
||
public enum EPlayMode
|
||
{
|
||
/// <summary>
|
||
/// 编辑器下的模拟模式
|
||
/// </summary>
|
||
EditorSimulateMode,
|
||
|
||
/// <summary>
|
||
/// 离线运行模式
|
||
/// </summary>
|
||
OfflinePlayMode,
|
||
|
||
/// <summary>
|
||
/// WebGL运行模式
|
||
/// </summary>
|
||
WebPlayMode,
|
||
}
|
||
|
||
public enum AtlasType
|
||
{
|
||
ItemIcon,
|
||
NpcIcon,
|
||
ButtleIcon,
|
||
DrawingIcon,
|
||
AtlasBattleBack
|
||
} |