Addreseable 组的设置 1.设置文件分组
[MenuItem("AddressableEditor/SetAllDirectorToAddress", priority = 2)]
public static void SetAllDirectorToAddress()
{
var arr = Selection.GetFiltered(typeof(DefaultAsset), SelectionMode.Assets);
string folder = AssetDatabase.GetAssetPath(arr[0]); ;
LoopSetAllDirectorToAddress(folder);
}
private static void LoopSetAllDirectorToAddress(string pFileDirectorRoot)
{
if (Directory.Exists(pFileDirectorRoot) && !pFileDirectorRoot.Contains("_Bundles/Lua"))
{
SetDirectorABNameNull(pFileDirectorRoot);
var dirctory = new DirectoryInfo(pFileDirectorRoot);
var direcs = dirctory.GetDirectories("*", SearchOption.TopDirectoryOnly);
if (direcs.Length > 0)
{
for (var i = 0; i < direcs.Length; i++)
{
if (direcs[i].FullName != pFileDirectorRoot)
{
LoopSetAllDirectorToAddress(direcs[i].FullName);
}
}
}
}
}
private static void SetDirectorABNameNull(string pFileDirectorRoot)
{
if (Directory.Exists(pFileDirectorRoot) && !pFileDirectorRoot.Contains("_Bundles/Lua"))
{
var dirctory = new DirectoryInfo(pFileDirectorRoot);
var files = dirctory.GetFiles("*", SearchOption.TopDirectoryOnly);
bool isAdd = false;
for (var i = 0; i < files.Length; i++)
{
var file = files[i];
if (file.Name.EndsWith(".meta"))
continue;
if (file.Name.EndsWith(".txt"))
continue;
string assetPath = file.FullName;
assetPath = FormatFilePath(assetPath);
var assetLength = UnityEngine.Application.dataPath.Length - 6;
assetPath = assetPath.Substring(assetLength, assetPath.Length - assetLength);
AutoGroup(dirctory.Name, assetPath);
isAdd = true;
}
if (isAdd)
AssetDatabase.Refresh();
}
}
public static void AutoGroup(string groupName, string assetPath)
{
var settings = AddressableAssetSettingsDefaultObject.Settings;
AddressableAssetGroup group = settings.FindGroup(groupName);
if (group == null)
{
group = CreatAssetGroup<System.Data.SchemaType>(settings, groupName);
}
var guid = AssetDatabase.AssetPathToGUID(assetPath);
var entry = settings.CreateOrMoveEntry(guid, group);
entry.address = assetPath;
entry.SetLabel(groupName,true,true);
}
private static AddressableAssetGroup CreatAssetGroup<SchemaType>(AddressableAssetSettings settings, string groupName)
{
return settings.CreateGroup(groupName, false, false, false,
new List<AddressableAssetGroupSchema> { settings.DefaultGroup.Schemas[0], settings.DefaultGroup.Schemas[1] }, typeof(SchemaType));
}
2.分组设置
3.总体设置: 4. 游戏启动更新逻辑
void checkUpdate(System.Action pFinish)
{
Debug.LogError(" checkUpdate >>>");
StartCoroutine(Initialize(() =>
{
StartCoroutine(checkUpdateSize((oSize, oList) =>
{
if (oList.Count > 0)
{
StartCoroutine(DoUpdate(oList, () =>
{
pFinish();
}));
}
else
{
pFinish();
}
}));
}));
}
IEnumerator Initialize(System.Action pOnFinish)
{
Debug.LogError(" Initialize >>>");
var init = Addressables.InitializeAsync();
yield return init;
Addressables.InternalIdTransformFunc = InternalIdTransformFunc;
pOnFinish.Invoke();
}
IEnumerator checkUpdateSize(System.Action<long, List<string>> pOnFinish)
{
Debug.LogError(" checkUpdateSize >>>");
long sizeLong = 0;
List<string> catalogs = new List<string>();
AsyncOperationHandle<List<string>> checkHandle = Addressables.CheckForCatalogUpdates(false);
yield return checkHandle;
if (checkHandle.Status == AsyncOperationStatus.Succeeded)
{
catalogs = checkHandle.Result;
}
IEnumerable<IResourceLocator> locators = Addressables.ResourceLocators;
List<object> keys = new List<object>();
foreach (var locator in locators)
{
foreach (var key in locator.Keys)
{
keys.Add(key);
}
}
Debug.Log("download start catalogs keys is :" + keys.Count);
Debug.Log("download start catalogs count is :" + catalogs.Count);
var handle = Addressables.GetDownloadSizeAsync(catalogs);
yield return handle;
long downloadSize = handle.Result;
sizeLong = handle.Result;
Debug.Log("download start catalogs sizeLong is :" + sizeLong);
Addressables.Release(handle);
Addressables.Release(checkHandle);
pOnFinish.Invoke(sizeLong, catalogs);
}
IEnumerator DoUpdate(List<string> catalogs, System.Action pOnFinish)
{
Debug.LogError(" DocatalogUpdate >>>");
var updateHandle = Addressables.UpdateCatalogs(catalogs, false);
yield return updateHandle;
Addressables.Release(updateHandle);
pOnFinish();
}
6.下载逻辑
public override void LoadAssetAsync()
{
Addressables.LoadAssetAsync<UnityEngine.Object>(path).Completed += OnAssetLoadedComplete;
}
public override void LoadPrefabAsync(Transform parent)
{
Addressables.InstantiateAsync(path, parent).Completed += OnGameObjectLoaderComplete;
}
public void OnGameObjectLoaderComplete(AsyncOperationHandle<GameObject> res)
{
if (res.Status == AsyncOperationStatus.Succeeded)
{
go_ = res.Result;
if (OnFinish != null)
{
OnFinish(go_, this);
}
else
{
UnLoadAsset();
ResLoadManager.UnLoadByAssetId(this.Id);
}
}
}
private void OnAssetLoadedComplete(AsyncOperationHandle<UnityEngine.Object> res)
{
if (res.Status == AsyncOperationStatus.Succeeded)
{
asset_ = res.Result;
if (OnFinish != null)
{
OnFinish(asset_, this);
}
else
{
UnLoadAsset();
ResLoadManager.UnLoadByAssetId(this.Id);
}
}
}
public override void UnLoadAsset()
{
if (go_ != null)
{
Addressables.ReleaseInstance(go_);
}
if (asset_ != null)
{
Addressables.Release<UnityEngine.Object>(asset_);
}
go_ = null;
asset_ = null;
OnFinish = null;
}
|