using System;
using UnityEngine;
namespace UnityEditor.AddressableAssets.Build
{
///
/// Entry point to set callbacks for builds.
///
public static class BuildScript
{
///
/// Global delegate for handling the result of AddressableAssets builds. This will get called for player builds and when entering play mode.
///
public static Action buildCompleted;
}
static class AddressablesBuildScriptHooks
{
[InitializeOnLoadMethod]
static void Init()
{
EditorApplication.playModeStateChanged += OnEditorPlayModeChanged;
}
static void OnEditorPlayModeChanged(PlayModeStateChange state)
{
var settings = AddressableAssetSettingsDefaultObject.Settings;
if (settings == null)
return;
if (state == PlayModeStateChange.ExitingEditMode)
{
if (settings.ActivePlayModeDataBuilder == null)
{
var err = "Active play mode build script is null.";
Debug.LogError(err);
if (BuildScript.buildCompleted != null)
{
var result = AddressableAssetBuildResult.CreateResult(null, 0, err);
BuildScript.buildCompleted(result);
}
return;
}
if (!settings.ActivePlayModeDataBuilder.CanBuildData())
{
var err = string.Format("Active build script {0} cannot build AddressablesPlayModeBuildResult.", settings.ActivePlayModeDataBuilder);
Debug.LogError(err);
if (BuildScript.buildCompleted != null)
{
var result = AddressableAssetBuildResult.CreateResult(null, 0, err);
BuildScript.buildCompleted(result);
}
return;
}
var res = settings.ActivePlayModeDataBuilder.BuildData(new AddressablesDataBuilderInput(settings));
if (!string.IsNullOrEmpty(res.Error))
{
Debug.LogError(res.Error);
EditorApplication.isPlaying = false;
}
else
{
if (BuildScript.buildCompleted != null)
BuildScript.buildCompleted(res);
settings.DataBuilderCompleted(settings.ActivePlayModeDataBuilder, res);
settings.HostingServicesManager.exitingEditMode = true;
}
}
else
settings.HostingServicesManager.exitingEditMode = false;
}
}
}