Forest_Client/Forest/Assets/Scripts/Gameplay2/BuildSelectManager.cs

109 lines
3.1 KiB
C#

using System;
using PhxhSDK;
using Framework;
using UnityEngine;
using System.Linq;
using Framework.Constants;
using Cysharp.Threading.Tasks;
using System.Collections.Generic;
public class BuildSelectManager
{
public class BuildSelectItem
{
public Sprite BuildSprite;
public string BuildID;
public string PathSprite;
public int Index;
}
private static BuildSelectManager _instance;
public static BuildSelectManager Instance
{
get
{
if (_instance == null)
{
_instance = new BuildSelectManager();
}
return _instance;
}
}
public Dictionary<string, BuildSelectItem> BuildSelectItems;
private bool _isInit;
public async UniTask InitAsync()
{
if (_isInit) return;
try
{
BuildSelectItems = new Dictionary<string, BuildSelectItem>();
var buildConfig = TableManager.Instance.Tables.BuildConfig.DataMap;
var index = 0;
foreach (var build in buildConfig)
{
var id = build.Key;
var path = string.Format(PathConstants.BuildSelect, id);
var sprite = await AssetManager.Instance.LoadAssetAsync<Sprite>(path);
var buildItem = new BuildSelectItem
{
BuildSprite = sprite,
BuildID = id,
PathSprite = path,
Index = index
};
BuildSelectItems.Add(id, buildItem);
index++;
}
//添加敬请期待
var stayTurnSprite = await AssetManager.Instance.LoadAssetAsync<Sprite>(PathConstants.BuildSelectStayTurn);
var buildStayTurnItem = new BuildSelectItem
{
BuildSprite = stayTurnSprite,
BuildID = "None",
PathSprite = PathConstants.BuildSelectStayTurn,
Index = index
};
BuildSelectItems.Add("None", buildStayTurnItem);
_isInit = true;
}
catch (Exception e)
{
DebugUtil.LogError("BuildSelectManager.InitAsync Error: {0}", e);
}
}
public BuildSelectItem GetBuildSelectItem(string buildID)
{
return BuildSelectItems.GetValueOrDefault(buildID);
}
public BuildSelectItem GetNextBuildSelectItem(int index)
{
return BuildSelectItems.Values.FirstOrDefault(item => item.Index == index + 1);
}
public BuildSelectItem GetPreviousBuildSelectItem(int index)
{
return BuildSelectItems.Values.FirstOrDefault(item => item.Index == index - 1);
}
public void Release()
{
foreach (var buildSelectItem in BuildSelectItems)
{
AssetManager.Instance.Unload(buildSelectItem.Value.PathSprite);
buildSelectItem.Value.BuildSprite = null;
}
BuildSelectItems = null;
}
}