using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using UnityEditor.AddressableAssets.Settings; using UnityEditor.AddressableAssets.Settings.GroupSchemas; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.AddressableAssets.Initialization; using UnityEngine.AddressableAssets.ResourceLocators; using UnityEngine.AddressableAssets.ResourceProviders; using UnityEngine.ResourceManagement.ResourceProviders; using UnityEngine.ResourceManagement.Util; namespace UnityEditor.AddressableAssets.Build.DataBuilders { /// /// Build script used for fast iteration in the editor. /// [CreateAssetMenu(fileName = "BuildScriptFast.asset", menuName = "Addressable Assets/Data Builders/Fast Mode")] public class BuildScriptFastMode : BuildScriptBase { /// public override string Name { get { return "Fast Mode"; } } /// public override bool CanBuildData() { return typeof(T).IsAssignableFrom(typeof(AddressablesPlayModeBuildResult)); } /// public override void ClearCachedData() { DeleteFile(string.Format(PathFormat, "", "catalog")); DeleteFile(string.Format(PathFormat, "", "settings")); } /// public override bool IsDataBuilt() { var catalogPath = string.Format(PathFormat, "", "catalog"); var settingsPath = string.Format(PathFormat, "", "settings"); return File.Exists(catalogPath) && File.Exists(settingsPath); } private string m_PathFormatStore; private string PathFormat { get { if(string.IsNullOrEmpty(m_PathFormatStore)) m_PathFormatStore = "{0}Library/com.unity.addressables/{1}_BuildScriptFastMode.json"; return m_PathFormatStore; } set { m_PathFormatStore = value; } } bool m_NeedsLegacyProvider = false; /// protected override TResult BuildDataImplementation(AddressablesDataBuilderInput context) { TResult result = default(TResult); var timer = new Stopwatch(); timer.Start(); var aaSettings = context.AddressableSettings; PathFormat = context.PathFormat; //create runtime data var aaContext = new AddressableAssetsBuildContext { settings = aaSettings, runtimeData = new ResourceManagerRuntimeData(), bundleToAssetGroup = null, locations = new List() }; aaContext.runtimeData.BuildTarget = context.Target.ToString(); aaContext.runtimeData.LogResourceManagerExceptions = aaSettings.buildSettings.LogResourceManagerExceptions; aaContext.runtimeData.ProfileEvents = ProjectConfigData.postProfilerEvents; aaContext.runtimeData.CatalogLocations.Add(new ResourceLocationData(new[] { ResourceManagerRuntimeData.kCatalogAddress }, string.Format(PathFormat, "file://{UnityEngine.Application.dataPath}/../", "catalog"), typeof(ContentCatalogProvider), typeof(ContentCatalogData))); var errorString = ProcessAllGroups(aaContext); if(!string.IsNullOrEmpty(errorString)) result = AddressableAssetBuildResult.CreateResult(null, 0, errorString); if (result == null) { foreach (var io in aaSettings.InitializationObjects) { if (io is IObjectInitializationDataProvider) aaContext.runtimeData.InitializationObjects.Add((io as IObjectInitializationDataProvider).CreateObjectInitializationData()); } var settingsPath = string.Format(PathFormat, "", "settings"); WriteFile(settingsPath, JsonUtility.ToJson(aaContext.runtimeData), context.Registry); //save catalog var catalogData = new ContentCatalogData(aaContext.locations); if (m_NeedsLegacyProvider) catalogData.ResourceProviderData.Add(ObjectInitializationData.CreateSerializedInitializationData(typeof(LegacyResourcesProvider))); catalogData.ResourceProviderData.Add(ObjectInitializationData.CreateSerializedInitializationData()); catalogData.InstanceProviderData = ObjectInitializationData.CreateSerializedInitializationData(instanceProviderType.Value); catalogData.SceneProviderData = ObjectInitializationData.CreateSerializedInitializationData(sceneProviderType.Value); WriteFile(string.Format(PathFormat, "", "catalog"), JsonUtility.ToJson(catalogData), context.Registry); //inform runtime of the init data path var runtimeSettingsPath = string.Format(PathFormat, "file://{UnityEngine.Application.dataPath}/../", "settings"); PlayerPrefs.SetString(Addressables.kAddressablesRuntimeDataPath, runtimeSettingsPath); result = AddressableAssetBuildResult.CreateResult(settingsPath, aaContext.locations.Count); } if(result != null) result.Duration = timer.Elapsed.TotalSeconds; return result; } /// protected override string ProcessGroup(AddressableAssetGroup assetGroup, AddressableAssetsBuildContext aaContext) { var errorString = string.Empty; PlayerDataGroupSchema playerSchema = assetGroup.GetSchema(); if (playerSchema != null) { m_NeedsLegacyProvider = CreateLocationsForPlayerData(playerSchema, assetGroup, aaContext.locations); return errorString; } var allEntries = new List(); foreach (var a in assetGroup.entries) a.GatherAllAssets(allEntries, true, true); var providerType = typeof(AssetDatabaseProvider).FullName; foreach (var a in allEntries) a.CreateCatalogEntries(aaContext.locations, false, providerType, null, null); return errorString; } } }