Firstborn/Library/PackageCache/com.unity.addressables@1.19.19/Editor/Build/AddressablesBuildScriptHook...

77 lines
2.9 KiB
C#
Raw Normal View History

using System;
using UnityEngine;
namespace UnityEditor.AddressableAssets.Build
{
/// <summary>
/// Entry point to set callbacks for builds.
/// </summary>
public static class BuildScript
{
/// <summary>
/// Global delegate for handling the result of AddressableAssets builds. This will get called for player builds and when entering play mode.
/// </summary>
public static Action<AddressableAssetBuildResult> 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<AddressableAssetBuildResult>(null, 0, err);
BuildScript.buildCompleted(result);
}
return;
}
if (!settings.ActivePlayModeDataBuilder.CanBuildData<AddressablesPlayModeBuildResult>())
{
var err = string.Format("Active build script {0} cannot build AddressablesPlayModeBuildResult.", settings.ActivePlayModeDataBuilder);
Debug.LogError(err);
if (BuildScript.buildCompleted != null)
{
var result = AddressableAssetBuildResult.CreateResult<AddressableAssetBuildResult>(null, 0, err);
BuildScript.buildCompleted(result);
}
return;
}
var res = settings.ActivePlayModeDataBuilder.BuildData<AddressablesPlayModeBuildResult>(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;
}
}
}