using System; using UnityEngine; using UnityEngine.Serialization; namespace UnityEditor.AddressableAssets.Settings { /// /// Used to store references to profile variables. This class is intended to be used for fields in user scripts, specifically ones that subclass AddressableAssetGroupSchema. /// [Serializable] public class ProfileValueReference { [FormerlySerializedAs("m_id")] [SerializeField] string m_Id; /// /// This delegate will be invoked when the reference profile id changes. This will NOT be invoked when the actual profile value itself changes. /// public Action OnValueChanged; /// /// Get the profile variable id. /// public string Id { get { return m_Id; } internal set { m_Id = value; } } /// /// Evaluate the profile value using the provided settings object. /// /// The settings object to evaluate with. The activeProfileId will be used. /// The evaluated string stored in the referenced profile variable. public string GetValue(AddressableAssetSettings settings) { if (settings == null) { Debug.LogWarning("ProfileValueReference: AddressableAssetSettings object is null."); return null; } return GetValue(settings.profileSettings, settings.activeProfileId); } /// /// Evaluate the profile value using the provided profile settings object and a profile id. /// /// The profile settings object. /// The profile id. /// The evaluated string stored in the referenced profile variable. public string GetValue(AddressableAssetProfileSettings profileSettings, string profileId) { if (profileSettings == null) { Debug.LogWarning("ProfileValueReference: AddressableAssetProfileSettings object is null."); return null; } if (string.IsNullOrEmpty(profileId)) { Debug.LogWarning("ProfileValueReference: GetValue called with invalid profileId."); return null; } return profileSettings.EvaluateString(profileId, profileSettings.GetValueById(profileId, m_Id)); } /// /// Get the profile variable name that is referenced. /// /// The settings object. /// The name of the profile variable name. public string GetName(AddressableAssetSettings settings) { if (settings == null) { Debug.LogWarning("ProfileValueReference: GetName() - AddressableAssetSettings object is null."); return null; } var pid = settings.profileSettings.GetProfileDataById(m_Id); if (pid == null) Debug.LogWarningFormat("ProfileValueReference: GetName() - Unable to find variable id {0} in settings object.", m_Id); return pid == null ? string.Empty : pid.ProfileName; } internal bool HasValue(AddressableAssetSettings settings) { if (settings == null) { Debug.LogWarning("ProfileValueReference: HasValue() - AddressableAssetSettings object is null."); return false; } return !string.IsNullOrEmpty(settings.profileSettings.GetValueById(settings.activeProfileId, m_Id)); } /// /// Set the profile variable id using the id of the variable. /// /// The settings object. /// The id of the profile variable to set. /// True if the profile data is found and set, false otherwise. public bool SetVariableById(AddressableAssetSettings settings, string id) { if (settings == null) { Debug.LogWarning("ProfileValueReference: SetVariableById() - AddressableAssetSettings object is null."); return false; } if (string.IsNullOrEmpty(id)) { Debug.LogWarning("ProfileValueReference: SetVariableById() - invalid parameter id."); return false; } if (settings.profileSettings.GetProfileDataById(id) == null) { Debug.LogWarningFormat("ProfileValueReference: SetVariableById() - Unable to find variable id {0} in settings object.", id); return false; } m_Id = id; if (OnValueChanged != null) OnValueChanged(this); return true; } /// /// Set the profile variable id using the name of the variable. /// /// The settings object. /// The name of the profile variable to set. /// True if the profile data is found and set, false otherwise. public bool SetVariableByName(AddressableAssetSettings settings, string name) { if (settings == null) { Debug.LogWarning("ProfileValueReference: SetVariableByName() - AddressableAssetSettings object is null."); return false; } if (string.IsNullOrEmpty(name)) { Debug.LogWarning("ProfileValueReference: SetVariableByName() - invalid parameter name."); return false; } var idData = settings.profileSettings.GetProfileDataByName(name); if (idData == null) { Debug.LogWarningFormat("ProfileValueReference: SetVariableByName() - Unable to find variable name {0} in settings object.", name); return false; } m_Id = idData.Id; if (OnValueChanged != null) OnValueChanged(this); return true; } } }