docs.unity3d.com
    Show / Hide Table of Contents

    Class LocalizedAsset<TObject>

    Provides a way to reference an AssetTableEntry inside of a specific AssetTable and request the localized asset.

    Inheritance
    Object
    LocalizedReference
    LocalizedAssetBase
    LocalizedAsset<TObject>
    LocalizedAudioClip
    LocalizedFont
    LocalizedGameObject
    LocalizedMaterial
    LocalizedMesh
    LocalizedObject
    LocalizedSprite
    LocalizedTexture
    LocalizedTmpFont
    Inherited Members
    LocalizedReference.TableReference
    LocalizedReference.TableEntryReference
    LocalizedReference.FallbackState
    LocalizedReference.LocaleOverride
    LocalizedReference.IsEmpty
    LocalizedReference.SetReference(TableReference, TableEntryReference)
    Namespace: UnityEngine.Localization
    Syntax
    [Serializable]
    public class LocalizedAsset<TObject> : LocalizedAssetBase, ISerializationCallbackReceiver, IDisposable where TObject : Object
    Type Parameters
    Name Description
    TObject

    The type that should be supported. This can be any type that inherits from UnityEngine.Object.

    Examples

    This example shows how a LocalizedAsset<TObject> can be used to localize a Prefabs. See also LocalizedGameObject and LocalizedGameObjectEvent.

    public class LocalizedPrefabExample : MonoBehaviour
    {
    [Serializable]
    public class LocalizedPrefab : LocalizedAsset<GameObject> {}
    
    public LocalizedPrefab localizedPrefab;
    
    GameObject currentInstance;
    
    void OnEnable()
    {
        localizedPrefab.AssetChanged += UpdatePrefab;
    }
    
    void OnDisable()
    {
        localizedPrefab.AssetChanged -= UpdatePrefab;
    }
    
    void UpdatePrefab(GameObject value)
    {
        if (currentInstance != null)
            Destroy(currentInstance);
    
        currentInstance = Instantiate(value);
    }
    }

    This example shows how a LocalizedAsset<TObject> can be used to localize a ScriptableObject.

    [CreateAssetMenu]
    public class MyScriptableObject : ScriptableObject
    {
    public string someStringValue;
    }
    
    [Serializable]
    public class LocalizedMyScriptableObject : LocalizedAsset<MyScriptableObject> { }
    
    public class Example : MonoBehaviour
    {
    public LocalizedMyScriptableObject localizedScriptableObject;
    
    void OnEnable()
    {
        localizedScriptableObject.AssetChanged += OnAssetChanged;
    }
    
    void OnDisable()
    {
        localizedScriptableObject.AssetChanged -= OnAssetChanged;
    }
    
    void OnAssetChanged(MyScriptableObject value)
    {
        Debug.Log(value.someStringValue);
    }
    }

    Constructors

    LocalizedAsset()

    Initializes and returns an empty instance of a LocalizedAsset<TObject>.

    Declaration
    public LocalizedAsset()

    Properties

    CurrentLoadingOperationHandle

    The current loading operation for the asset when using AssetChanged. This is default if a loading operation is not available.

    Declaration
    public AsyncOperationHandle<TObject> CurrentLoadingOperationHandle { get; }
    Property Value
    Type Description
    AsyncOperationHandle<TObject>

    HasChangeHandler

    Returns true if AssetChanged has any subscribers.

    Declaration
    public bool HasChangeHandler { get; }
    Property Value
    Type Description
    Boolean

    WaitForCompletion

    Determines if WaitForCompletion should be used to force loading to be completed immediately. See Synchronous Workflow for further details. Please note that WaitForCompletion is not supported on WebGL.

    Declaration
    public override bool WaitForCompletion { set; }
    Property Value
    Type Description
    Boolean
    Overrides
    LocalizedReference.WaitForCompletion

    Methods

    ForceUpdate()

    Declaration
    protected override void ForceUpdate()
    Overrides
    LocalizedReference.ForceUpdate()

    LoadAsset()

    Provides a localized asset from a AssetTable with the TableReference and the the asset that matches TableEntryReference. Uses WaitForCompletion to force the loading to complete synchronously. Please note that WaitForCompletion is not supported on WebGL.

    Declaration
    public TObject LoadAsset()
    Returns
    Type Description
    TObject

    Returns the localized asset.

    LoadAssetAsObjectAsync()

    Returns the localized asset as a UnityEngine.Object.

    Declaration
    public override AsyncOperationHandle<Object> LoadAssetAsObjectAsync()
    Returns
    Type Description
    AsyncOperationHandle<Object>
    Overrides
    LocalizedAssetBase.LoadAssetAsObjectAsync()

    LoadAssetAsync()

    Provides a localized asset from a AssetTable with the TableReference and the the asset that matches TableEntryReference.

    Declaration
    public AsyncOperationHandle<TObject> LoadAssetAsync()
    Returns
    Type Description
    AsyncOperationHandle<TObject>

    Returns the loading operation for the request.

    Remarks

    The asset may have already been loaded, either during a previous operation or if Preload mode is used. Check the IsDone property to see if the asset is already loaded and therefore is immediately available. See Async operation handling for further details.

    Examples

    This example shows how LoadAssetAsync() can be used to request a sprite asset when the SelectedLocale changes.

    public class LocalizedSpriteExample : MonoBehaviour
    {
    public LocalizedSprite localizedSprite;
    
    public Image image;
    
    void OnEnable()
    {
        LocalizationSettings.SelectedLocaleChanged += SelectedLocaleChanged;
        StartCoroutine(LoadAssetCoroutine());
    }
    
    void OnDisable()
    {
        LocalizationSettings.SelectedLocaleChanged -= SelectedLocaleChanged;
    }
    
    void SelectedLocaleChanged(Locale obj)
    {
        StartCoroutine(LoadAssetCoroutine());
    }
    
    IEnumerator LoadAssetCoroutine()
    {
        var operation = localizedSprite.LoadAssetAsync();
        yield return operation;
        image.sprite = operation.Result;
    }
    }

    LoadAssetAsync<T>()

    Overrides the asset's default type. This loads a type from AssetTable with the TableReference and the the asset that matchesTableEntryReference. This helps to filter sub-assets when trying to load them and they share a common type among other sub-assets with the same name.
    For example, an asset could have the following structure:
    Main Asset [GameObject]
    - Sub Asset[GameObject]
    - Sub Asset[Mesh] If you were using a LocalizedObject, calling orLoadAssetAsObjectAsync() to load "Main Asset/Sub Asset" would return the first matching asset. In this case, this would be the GameObject version. With this method, you could provide the type Mesh to filter out the GameObject version and return the Mesh.

    Declaration
    public override AsyncOperationHandle<T> LoadAssetAsync<T>()
        where T : Object
    Returns
    Type Description
    AsyncOperationHandle<T>

    Returns the loading operation for the request.

    Type Parameters
    Name Description
    T
    Overrides
    LocalizedAssetBase.LoadAssetAsync<TObject>()

    Reset()

    Called when values are changed due to a change made via serialization, such as via the inspector.

    Declaration
    protected override void Reset()
    Overrides
    LocalizedReference.Reset()

    Events

    AssetChanged

    Provides a callback that will be invoked when the asset is available or has changed.

    Declaration
    public event LocalizedAsset<TObject>.ChangeHandler AssetChanged
    Event Type
    Type Description
    LocalizedAsset.ChangeHandler<>
    Remarks

    The following events will trigger an update:

    • The first time the action is added to the event.
    • The SelectedLocale changing.
    • The TableReference or TableEntryReference changing.

    When the first LocalizedAsset<TObject>.ChangeHandler is added, a loading operation (see CurrentLoadingOperationHandle) will automatically start and the localized asset will be sent to the subscriber when completed. Any adding additional subscribers added after loading has completed will also be sent the latest localized asset when they are added. This ensures that a subscriber will always have the correct localized value regardless of when it was added.

    Examples

    This example shows how the AssetChanged event could be used to change the Font on some localized Text.

    public class LocalizedTextWithFont : MonoBehaviour
    {
    [Serializable]
    public class LocalizedFont : LocalizedAsset<Font> {}
    
    public LocalizedString localizedString;
    public LocalizedFont localizedFont;
    
    public Text uiText;
    
    void OnEnable()
    {
       localizedString.StringChanged += UpdateText;
       localizedFont.AssetChanged += FontChanged;
    }
    
    void OnDisable()
    {
       localizedString.StringChanged -= UpdateText;
       localizedFont.AssetChanged -= FontChanged;
    }
    
    void FontChanged(Font f)
    {
       uiText.font = f;
    }
    
    void UpdateText(string s)
    {
       uiText.text = s;
    }
    }
    Back to top Copyright © 2023 Unity Technologies — Terms of use
    Generated by DocFX
    on Wednesday, September 20, 2023