docs.unity3d.com
    Show / Hide Table of Contents

    Class CustomTrackedObjectAttribute

    Indicates that the class is used to Track Property Variants for a particular Type.

    Inheritance
    Object
    Attribute
    CustomTrackedObjectAttribute
    Namespace: UnityEngine.Localization.PropertyVariants
    Syntax
    [AttributeUsage(AttributeTargets.Class)]
    public class CustomTrackedObjectAttribute : Attribute, _Attribute
    Examples

    This shows how to create a custom TrackedObject 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";
    }
    }

    Constructors

    CustomTrackedObjectAttribute(Type, Boolean)

    Creates a new instance of a CustomTrackedObjectAttribute.

    Declaration
    public CustomTrackedObjectAttribute(Type type, bool supportsInheritedTypes)
    Parameters
    Type Name Description
    Type type

    The Type of Object that this Tracked Object supports.

    Boolean supportsInheritedTypes

    Does this class also support types that inherit from type?

    In This Article
    • Constructors
      • CustomTrackedObjectAttribute(Type, Boolean)
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023