307 lines
8.2 KiB
C#
307 lines
8.2 KiB
C#
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.Events;
|
||
|
|
using BansheeGz.BGDatabase;
|
||
|
|
using UnityEngine.AddressableAssets;
|
||
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
||
|
|
using UnityEngine.ResourceManagement.ResourceLocations;
|
||
|
|
using UnityEditor;
|
||
|
|
using System.Data;
|
||
|
|
|
||
|
|
public class BGDataBaseManager : MonoBehaviour
|
||
|
|
{
|
||
|
|
private BGRepo BGDataBase = null;
|
||
|
|
public BGRepo DataBase
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
if (BGDataBase != null)
|
||
|
|
{
|
||
|
|
return BGDataBase;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
StartCoroutine(LoadDataBase());
|
||
|
|
return BGDataBase;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public UnityAction<bool> OnCompleteLoadHuntingImage;
|
||
|
|
|
||
|
|
private float loadPercent;
|
||
|
|
public float LoadPercent
|
||
|
|
{
|
||
|
|
get { return loadPercent; }
|
||
|
|
}
|
||
|
|
|
||
|
|
private Dictionary<string, AsyncOperationHandle<Sprite>> loadUnlockHuntingImageHandle;
|
||
|
|
public Dictionary<string, AsyncOperationHandle<Sprite>> LoadUnlockHuntingImageHandle
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
return loadUnlockHuntingImageHandle;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Awake()
|
||
|
|
{
|
||
|
|
//InitDataBase();
|
||
|
|
//StartCoroutine(LoadDataBase());
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnDestroy()
|
||
|
|
{
|
||
|
|
ReleaseHuntingUnlockImage();
|
||
|
|
}
|
||
|
|
|
||
|
|
public Coroutine InitDataBase()
|
||
|
|
{
|
||
|
|
return StartCoroutine(LoadDataBase());
|
||
|
|
}
|
||
|
|
|
||
|
|
IEnumerator LoadDataBase()
|
||
|
|
{
|
||
|
|
if (BGDataBase != null)
|
||
|
|
{
|
||
|
|
yield break;
|
||
|
|
}
|
||
|
|
|
||
|
|
yield return new WaitForSecondsRealtime(1.5f);
|
||
|
|
|
||
|
|
BGRepo.SetDefaultRepoContent(NeedAsset.Instance.BGDataBaseText.bytes);
|
||
|
|
BGRepo.Load();
|
||
|
|
|
||
|
|
BGDataBase = BGRepo.I;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnDatabaseLoaded(AsyncOperationHandle<TextAsset> handle)
|
||
|
|
{
|
||
|
|
if (handle.Status == AsyncOperationStatus.Succeeded)
|
||
|
|
{
|
||
|
|
BGRepo.SetDefaultRepoContent(handle.Result.bytes);
|
||
|
|
BGRepo.Load();
|
||
|
|
|
||
|
|
BGDataBase = BGRepo.I;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void LoadHuntingUnlockImage()
|
||
|
|
{
|
||
|
|
int count = 0;
|
||
|
|
int EndCount = DB_HuntingData.CountEntities;
|
||
|
|
float onePercent = 1.0f / DB_HuntingData.CountEntities;
|
||
|
|
|
||
|
|
loadUnlockHuntingImageHandle = new Dictionary<string, AsyncOperationHandle<Sprite>>(EndCount);
|
||
|
|
DB_HuntingData.ForEachEntity(delegate (DB_HuntingData entity)
|
||
|
|
{
|
||
|
|
if (entity != null)
|
||
|
|
{
|
||
|
|
string address = entity.GetDBF_UnlockImageAddressablesAddress();
|
||
|
|
AsyncOperationHandle<Sprite> spriteHandle = Addressables.LoadAssetAsync<Sprite>(address);
|
||
|
|
loadUnlockHuntingImageHandle.Add(address, spriteHandle);
|
||
|
|
|
||
|
|
spriteHandle.Completed += handle =>
|
||
|
|
{
|
||
|
|
if (handle.Status == AsyncOperationStatus.Succeeded)
|
||
|
|
{
|
||
|
|
loadPercent += onePercent;
|
||
|
|
count++;
|
||
|
|
|
||
|
|
if (EndCount <= count)
|
||
|
|
{
|
||
|
|
OnCompleteLoadHuntingImage?.Invoke(true);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Debug.Log("BGDataBaseManager: " + handle.OperationException.Message);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ReleaseHuntingUnlockImage()
|
||
|
|
{
|
||
|
|
if (loadUnlockHuntingImageHandle != null)
|
||
|
|
{
|
||
|
|
foreach (KeyValuePair<string, AsyncOperationHandle<Sprite>> handle in loadUnlockHuntingImageHandle)
|
||
|
|
{
|
||
|
|
if (handle.Value.IsValid())
|
||
|
|
{
|
||
|
|
Addressables.Release(handle.Value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
loadUnlockHuntingImageHandle.Clear();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool IsFirstHuntingData(BGId huntingListID, BGId huntingDataID)
|
||
|
|
{
|
||
|
|
DB_HuntingListData listData = DB_HuntingListData.GetEntity(huntingListID);
|
||
|
|
if (listData != null && listData.DBF_FirstHuntingDataID == huntingDataID)
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public BGId GetLastUnolockHuntingDataID()
|
||
|
|
{
|
||
|
|
float lastCount = GameManager.DB.GetUnlockHuntingCount();
|
||
|
|
int huntingListIndex = Mathf.FloorToInt((lastCount * 0.5f) - 0.5f);
|
||
|
|
|
||
|
|
DB_HuntingListData huntingListData = DB_HuntingListData.GetEntity(huntingListIndex);
|
||
|
|
|
||
|
|
if (huntingListData != null)
|
||
|
|
{
|
||
|
|
if ((lastCount * 0.5f) - 0.5f > huntingListIndex)
|
||
|
|
{
|
||
|
|
return huntingListData.DBF_SecondHuntingDataID;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
return huntingListData.DBF_FirstHuntingDataID;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return BGId.Empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
public BGId GetLastUnolockHuntingListDataID()
|
||
|
|
{
|
||
|
|
float lastCount = GameManager.DB.GetUnlockHuntingCount();
|
||
|
|
int huntingListIndex = Mathf.FloorToInt((lastCount * 0.5f) - 0.5f);
|
||
|
|
|
||
|
|
DB_HuntingListData huntingListData = DB_HuntingListData.GetEntity(huntingListIndex);
|
||
|
|
if (huntingListData != null)
|
||
|
|
{
|
||
|
|
return huntingListData.Id;
|
||
|
|
}
|
||
|
|
|
||
|
|
return BGId.Empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
public BGId GetNextLockHuntingDataID()
|
||
|
|
{
|
||
|
|
float lastCount = GameManager.DB.GetUnlockHuntingCount() + 1;
|
||
|
|
if (GameManager.DB.GetHuntingListLength() > lastCount)
|
||
|
|
{
|
||
|
|
lastCount = GameManager.DB.GetHuntingListLength();
|
||
|
|
}
|
||
|
|
|
||
|
|
int huntingListIndex = Mathf.FloorToInt((lastCount * 0.5f) - 0.5f);
|
||
|
|
|
||
|
|
if (huntingListIndex >= DB_HuntingListData.CountEntities)
|
||
|
|
{
|
||
|
|
return BGId.Empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
DB_HuntingListData huntingListData = DB_HuntingListData.GetEntity(huntingListIndex);
|
||
|
|
|
||
|
|
if (huntingListData != null)
|
||
|
|
{
|
||
|
|
if ((lastCount / 2.0f) > huntingListIndex)
|
||
|
|
{
|
||
|
|
return huntingListData.DBF_SecondHuntingDataID;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
return huntingListData.DBF_FirstHuntingDataID;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return BGId.Empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
public BGId GetNextLockHuntingListDataID()
|
||
|
|
{
|
||
|
|
float lastCount = GameManager.DB.GetUnlockHuntingCount() + 1;
|
||
|
|
if (GameManager.DB.GetHuntingListLength() <= lastCount)
|
||
|
|
{
|
||
|
|
lastCount = GameManager.DB.GetHuntingListLength();
|
||
|
|
}
|
||
|
|
|
||
|
|
int huntingListIndex = Mathf.FloorToInt((lastCount * 0.5f) - 0.5f);
|
||
|
|
|
||
|
|
if (huntingListIndex >= DB_HuntingListData.CountEntities)
|
||
|
|
{
|
||
|
|
return BGId.Empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
DB_HuntingListData huntingListData = DB_HuntingListData.GetEntity(huntingListIndex);
|
||
|
|
|
||
|
|
if (huntingListData != null)
|
||
|
|
{
|
||
|
|
return huntingListData.Id;
|
||
|
|
}
|
||
|
|
|
||
|
|
return BGId.Empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
public BGId GetFirstHuntingData(BGId huntingListID)
|
||
|
|
{
|
||
|
|
DB_HuntingListData data = DB_HuntingListData.GetEntity(huntingListID);
|
||
|
|
if (data != null)
|
||
|
|
{
|
||
|
|
return data.DBF_FirstHuntingDataID;
|
||
|
|
}
|
||
|
|
return BGId.Empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
public BGId GetSecondHuntingData(BGId huntingListID)
|
||
|
|
{
|
||
|
|
DB_HuntingListData data = DB_HuntingListData.GetEntity(huntingListID);
|
||
|
|
if (data != null)
|
||
|
|
{
|
||
|
|
return data.DBF_SecondHuntingDataID;
|
||
|
|
}
|
||
|
|
return BGId.Empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
public BGId GetNextHuntingListData(BGId huntingListID)
|
||
|
|
{
|
||
|
|
DB_HuntingListData data = DB_HuntingListData.GetEntity(huntingListID);
|
||
|
|
if(data.Index + 1 < DB_HuntingListData.CountEntities)
|
||
|
|
{
|
||
|
|
DB_HuntingListData nextData = DB_HuntingListData.GetEntity(data.Index + 1);
|
||
|
|
return nextData.Id;
|
||
|
|
}
|
||
|
|
return BGId.Empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
public int GetHuntingDataIndex(BGId huntingID)
|
||
|
|
{
|
||
|
|
DB_HuntingData data = DB_HuntingData.GetEntity(huntingID);
|
||
|
|
if (data != null)
|
||
|
|
{
|
||
|
|
return data.Index;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
private class TextAssetDatabaseResource : BGRepoCustomLoaderModel.DatabaseResource
|
||
|
|
{
|
||
|
|
private readonly TextAsset asset;
|
||
|
|
|
||
|
|
public TextAsset Asset
|
||
|
|
{
|
||
|
|
get { return asset; }
|
||
|
|
}
|
||
|
|
|
||
|
|
public TextAssetDatabaseResource(TextAsset asset)
|
||
|
|
{
|
||
|
|
this.asset = asset;
|
||
|
|
}
|
||
|
|
|
||
|
|
public override byte[] Content
|
||
|
|
{
|
||
|
|
get { return asset.bytes; }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|