docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class TrackedObject

    Provides common Property Variant functionality for a Unity object. You can inherit from this class to create custom object trackers. This shows how to create a UnityEngine.Localization.PropertyVariants.TrackedObjects to support the AudioSource component.

    [Serializable]
    [DisplayName("Audio Source")]
    [CustomTrackedObject(typeof(AudioSource), false)]
    public class TrackedAudioSource : TrackedObject
    {
        public override AsyncOperationHandle ApplyLocale(Locale variantLocale, Locale defaultLocale)
        {
            var audioClipProperty = GetTrackedProperty("m_audioClip");
            if (audioClipProperty == null)
                return default;
    
            // Check if the Asset is stored in an Asset Table
            if (audioClipProperty is LocalizedAssetProperty localizedAssetProperty &&
                localizedAssetProperty.LocalizedObject is LocalizedAudioClip localizedAudioClip)
            {
                localizedAudioClip.LocaleOverride = variantLocale;
                var loadHandle = localizedAudioClip.LoadAssetAsync();
                if (loadHandle.IsDone)
                    AudioClipLoaded(loadHandle);
                else
                {
                    loadHandle.Completed += AudioClipLoaded;
                    return loadHandle;
                }
            }
            // Check if the Asset is stored locally
            else if (audioClipProperty is UnityObjectProperty localAssetProperty)
            {
                if (localAssetProperty.GetValue(variantLocale.Identifier, defaultLocale.Identifier, out var clip))
                    SetAudioClip(clip as AudioClip);
            }
    
            return default;
        }
    
        void AudioClipLoaded(AsyncOperationHandle<AudioClip> loadHandle)
        {
            SetAudioClip(loadHandle.Result);
        }
    
        void SetAudioClip(AudioClip clip)
        {
            var source = (AudioSource)Target;
            source.Stop();
            source.clip = clip;
            if (clip != null)
                source.Play();
        }
    
        public override bool CanTrackProperty(string propertyPath)
        {
            // We only care about the Audio clip
            return propertyPath == "m_audioClip";
        }
    }
    Inheritance
    object
    TrackedObject
    JsonSerializerTrackedObject
    TrackedMeshFilter
    TrackedTransform
    Implements
    ISerializationCallbackReceiver
    Namespace: UnityEngine.Localization.PropertyVariants.TrackedObjects
    Assembly: Unity.Localization.dll
    Syntax
    [Serializable]
    public abstract class TrackedObject : ISerializationCallbackReceiver

    Properties

    Name Description
    Target

    The target that the variants will be applied to.

    TrackedProperties

    The tracked properties for this object.

    Methods

    Name Description
    AddTrackedProperty(ITrackedProperty)

    Add a tracked property for this object.

    AddTrackedProperty<T>(string)

    Create and add a tracked property for this object.

    ApplyLocale(Locale, Locale)

    Apply the TrackedProperties for variantLocale. If a value does not exist for this locale then the value for defaultLocale is used as a fallback.

    CanTrackProperty(string)

    Can be used to reject certain properties.

    CreateCustomTrackedProperty(string)
    GetTrackedProperty(string)

    Return the tracked property for the property path.

    GetTrackedProperty<T>(string, bool)

    Return the tracked property for the property path.

    PostApplyTrackedProperties()

    Called when the variants have been applied to Target.

    RemoveTrackedProperty(ITrackedProperty)

    Remove a tracked property for this object.

    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)