119 lines
3.1 KiB
C#
Raw Permalink Normal View History

2025-05-18 01:04:31 +08:00
// ***********************************************************************
// Assembly : Unity
// Author : Kimch
// Created : 2019-05-20
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "ItemGroupHelper" company="Kunpo"></copyright>
// <summary></summary>
// ***********************************************************************
namespace G
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using F;
using F.Item;
using UnityEngine;
public class ItemGroupHelper : IItemGroupHelper
{
private readonly Dictionary<Type, string> _itemClasses = new Dictionary<Type, string>();
private Dictionary<string, byte[]> _itemAssets;
public void AddClass<T>(string className)
{
var t = typeof(T);
string assetName = (string)t.GetField("ASSET_NAME").GetValue(null);
_itemClasses.Add(t, assetName);
}
public void Begin()
{
_itemAssets = new Dictionary<string, byte[]>();
}
/// <summary>
///ItemProxy代理类所有注册类型的配置数据
/// </summary>
/// <param name="group"></param>
///// <param name="result"></param>
/// <returns></returns>
public bool Load<T>(List<T> result) where T : IItem
{
var groupType = typeof(T);
if (_itemClasses.TryGetValue(groupType, out string assetName))
if (_itemAssets.TryGetValue(assetName, out byte[] jsonBytes))
{
var assetDict = jsonBytes.ToJsonTable();
groupType.GetMethod("LoadAll").Invoke(null, new object[] { assetDict, result });
return true;
}
return false;
}
public void End()
{
_itemAssets = null;
Resources.UnloadUnusedAssets();
}
System.Diagnostics.Stopwatch _loadSw;
public async Task BeginAsync()
{
_loadSw = System.Diagnostics.Stopwatch.StartNew();
var asyncOp = AssetProxy.Instance.GetAssetsAsync<TextAsset>("config");
var textList = await asyncOp.Task;
_itemAssets = new Dictionary<string, byte[]>(textList.Count);
foreach (var text in textList)
{
_itemAssets[text.name] = text.bytes;
}
AssetProxy.Instance.Release(asyncOp);
LanguageProxy.Instance.LoadBytes(_itemAssets["language"]);
}
public async Task<bool> LoadAsync<T>(List<T> result) where T : IItem
{
var groupType = typeof(T);
if (_itemClasses.TryGetValue(groupType, out string assetName))
if (_itemAssets.TryGetValue(assetName, out byte[] jsonBytes))
{
var assetDict = jsonBytes.ToJsonTable();
groupType.GetMethod("LoadAll").Invoke(null, new object[] { assetDict, result });
return true;
}
//await Task.Yield();
return false;
}
public async Task EndAsync()
{
_itemAssets = null;
_loadSw.Stop();
//#if UNITY_EDITOR
Debug.Log("gsw " + _loadSw.ElapsedMilliseconds);
//#endif
await Task.Yield();
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="result"></param>
public void LoadCompleted<T>(List<T> result) where T : IItem
{
foreach (var item in result)
{
(item as Item).Init();
}
}
}
}