docs.unity3d.com
Search Results for

    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
    Implements
    ISerializationCallbackReceiver
    Inherited Members
    LocalizedReference.m_CurrentTable
    LocalizedReference.m_CurrentTableEntry
    LocalizedReference.TableReference
    LocalizedReference.TableEntryReference
    LocalizedReference.FallbackState
    LocalizedReference.LocaleOverride
    LocalizedReference.IsEmpty
    LocalizedReference.SetReference(TableReference, TableEntryReference)
    Namespace: UnityEngine.Localization
    Assembly: Unity.Localization.dll
    Syntax
    [Serializable]
    public class LocalizedAsset<TObject> : LocalizedAssetBase, ISerializationCallbackReceiver 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);
        }
    }

    This example shows how to bind to a UI Toolkit property. Note that this requires Unity 2023.2 and above.

    using UnityEngine;
    using UnityEngine.Localization;
    using UnityEngine.UIElements;
    
    public class LocalizedTextureUIDocumentExample : MonoBehaviour
    {
        void Start()
        {
            var document = GetComponent<UIDocument>();
            var label = new Label();
            label.text = "Some localized image";
            document.rootVisualElement.Add(label);
    
            // Add binding to the background style property of the label.
            var localizedTexture = new LocalizedTexture { TableReference = "My asset table", TableEntryReference = "My Texture" };
            label.SetBinding("style.backgroundImage", localizedTexture);
        }
    }

    Constructors

    Name Description
    LocalizedAsset()

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

    Properties

    Name Description
    CurrentLoadingOperation

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

    CurrentLoadingOperationHandle

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

    HasChangeHandler

    Returns true if AssetChanged has any subscribers.

    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.

    Methods

    Name Description
    ClearChangeHandler()
    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.

    LoadAssetAsObjectAsync()

    Returns the localized asset as a UnityEngine.Object.

    LoadAssetAsync()

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

    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, callingLoadAsset 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.

    RegisterChangeHandler(ChangeHandler)
    Reset()

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

    Events

    Name Description
    AssetChanged

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

    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)