For instance I have an empty project with all my sprites (mercenaries and weapons). I assigned asset bundles and variants.
Create a main unity scene empty.
A prebuild script create AB like this:
// Build specific target
static void Build(BuildTarget target, string path)
{
TestVersion();
Debug.Log("Build Asset Bundles");
BuildPipeline.BuildAssetBundles(path,
BuildAssetBundleOptions.None,
target);
CopyAssetBundlesToStreamingAsset();
}
I have a preproc define to enable only right Build method like this:
#if UNITY_ANDROID
[MenuItem("Resources/AssetsBundles/Build Asset Bundles/Android")]
public static void BuildAssetBundles()
{
Build(BuildTarget.Android, "AssetBundles/Android");
}
#endif
Then AB is deleted and copyied in StreamingAssets in my Main game project. In my other project that manage mercenaries and weapons I upload to server in post buid script.
Add post build editor script in UCB that create AB and upload to my server with UnityWebRequest (or HttpWebRequest because depends of your server sometimes one work or the other).
sample code to put to AmazonS3 (read the doc on Amazon if needed)
Debug.Log("send to amazons3");
request = (HttpWebRequest)WebRequest.Create(amazonS3SignedUrl);
request.Method = "PUT";
//request.ServicePoint.Expect100Continue = false;
request.ProtocolVersion = HttpVersion.Version10;
request.ReadWriteTimeout = 500000;
request.Timeout = 500000;
string filePath = "AssetBundles/"+plateforme+"/" + Pair.Key;
using (Stream dataStream = request.GetRequestStream())
{
// envoie en streaming
byte[] buffer = new byte[8000];
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
int bytesRead = 0;
long totalRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
dataStream.Write(buffer, 0, bytesRead);
#if !UNITY_CLOUD_BUILD
totalRead += bytesRead;
EditorUtility.DisplayProgressBar("Uploading Asset Bundles", "Reading Asset " + Pair.Key, (float)totalRead / (float)fileStream.Length);
#endif
}
}
}
#if !UNITY_CLOUD_BUILD
EditorUtility.DisplayProgressBar("Uploading Asset Bundles", "Uploading Asset " + Pair.Key, progress / nbBundles);
#endif
httpResponse = request.GetResponse() as HttpWebResponse;
Debug.Log(httpResponse.ToString());
httpResponse.Close();
Maybe you need certificate callback for https:
public static bool MyRemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
bool isOk = true;
// If there are errors in the certificate chain, look at each error to determine the cause.
if (sslPolicyErrors != SslPolicyErrors.None)
{
for (int i = 0; i < chain.ChainStatus.Length; i++)
{
if (chain.ChainStatus[i].Status != X509ChainStatusFlags.RevocationStatusUnknown)
{
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
bool chainIsValid = chain.Build((X509Certificate2)certificate);
if (!chainIsValid)
{
isOk = false;
}
}
}
}
return isOk;
}
//set callback for HTTPS
ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
This is a good starting point. You need to have a server URL where to sent AB. Before PUT I POST on a server to obtain a signed Amazon URL.
Hope you will manage to create a great pipeline for AB and UCB. This saved my life and hours of build.