using System; using System.Linq; using UnityEditor.Build; using UnityEditor.Build.Reporting; using UnityEngine; namespace UnityEditor.XR.Management { /// /// Base abstract class that provides some common functionality for plugins wishing to integrate with management assisted build. /// /// The type parameter that will be used as the base type of the settings. public abstract class XRBuildHelper : IPreprocessBuildWithReport, IPostprocessBuildWithReport where T : UnityEngine.Object { /// Override of base IXxxprocessBuildWithReport /// The callback order. public virtual int callbackOrder { get { return 0; } } /// Override of base IXxxprocessBuildWithReport /// A string specifying the key to be used to set/get settigns in EditorBuildSettings. public abstract string BuildSettingsKey { get; } /// Helper functin to return current settings for a specific build target. /// /// An enum specifying which platform group this build is for. /// A unity object representing the settings instance data for that build target, or null if not found. public virtual UnityEngine.Object SettingsForBuildTargetGroup(BuildTargetGroup buildTargetGroup) { UnityEngine.Object settingsObj = null; EditorBuildSettings.TryGetConfigObject(BuildSettingsKey, out settingsObj); if (settingsObj == null || !(settingsObj is T)) return null; return settingsObj; } void CleanOldSettings() { BuildHelpers.CleanOldSettings(); } void SetSettingsForRuntime(UnityEngine.Object settingsObj) { // Always remember to cleanup preloaded assets after build to make sure we don't // dirty later builds with assets that may not be needed or are out of date. CleanOldSettings(); if (settingsObj == null) return; if (!(settingsObj is T)) { Type typeOfT = typeof(T); Debug.LogErrorFormat("Settings object is not of type {0}. No settings will be copied to runtime.", typeOfT.Name); return; } UnityEngine.Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets(); if (!preloadedAssets.Contains(settingsObj)) { var assets = preloadedAssets.ToList(); assets.Add(settingsObj); PlayerSettings.SetPreloadedAssets(assets.ToArray()); } } /// Override of base IPreprocessBuildWithReport /// /// BuildReport instance passed in from build pipeline. public virtual void OnPreprocessBuild(BuildReport report) { SetSettingsForRuntime(SettingsForBuildTargetGroup(report.summary.platformGroup)); } /// Override of base IPostprocessBuildWithReport /// /// BuildReport instance passed in from build pipeline. public virtual void OnPostprocessBuild(BuildReport report) { // Always remember to cleanup preloaded assets after build to make sure we don't // dirty later builds with assets that may not be needed or are out of date. CleanOldSettings(); } } }