This commit is contained in:
CortexCore
2024-11-14 17:17:46 +08:00
parent 416e3322db
commit b65b45c062
368 changed files with 1785 additions and 400 deletions

View File

@@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using YooAsset;
public static class AssetOperationHandleExtension
{
/// <summary>
/// 等待异步执行完毕
/// </summary>
public static AssetHandle WaitForAsyncOperationComplete(this AssetHandle thisHandle)
{
thisHandle.WaitForAsyncComplete();
return thisHandle;
}
}

View File

@@ -0,0 +1,73 @@
using UnityEngine;
using YooAsset;
#if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(GameObjectAssetReference), true)]
public class GameObjectAssetReferenceInspector : UnityEditor.Editor
{
private bool _init = false;
private GameObject _cacheObject;
public override void OnInspectorGUI()
{
GameObjectAssetReference mono = (GameObjectAssetReference)target;
if (_init == false)
{
_init = true;
if (string.IsNullOrEmpty(mono.AssetGUID) == false)
{
string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(mono.AssetGUID);
if (string.IsNullOrEmpty(assetPath) == false)
{
_cacheObject = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
}
}
}
GameObject go = (GameObject)UnityEditor.EditorGUILayout.ObjectField(_cacheObject, typeof(GameObject), false);
if (go != _cacheObject)
{
_cacheObject = go;
string assetPath = UnityEditor.AssetDatabase.GetAssetPath(go);
mono.AssetGUID = UnityEditor.AssetDatabase.AssetPathToGUID(assetPath);
UnityEditor.EditorUtility.SetDirty(target);
}
UnityEditor.EditorGUILayout.LabelField("Asset GUID", mono.AssetGUID);
}
}
#endif
public class GameObjectAssetReference : MonoBehaviour
{
[HideInInspector]
public string AssetGUID = "";
private AssetHandle _handle;
public void Start()
{
var package = YooAssets.GetPackage("DefaultPackage");
var assetInfo = package.GetAssetInfoByGUID(AssetGUID);
_handle = package.LoadAssetAsync(assetInfo);
_handle.Completed += Handle_Completed;
}
public void OnDestroy()
{
if (_handle != null)
{
_handle.Release();
_handle = null;
}
}
private void Handle_Completed(AssetHandle handle)
{
if (handle.Status == EOperationStatus.Succeed)
{
handle.InstantiateSync(this.transform);
}
}
}

View File

@@ -0,0 +1,117 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using YooAsset;
public class LoadAssetsByTagOperation<TObject> : GameAsyncOperation where TObject : UnityEngine.Object
{
private enum ESteps
{
None,
LoadAssets,
CheckResult,
Done,
}
private readonly string _tag;
private ESteps _steps = ESteps.None;
private List<AssetHandle> _handles;
/// <summary>
/// 资源对象集合
/// </summary>
public List<TObject> AssetObjects { private set; get; }
public LoadAssetsByTagOperation(string tag)
{
_tag = tag;
}
protected override void OnStart()
{
_steps = ESteps.LoadAssets;
}
protected override void OnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.LoadAssets)
{
AssetInfo[] assetInfos = YooAssets.GetAssetInfos(_tag);
_handles = new List<AssetHandle>(assetInfos.Length);
foreach (var assetInfo in assetInfos)
{
var handle = YooAssets.LoadAssetAsync(assetInfo);
_handles.Add(handle);
}
_steps = ESteps.CheckResult;
}
if (_steps == ESteps.CheckResult)
{
int index = 0;
foreach (var handle in _handles)
{
if (handle.IsDone == false)
{
Progress = (float)index / _handles.Count;
return;
}
index++;
}
AssetObjects = new List<TObject>(_handles.Count);
foreach (var handle in _handles)
{
if (handle.Status == EOperationStatus.Succeed)
{
var assetObject = handle.AssetObject as TObject;
if (assetObject != null)
{
AssetObjects.Add(assetObject);
}
else
{
string error = $"资源类型转换失败:{handle.AssetObject.name}";
Debug.LogError($"{error}");
AssetObjects.Clear();
SetFinish(false, error);
return;
}
}
else
{
Debug.LogError($"{handle.LastError}");
AssetObjects.Clear();
SetFinish(false, handle.LastError);
return;
}
}
SetFinish(true);
}
}
protected override void OnAbort()
{
}
private void SetFinish(bool succeed, string error = "")
{
Error = error;
Status = succeed ? EOperationStatus.Succeed : EOperationStatus.Failed;
_steps = ESteps.Done;
}
/// <summary>
/// 释放资源句柄
/// </summary>
public void ReleaseHandle()
{
foreach (var handle in _handles)
{
handle.Release();
}
_handles.Clear();
}
}