using System;
using System.Collections.Generic;
#if UNITY_2019_3_OR_NEWER
using UnityEditor.Build.Content;
#endif
using UnityEditor.Build.Pipeline.Interfaces;
using UnityEditor.Build.Pipeline.Utilities;
namespace UnityEditor.Build.Pipeline
{
#if UNITY_2019_3_OR_NEWER
///
/// Basic implementation of ICustomAssets. Stores the list of Custom Assets generated during the Scriptable Build Pipeline.
///
///
[Serializable]
public class CustomAssets : ICustomAssets
{
///
public List Assets { get; private set; }
///
/// Default constructor, creates an empty CustomAssets.
///
public CustomAssets()
{
Assets = new List();
}
}
#endif
///
/// Basic implementation of IBuildContent. Stores the list of Assets to feed the Scriptable Build Pipeline.
///
///
[Serializable]
public class BuildContent : IBuildContent
{
///
public List Assets { get; private set; }
///
public List Scenes { get; private set; }
#if UNITY_2019_3_OR_NEWER
///
public List CustomAssets { get; private set; }
#endif
///
/// Default constructor, creates an empty BuildContent.
///
public BuildContent() {}
///
/// Default constructor, takes a set of Assets and converts them to the appropriate properties.
///
/// The set of Assets identified by GUID to ensure are packaged with the build
public BuildContent(IEnumerable assets)
{
if (assets == null)
throw new ArgumentNullException("assets");
Assets = new List();
Scenes = new List();
#if UNITY_2019_3_OR_NEWER
CustomAssets = new List();
#endif
foreach (var asset in assets)
{
ValidationMethods.Status assetType = ValidationMethods.ValidAsset(asset);
if (assetType == ValidationMethods.Status.Asset)
Assets.Add(asset);
else if (assetType == ValidationMethods.Status.Scene)
Scenes.Add(asset);
else
throw new ArgumentException(string.Format("Asset '{0}' is not a valid Asset or Scene.", asset.ToString()));
}
}
}
///
/// Basic implementation of IBundleBuildContent. Stores the list of Assets with explicit Asset Bundle layout to feed the Scriptable Build Pipeline.
///
///
[Serializable]
public class BundleBuildContent : IBundleBuildContent
{
///
public List Assets { get; private set; }
///
public List Scenes { get; private set; }
#if UNITY_2019_3_OR_NEWER
///
public List CustomAssets { get; private set; }
///
public Dictionary> AdditionalFiles { get; private set; }
#endif
///
public Dictionary Addresses { get; private set; }
///
public Dictionary> BundleLayout { get; private set; }
///
/// Default constructor, creates an empty BundleBuildContent.
///
public BundleBuildContent() {}
///
/// Default constructor, takes a set of AssetBundleBuild and converts them to the appropriate properties.
///
/// The set of AssetbundleBuild to be built.
public BundleBuildContent(IEnumerable bundleBuilds)
{
if (bundleBuilds == null)
throw new ArgumentNullException("bundleBuilds");
Assets = new List();
Scenes = new List();
Addresses = new Dictionary();
BundleLayout = new Dictionary>();
#if UNITY_2019_3_OR_NEWER
CustomAssets = new List();
AdditionalFiles = new Dictionary>();
#endif
foreach (var bundleBuild in bundleBuilds)
{
List guids;
BundleLayout.GetOrAdd(bundleBuild.assetBundleName, out guids);
ValidationMethods.Status bundleType = ValidationMethods.Status.Invalid;
for (int i = 0; i < bundleBuild.assetNames.Length; i++)
{
string assetPath = bundleBuild.assetNames[i];
GUID asset = new GUID(AssetDatabase.AssetPathToGUID(assetPath));
// Ensure the path is valid
ValidationMethods.Status status = ValidationMethods.ValidAsset(asset);
if (status == ValidationMethods.Status.Invalid)
throw new ArgumentException(string.Format("Asset '{0}' is not a valid Asset or Scene.", assetPath));
// Ensure we do not have a mixed bundle
if (bundleType == ValidationMethods.Status.Invalid)
bundleType = status;
else if (bundleType != status)
throw new ArgumentException(string.Format("Asset Bundle '{0}' is invalid because it contains mixed Asset and Scene types.", bundleBuild.assetBundleName));
string address = bundleBuild.addressableNames != null && i < bundleBuild.addressableNames.Length && !string.IsNullOrEmpty(bundleBuild.addressableNames[i]) ?
bundleBuild.addressableNames[i] : AssetDatabase.GUIDToAssetPath(asset.ToString());
// Add the guid to the bundle map
guids.Add(asset);
// Add the guid & address
Addresses.Add(asset, address);
// Add the asset to the correct collection
if (status == ValidationMethods.Status.Asset)
Assets.Add(asset);
else if (status == ValidationMethods.Status.Scene)
Scenes.Add(asset);
}
}
}
}
}