using System;
using UnityEngine.Localization.Operations;
using UnityEngine.Localization.Tables;
using UnityEngine.Pool;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace UnityEngine.Localization.Settings
{
///
/// The Localized Asset Database provides a single point of access for all localized assets.
///
///
/// A localized asset must derive from .
///
[Serializable]
public class LocalizedAssetDatabase : LocalizedDatabase
{
///
/// Returns a handle to a localized asset loading operation from the .
/// This method is asynchronous and may not have an immediate result.
/// Check [IsDone](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.IsDone) to see if the data is available,
/// if it is false then you can use the [Completed](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.Completed) event to get a callback when it is finished,
/// yield on the operation or call [WaitForCompletion](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.WaitForCompletion)
/// to force the operation to complete.
/// Once the Completed event has been called, during the next update, the internal operation will be returned to a pool so that it can be reused.
/// If you do plan to keep hold of the handle after completion then you should call [Acquire](xref::UnityEngine.ResourceManagement.AsyncOperationHandle.Acquire)
/// to prevent the operation being reused and to finally return the operation back to the pool.
///
/// The type of asset that should be loaded.
/// A reference to the entry in the
/// The to load the table from. Null will use .
/// Determines if a fallback should be used when no value could be found for the Locale.
///
public AsyncOperationHandle GetLocalizedAssetAsync(TableEntryReference tableEntryReference, Locale locale = null, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings) where TObject : Object
{
return GetLocalizedAssetAsync(GetDefaultTable(), tableEntryReference, locale, fallbackBehavior);
}
///
/// Returns a localized asset from the .
///
/// The type of asset that should be loaded.
/// A reference to the entry in the
/// The to load the table from. Null will use .
///
public TObject GetLocalizedAsset(TableEntryReference tableEntryReference, Locale locale = null) where TObject : Object
{
return GetLocalizedAssetAsync(GetDefaultTable(), tableEntryReference, locale).WaitForCompletion();
}
///
/// Returns a handle to a localized asset loading operation from the requested table.
/// This method is asynchronous and may not have an immediate result.
/// Check [IsDone](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.IsDone) to see if the data is available,
/// if it is false then you can use the [Completed](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.Completed) event to get a callback when it is finished,
/// yield on the operation or call [WaitForCompletion](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.WaitForCompletion)
/// to force the operation to complete.
/// Once the Completed event has been called, during the next update, the internal operation will be returned to a pool so that it can be reused.
/// If you do plan to keep hold of the handle after completion then you should call [Acquire](xref::UnityEngine.ResourceManagement.AsyncOperationHandle.Acquire)
/// to prevent the operation being reused and to finally return the operation back to the pool.
///
/// The type of asset that should be loaded.
/// A reference to the table that the asset should be loaded from.
/// A reference to the entry in the table.
/// The to load the table from. Null will use .
/// Determines if a fallback should be used when no value could be found for the Locale.
///
public virtual AsyncOperationHandle GetLocalizedAssetAsync(TableReference tableReference, TableEntryReference tableEntryReference, Locale locale = null, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings) where TObject : Object
{
return GetLocalizedAssetAsyncInternal(tableReference, tableEntryReference, locale, fallbackBehavior);
}
///
/// Returns a localized asset from the requested table.
///
/// The type of asset that should be loaded.
/// A reference to the table that the asset should be loaded from.
/// A reference to the entry in the table.
/// The to load the table from. Null will use .
///
public virtual TObject GetLocalizedAsset(TableReference tableReference, TableEntryReference tableEntryReference, Locale locale = null) where TObject : Object
{
return GetLocalizedAssetAsyncInternal(tableReference, tableEntryReference, locale).WaitForCompletion();
}
///
/// Implementation for all versions of .
///
/// The type of asset that should be loaded.
/// A reference to the table that the asset should be loaded from.
/// A reference to the entry in the table.
/// The to use instead of the default
/// Determines if a fallback should be used when no value could be found for the Locale.
///
protected virtual AsyncOperationHandle GetLocalizedAssetAsyncInternal(TableReference tableReference, TableEntryReference tableEntryReference, Locale locale, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings) where TObject : Object
{
var tableEntryOperation = GetTableEntryAsync(tableReference, tableEntryReference, locale, fallbackBehavior);
var operation = LoadAssetOperation.Pool.Get();
operation.Init(tableEntryOperation, true);
operation.Dependency = tableEntryOperation;
var handle = AddressablesInterface.ResourceManager.StartOperation(operation, tableEntryOperation);
return handle;
}
internal override void ReleaseTableContents(AssetTable table) => table.ReleaseAssets();
}
}