#if ENABLE_PROPERTY_VARIANTS || PACKAGE_DOCS_GENERATION
using System;
using System.Collections.Generic;
using UnityEngine.Localization.PropertyVariants.TrackedProperties;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace UnityEngine.Localization.PropertyVariants.TrackedObjects
{
///
/// 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 to support the component.
///
///
///
[Serializable]
public abstract class TrackedObject : ISerializationCallbackReceiver
{
// Class so we can provide a custom collection PropertyDrawer
[Serializable]
internal class TrackedPropertiesCollection
{
[SerializeReference]
public List items = new List();
}
[SerializeField, HideInInspector]
Object m_Target;
[SerializeField]
TrackedPropertiesCollection m_TrackedProperties = new TrackedPropertiesCollection();
readonly Dictionary m_PropertiesLookup = new Dictionary();
///
/// The target that the variants will be applied to.
///
public Object Target
{
get => m_Target;
set => m_Target = value;
}
///
/// The tracked properties for this object.
///
public IList TrackedProperties => m_TrackedProperties.items;
///
/// Can be used to reject certain properties.
///
///
///
public virtual bool CanTrackProperty(string propertyPath) => true;
///
/// Create and add a tracked property for this object.
///
///
///
///
public T AddTrackedProperty(string propertyPath) where T : ITrackedProperty, new()
{
var property = new T { PropertyPath = propertyPath };
AddTrackedProperty(property);
return property;
}
///
/// Add a tracked property for this object.
///
///
public virtual void AddTrackedProperty(ITrackedProperty trackedProperty)
{
if (trackedProperty == null)
throw new ArgumentNullException(nameof(trackedProperty));
if (string.IsNullOrEmpty(trackedProperty.PropertyPath))
throw new ArgumentException("Property path must not be null or empty.");
if (m_PropertiesLookup.ContainsKey(trackedProperty.PropertyPath))
throw new ArgumentException(trackedProperty.PropertyPath + " is already tracked.");
m_PropertiesLookup[trackedProperty.PropertyPath] = trackedProperty;
TrackedProperties.Add(trackedProperty);
}
///
/// Remove a tracked property for this object.
///
/// The tracked property to be removed.
/// Returns if the value was removed; otherwise .
public virtual bool RemoveTrackedProperty(ITrackedProperty trackedProperty)
{
m_PropertiesLookup.Remove(trackedProperty.PropertyPath);
return TrackedProperties.Remove(trackedProperty);
}
///
/// Return the tracked property for the property path.
///
///
/// The serialized property path.
/// Specify whether to create a property if no existing property is found.
///
public T GetTrackedProperty(string propertyPath, bool create = true) where T : ITrackedProperty, new()
{
var prop = GetTrackedProperty(propertyPath);
if (prop is T tVal)
return tVal;
return create ? AddTrackedProperty(propertyPath) : default;
}
///
/// Return the tracked property for the property path.
///
/// The serialized property path.
///
public virtual ITrackedProperty GetTrackedProperty(string propertyPath) => m_PropertiesLookup.TryGetValue(propertyPath, out var property) ? property : null;
public virtual ITrackedProperty CreateCustomTrackedProperty(string propertyPath) => null;
///
/// Apply the for .
/// If a value does not exist for this locale then the value for is used as a fallback.
///
/// The chosen variant to apply to .
/// The fallback to use when a value does not exist for this variant.
public abstract AsyncOperationHandle ApplyLocale(Locale variantLocale, Locale defaultLocale);
///
/// Called when the variants have been applied to .
///
protected virtual void PostApplyTrackedProperties() {}
public void OnAfterDeserialize()
{
m_PropertiesLookup.Clear();
foreach (var trackedProperty in m_TrackedProperties.items)
{
if (trackedProperty != null)
m_PropertiesLookup[trackedProperty.PropertyPath] = trackedProperty;
}
}
public void OnBeforeSerialize()
{
m_TrackedProperties.items.Clear();
foreach (var trackedProperty in m_PropertiesLookup.Values)
{
m_TrackedProperties.items.Add(trackedProperty);
}
}
}
}
#endif