using System;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
using UnityEngine.Serialization;
namespace UnityEditor.AddressableAssets
{
///
/// Class used to get and set the default Addressable Asset settings object.
///
public class AddressableAssetSettingsDefaultObject : ScriptableObject
{
///
/// Default name for the addressable assets settings
///
public const string kDefaultConfigAssetName = "AddressableAssetSettings";
///
/// The default folder for the serialized version of this class.
///
public const string kDefaultConfigFolder = "Assets/AddressableAssetsData";
///
/// The name of the default config object
///
public const string kDefaultConfigObjectName = "com.unity.addressableassets";
///
/// Default path for addressable asset settings assets.
///
public static string DefaultAssetPath
{
get
{
return kDefaultConfigFolder + "/" + kDefaultConfigAssetName + ".asset";
}
}
[FormerlySerializedAs("m_addressableAssetSettingsGuid")]
[SerializeField]
internal string m_AddressableAssetSettingsGuid;
bool m_LoadingSettingsObject = false;
internal AddressableAssetSettings LoadSettingsObject()
{
//prevent re-entrant stack overflow
if (m_LoadingSettingsObject)
{
Debug.LogWarning("Detected stack overflow when accessing AddressableAssetSettingsDefaultObject.Settings object.");
return null;
}
if (string.IsNullOrEmpty(m_AddressableAssetSettingsGuid))
{
Debug.LogError("Invalid guid for default AddressableAssetSettings object.");
return null;
}
var path = AssetDatabase.GUIDToAssetPath(m_AddressableAssetSettingsGuid);
if (string.IsNullOrEmpty(path))
{
Debug.LogErrorFormat("Unable to determine path for default AddressableAssetSettings object with guid {0}.", m_AddressableAssetSettingsGuid);
return null;
}
m_LoadingSettingsObject = true;
var settings = AssetDatabase.LoadAssetAtPath(path);
if (settings != null)
AddressablesAssetPostProcessor.OnPostProcess.Register(settings.OnPostprocessAllAssets, 0);
m_LoadingSettingsObject = false;
return settings;
}
void SetSettingsObject(AddressableAssetSettings settings)
{
if (settings == null)
{
m_AddressableAssetSettingsGuid = null;
return;
}
var path = AssetDatabase.GetAssetPath(settings);
if (string.IsNullOrEmpty(path))
{
Debug.LogErrorFormat("Unable to determine path for default AddressableAssetSettings object with guid {0}.", m_AddressableAssetSettingsGuid);
return;
}
AddressablesAssetPostProcessor.OnPostProcess.Register(settings.OnPostprocessAllAssets, 0);
m_AddressableAssetSettingsGuid = AssetDatabase.AssetPathToGUID(path);
}
static AddressableAssetSettings s_DefaultSettingsObject;
///
/// Used to determine if a default settings asset exists.
///
public static bool SettingsExists
{
get
{
AddressableAssetSettingsDefaultObject so;
if (EditorBuildSettings.TryGetConfigObject(kDefaultConfigObjectName, out so))
return !string.IsNullOrEmpty(AssetDatabase.GUIDToAssetPath(so.m_AddressableAssetSettingsGuid));
return false;
}
}
///
/// Gets the default addressable asset settings object. This will return null during editor startup if EditorApplication.isUpdating or EditorApplication.isCompiling are true.
///
public static AddressableAssetSettings Settings
{
get
{
if (s_DefaultSettingsObject == null)
{
AddressableAssetSettingsDefaultObject so;
if (EditorBuildSettings.TryGetConfigObject(kDefaultConfigObjectName, out so))
{
s_DefaultSettingsObject = so.LoadSettingsObject();
}
else
{
//legacy support, try to get the old config object and then remove it
if (EditorBuildSettings.TryGetConfigObject(kDefaultConfigAssetName, out s_DefaultSettingsObject))
{
EditorBuildSettings.RemoveConfigObject(kDefaultConfigAssetName);
so = CreateInstance();
so.SetSettingsObject(s_DefaultSettingsObject);
AssetDatabase.CreateAsset(so, kDefaultConfigFolder + "/DefaultObject.asset");
EditorUtility.SetDirty(so);
AddressableAssetUtility.OpenAssetIfUsingVCIntegration(kDefaultConfigFolder + "/DefaultObject.asset");
AssetDatabase.SaveAssets();
EditorBuildSettings.AddConfigObject(kDefaultConfigObjectName, so, true);
}
}
}
return s_DefaultSettingsObject;
}
set
{
if (value != null)
{
var path = AssetDatabase.GetAssetPath(value);
if (string.IsNullOrEmpty(path))
{
Debug.LogErrorFormat("AddressableAssetSettings object must be saved to an asset before it can be set as the default.");
return;
}
}
s_DefaultSettingsObject = value;
AddressableAssetSettingsDefaultObject so;
if (!EditorBuildSettings.TryGetConfigObject(kDefaultConfigObjectName, out so))
{
so = CreateInstance();
AssetDatabase.CreateAsset(so, kDefaultConfigFolder + "/DefaultObject.asset");
AssetDatabase.SaveAssets();
EditorBuildSettings.AddConfigObject(kDefaultConfigObjectName, so, true);
}
so.SetSettingsObject(s_DefaultSettingsObject);
EditorUtility.SetDirty(so);
AddressableAssetUtility.OpenAssetIfUsingVCIntegration(kDefaultConfigFolder + "/DefaultObject.asset");
AssetDatabase.SaveAssets();
}
}
///
/// Gets the settings object with the option to create a new one if it does not exist.
///
/// If true and no settings object exists, a new one will be created using the default config folder and asset name.
/// The default settings object.
public static AddressableAssetSettings GetSettings(bool create)
{
if (Settings == null && create)
Settings = AddressableAssetSettings.Create(kDefaultConfigFolder, kDefaultConfigAssetName, true, true);
return Settings;
}
}
}