using System;
using UnityEngine.Assertions;
namespace UnityEngine.Rendering.HighDefinition
{
///
/// Define the level's value for a .
///
/// Use this setting in an asset that defines the quality settings for a specific platform.
/// Then those settings can be used with the to get the actual value to use.
///
/// If you intend to serialize this type, use specialized version instead. ().
///
/// The type of the scalable setting.
[Serializable]
public class ScalableSetting : ISerializationCallbackReceiver
{
[SerializeField] private T[] m_Values;
[SerializeField] private ScalableSettingSchemaId m_SchemaId;
/// Build a new scalable setting.
/// The values of the scalable setting. Must not be null.
/// The of the schema for this scalable setting.
public ScalableSetting(T[] values, ScalableSettingSchemaId schemaId)
{
Assert.IsNotNull(values, $"{nameof(values)} must not be null.");
m_Values = values;
m_SchemaId = schemaId;
}
/// The schema id of this scalable setting.
public ScalableSettingSchemaId schemaId
{
get => m_SchemaId;
set => m_SchemaId = value;
}
/// Get the value for a specific level.
/// The index of the value to get.
///
/// If the is in the range of the contained values, the associated value will be returned.
/// Otherwise, default is returned.
///
public T this[int index] => m_Values != null && index >= 0 && index < m_Values.Length ? m_Values[index] : default;
///
/// Get the value of the level .
///
/// The index of the level to get.
///
/// Contains the value of the level when the level is found.
///
/// default when:
/// - The index is out of range
///
/// true when the value was evaluated, false when the value could not be evaluated.
public bool TryGet(int index, out T value)
{
Assert.IsNotNull(m_Values, "Values must not be null, it was checked in the constructor and in serialization callbacks");
if (index >= 0 && index < m_Values.Length)
{
value = m_Values[index];
return true;
}
value = default;
return false;
}
/// Serialization callback
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
// Enforce the number of stored value after serialization
if (ScalableSettingSchema.Schemas.TryGetValue(m_SchemaId, out var schema))
Array.Resize(ref m_Values, schema.levelCount);
else if (m_Values == null)
m_Values = new T[0];
}
/// Serialization callback
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
// Enforce the number of stored value before serialization
if (ScalableSettingSchema.Schemas.TryGetValue(m_SchemaId, out var schema))
Array.Resize(ref m_Values, schema.levelCount);
else if (m_Values == null)
m_Values = new T[0];
}
}
#region Specialized Scalable Settings
// We define explicitly specialized version of the ScalableSetting so it can be serialized with
// Unity's serialization API.
/// .
[Serializable]
public class IntScalableSetting : ScalableSetting
{
///
/// Instantiate a new int scalable setting.
///
/// The values of the settings
/// The schema of the setting.
public IntScalableSetting(int[] values, ScalableSettingSchemaId schemaId) : base(values, schemaId) {}
}
/// .
[Serializable]
public class UintScalableSetting : ScalableSetting
{
///
/// Instantiate a new uint scalable setting.
///
/// The values of the settings
/// The schema of the setting.
public UintScalableSetting(uint[] values, ScalableSettingSchemaId schemaId) : base(values, schemaId) {}
}
/// .
[Serializable]
public class FloatScalableSetting : ScalableSetting
{
///
/// Instantiate a new float scalable setting.
///
/// The values of the settings
/// The schema of the setting.
public FloatScalableSetting(float[] values, ScalableSettingSchemaId schemaId) : base(values, schemaId) {}
}
/// .
[Serializable]
public class BoolScalableSetting : ScalableSetting
{
///
/// Instantiate a new bool scalable setting.
///
/// The values of the settings
/// The schema of the setting.
public BoolScalableSetting(bool[] values, ScalableSettingSchemaId schemaId) : base(values, schemaId) {}
}
#endregion
}