AkiraPixelWind/Assets/Scripts/Main/CustomsComponent/ResourcesComponent.cs

369 lines
13 KiB
C#
Raw Permalink Normal View History

2022-12-29 18:20:40 +08:00
using Axibug;
using Axibug.Event;
using Axibug.Resources;
using Axibug.Runtime;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using TMPro;
using UnityEditor;
using UnityEngine;
namespace Game
{
public class ResourcesComponent : GameComponent
{
public delegate void CallBackFunction(Object obj, CallbackType err, object userData = null);
private Dictionary<string, CallBackFunction> _dicAssetToFuns;
private List<Sprite> _skillIcons = new List<Sprite>();
private string _rootPath = "Assets/GameAssets";
protected override void Awake()
{
base.Awake();
}
private void Start()
{
_dicAssetToFuns = new Dictionary<string, CallBackFunction>();
PrefabManager.LoadPrefabSuccess += OnLoadPrefabSuccess;
PrefabManager.LoadPrefabFailure += OnLoadPrefabFailure;
//TODO <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ʼ<EFBFBD><CABC>
}
private void OnLoadPrefabSuccess(object sender, LogicEventArgs e)
{
LoadPrefabSuccessEventArgs prefab = (LoadPrefabSuccessEventArgs)e;
if (prefab == null)
throw new GameException("LoadPrefabSuccessEventArgs = null");
if (!_dicAssetToFuns.TryGetValue(prefab.AssetName, out CallBackFunction fun))
return;
fun(prefab.obj, CallbackType.Success, prefab.UserData);
}
private void OnLoadPrefabFailure(object sender, LogicEventArgs e)
{
LoadPrefabFailureEventArgs prefab = (LoadPrefabFailureEventArgs)e;
if (prefab == null)
throw new GameException("LoadPrefabFailureEventArgs = null");
if (!_dicAssetToFuns.TryGetValue(prefab.AssetName, out CallBackFunction fun))
return;
Log.Warning($"abName = {prefab.Name}, assetName = {prefab.AssetName}");
fun(null, CallbackType.NullAsset);
}
private string GetBundleName(string assetsName, bool isFolder = false)
{
//Assets/GameAssets/Character/Player/GS_M/Prefab/GS_M.prefab
//Assets/GameAssets/Prefabs/UI/Loading/Loading.prefab
if (string.IsNullOrEmpty(assetsName))
throw new GameException("GetBundleName---assetsName is invalid");
//Character/Player/GS_M/Prefab/GS_M.prefab
//Prefabs/UI/Loading/Loading.prefab
string tmp = assetsName.Remove(0, _rootPath.Length + 1);
if (isFolder)
return tmp.ToLower();
//Character/Player/GS_M/Prefab
int idx = tmp.LastIndexOf('/');
string bundleName = tmp.Substring(0, idx);
//if(bundleName.Contains("Character") || bundleName.Contains("Maps")
// || bundleName.Contains("MiniMapTexture") || bundleName.Contains("TMPColor"))
if (bundleName.Contains("Character") || bundleName.Contains("TMPColor"))
{
idx = bundleName.LastIndexOf('/');
//Character/Player/GS_M
bundleName = bundleName.Substring(0, idx);
}
//prefabs/ui/loading
return bundleName.ToLower();
}
public void LoadAssetAsync(string assetName, CallBackFunction fun = null, object userData = null)
{
if (fun != null && !_dicAssetToFuns.ContainsKey(assetName))
_dicAssetToFuns.Add(assetName, fun);
string abName = GetBundleName(assetName);
if (AppEntry.Base.EditorResourceMode)
LoadAssetByEditorAsync(abName, assetName, fun, userData);
else
LoadPrefabByABAsync(abName, assetName, null, userData);
}
public void LoadAssetAsync(string abName, string assetName, CallBackFunction fun = null, object userData = null)
{
if (fun != null && !_dicAssetToFuns.ContainsKey(assetName))
_dicAssetToFuns.Add(assetName, fun);
if (AppEntry.Base.EditorResourceMode)
LoadAssetByEditorAsync(abName, assetName, fun, userData);
else
LoadPrefabByABAsync(abName, assetName, null, userData);
}
// public Sprite LoadSprite(string atlas, string spriteName, bool isAtlas = true, string suffix = "png")
// {
// if (AppEntry.Base.EditorResourceMode)
// {
//#if UNITY_EDITOR
// string assetName = Utility.Path.GetImageUIPath(atlas, spriteName, suffix);
// return AssetDatabase.LoadAssetAtPath<Sprite>(assetName);
//#endif
// return null;
// }
// else
// {
// string atlasFullPath = $"Assets/GameAssets/Image/UI/{atlas}";
// if (atlas.Contains("Icon/"))
// atlas = atlas.Split('/')[1];
// string abName = GetBundleName($"{atlasFullPath}/{atlas}");
// if (isAtlas)
// return LoadAtlasSprite(abName, atlas, spriteName);
// return AssetManager.LoadAsset<Sprite>(abName, spriteName);
// }
// }
//public Sprite LoadAtlasSprite(string abName, string atlas, string spriteName)
//{
// string assetName = Utility.Path.GetImageUIPath(atlas, spriteName);
// if (AppEntry.Base.EditorResourceMode)
// return LoadSpriteByEditor(assetName); //LoadAssetByEditor ObjǿתSprite<74><65>Ч
// else
// return LoadPrefabByAB<Sprite>(abName, assetName, null);
//}
private Sprite LoadSpriteByEditor(string assetName)
{
#if UNITY_EDITOR
Sprite asset = AssetDatabase.LoadAssetAtPath<Sprite>(assetName);
return asset;
#endif
return null;
}
/*
public TMP_ColorGradient LoadColorGradient(string ColorName)
{
if (AppEntry.Base.EditorResourceMode)
{
#if UNITY_EDITOR
string assetName = Utility.Path.GetColorGradientPath(ColorName);
return AssetDatabase.LoadAssetAtPath<TMP_ColorGradient>(assetName);
#endif
return null;
}
else
{
string atlasFullPath = $"Assets/GameAssets/Font/TMPColor/{ColorName}";
string abName = GetBundleName($"{atlasFullPath}");
return AssetManager.LoadAsset<TMP_ColorGradient>(abName, ColorName);
}
}*/
public T[] LoadAssets<T>(string folder) where T : Object
{
string path = GetBundleName(folder, true);
#if UNITY_EDITOR
if(AppEntry.Base.EditorResourceMode)
{
FileInfo[] files = new DirectoryInfo(folder).GetFiles();
List<T> objs = new List<T>();
foreach (FileInfo file in files)
{
if (file.FullName.EndsWith(".meta"))
continue;
int idx = file.FullName.IndexOf("Assets");
path = file.FullName.Substring(idx);
T obj = AssetDatabase.LoadAssetAtPath<T>(path);
objs.Add(obj);
//var obj = LoadAssetByEditor(path);
//objs.Add((T)obj);
}
return objs.ToArray();
}
#endif
return AssetManager.LoadAssets<T>(path);
}
private void LoadAssetByEditorAsync(string abName, string assetName, CallBackFunction fun = null, object userData = null)
{
#if UNITY_EDITOR
Object asset = AssetDatabase.LoadAssetAtPath<Object>(assetName);
if (asset != null)
{
LoadPrefabSuccessEventArgs success = LoadPrefabSuccessEventArgs.Create(abName, assetName, 0, asset, userData);
OnLoadPrefabSuccess(null, success);
ReferencePool.Release(success);
}
else
{
LoadPrefabFailureEventArgs failure = LoadPrefabFailureEventArgs.Create(abName, assetName, 0);
OnLoadPrefabFailure(null, failure);
ReferencePool.Release(failure);
}
#endif
}
public void Clone(string assetName, CallBackFunction fun = null, Transform parent = null, object userData = null, string abSuffix = null)
{
if (fun != null)
{
if (!_dicAssetToFuns.ContainsKey(assetName))
_dicAssetToFuns.Add(assetName, fun);
else
_dicAssetToFuns[assetName] = fun;
}
string abName = GetBundleName(assetName);
if (AppEntry.Base.EditorResourceMode)
LoadPrefabByEditor(abName, assetName, parent, userData);
else
{
LoadPrefabByABAsync(abName, assetName, parent, userData);
}
}
// ͬ<><CDAC>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>
public GameObject CloneBySync(string assetName, Transform parent = null, string abSuffix = null)
{
string abName = GetBundleName(assetName);
Object asset = null;
if(AppEntry.Base.EditorResourceMode)
{
#if UNITY_EDITOR
asset = AssetDatabase.LoadAssetAtPath<GameObject>(assetName);
#endif
}
else
{
asset = LoadPrefabByAB<Object>(abName, assetName, parent);
}
if (asset == null)
{
Debug.LogError($"asset<65><74><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>path={assetName}");
return null;
}
GameObject go = Instantiate(asset, parent) as GameObject;
if (go == null)
{
Debug.LogError("LoadPrefabByEditor2. go == null. asset:" + asset);
int id = assetName.GetHashCode();
LoadPrefabFailureEventArgs failure = LoadPrefabFailureEventArgs.Create(abName, assetName, id);
OnLoadPrefabFailure(null, failure);
ReferencePool.Release(failure);
}
return go;
}
private void LoadPrefabByEditor(string abName, string assetName, Transform parent = null, object userData = null)
{
#if UNITY_EDITOR
Object asset = AssetDatabase.LoadAssetAtPath<Object>(assetName);
if(asset == null)
{
Log.Error($"asset<65><74><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>path={assetName}");
return;
}
GameObject go = Instantiate(asset, parent) as GameObject;
int id = assetName.GetHashCode();
if (go != null)
{
LoadPrefabSuccessEventArgs success = LoadPrefabSuccessEventArgs.Create(abName, assetName, id, go, userData);
OnLoadPrefabSuccess(null, success);
ReferencePool.Release(success);
}
else
{
LoadPrefabFailureEventArgs failure = LoadPrefabFailureEventArgs.Create(abName, assetName, id);
OnLoadPrefabFailure(null, failure);
ReferencePool.Release(failure);
}
#endif
}
private void LoadPrefabByABAsync(string abName, string assetName, Transform parent = null, object userData = null)
{
int id = assetName.GetHashCode();
PrefabManager.LoadPrefabAsync(abName, assetName, id, parent, userData);
}
private T LoadPrefabByAB<T>(string abName, string assetName, Transform parent = null) where T : Object
{
int id = assetName.GetHashCode();
return PrefabManager.LoadPrefab<T>(abName, assetName, id, parent);
}
public void Destory(string assetName)
{
if (AppEntry.Base.EditorResourceMode)
{
int startIdx = assetName.LastIndexOf('/') + 1;
int endIdx = assetName.IndexOf('.');
assetName = assetName.Substring(startIdx, endIdx - startIdx);
assetName += "(Clone)";
Transform trans = GamePlayEntry.UI.transform.Find(assetName);
if (trans == null)
return;
DestroyImmediate(trans.gameObject);
}
else
PrefabManager.DestoryByAssetName(assetName);
}
public Object LoadAsset(string assetName)
{
string abName = GetBundleName(assetName);
#if UNITY_EDITOR
return AssetDatabase.LoadAssetAtPath<Object>(assetName);
#else
return AssetManager.LoadAsset<Object>(abName, assetName.ToLower());
#endif
}
public void RemoveAsset(string assetName)
{
if (!AppEntry.Base.EditorResourceMode)
AssetManager.Unload(assetName);
}
}
}