您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
75 行
2.3 KiB
75 行
2.3 KiB
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEditor;
|
|
using UnityEditor.Build;
|
|
using UnityEditor.Build.Reporting;
|
|
|
|
|
|
namespace Unity.MLAgents.Editor
|
|
{
|
|
internal class MLAgentsSettingsBuildProvider : IPreprocessBuildWithReport, IPostprocessBuildWithReport
|
|
{
|
|
private MLAgentsSettings m_SettingsAddedToPreloadedAssets;
|
|
|
|
public int callbackOrder => 0;
|
|
|
|
public void OnPreprocessBuild(BuildReport report)
|
|
{
|
|
var wasDirty = IsPlayerSettingsDirty();
|
|
m_SettingsAddedToPreloadedAssets = null;
|
|
|
|
var preloadedAssets = PlayerSettings.GetPreloadedAssets().ToList();
|
|
if (!preloadedAssets.Contains(MLAgentsSettingsManager.Settings))
|
|
{
|
|
m_SettingsAddedToPreloadedAssets = MLAgentsSettingsManager.Settings;
|
|
preloadedAssets.Add(m_SettingsAddedToPreloadedAssets);
|
|
PlayerSettings.SetPreloadedAssets(preloadedAssets.ToArray());
|
|
}
|
|
|
|
if (!wasDirty)
|
|
ClearPlayerSettingsDirtyFlag();
|
|
}
|
|
|
|
public void OnPostprocessBuild(BuildReport report)
|
|
{
|
|
if (m_SettingsAddedToPreloadedAssets == null)
|
|
return;
|
|
|
|
var wasDirty = IsPlayerSettingsDirty();
|
|
|
|
var preloadedAssets = PlayerSettings.GetPreloadedAssets().ToList();
|
|
if (preloadedAssets.Contains(m_SettingsAddedToPreloadedAssets))
|
|
{
|
|
preloadedAssets.Remove(m_SettingsAddedToPreloadedAssets);
|
|
PlayerSettings.SetPreloadedAssets(preloadedAssets.ToArray());
|
|
}
|
|
|
|
m_SettingsAddedToPreloadedAssets = null;
|
|
|
|
if (!wasDirty)
|
|
ClearPlayerSettingsDirtyFlag();
|
|
}
|
|
|
|
|
|
private static bool IsPlayerSettingsDirty()
|
|
{
|
|
#if UNITY_2019_OR_NEWER
|
|
var settings = Resources.FindObjectsOfTypeAll<PlayerSettings>();
|
|
if (settings != null && settings.Length > 0)
|
|
return EditorUtility.IsDirty(settings[0]);
|
|
return false;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
private static void ClearPlayerSettingsDirtyFlag()
|
|
{
|
|
#if UNITY_2019_OR_NEWER
|
|
var settings = Resources.FindObjectsOfTypeAll<PlayerSettings>();
|
|
if (settings != null && settings.Length > 0)
|
|
EditorUtility.ClearDirty(settings[0]);
|
|
#endif
|
|
}
|
|
}
|
|
}
|