Souris
Souris

Reputation: 326

AssetBundle Unity on Server

I would like to ask if anyone create Assetbundle in Unity on cloud? I would like to generate the AssetBundle dynamically on cloud and the client app will download it accordingly.

Could you let me know your idea? Is there any cloud service for hosting Unity ?

Upvotes: 1

Views: 4092

Answers (2)

Arvind
Arvind

Reputation: 730

The accepted answer is spot on but there are slight changes as Unity5.6 now supports every other feature in the free version too. I've been working on a similar project that required building asset bundles dynamically. I'll post my code snippet for the same so that the process of identifying this gets simpler for everyone else in future.

But before that, there are some limitations for this process that you may need to consider. Building asset bundles dynamically on cloud requires (Command line) batchmode which runs Unity on commandline (Unity should be installed to build bundles). This asset building process works only on Windows and OSX (No Linux). The command to invoke Unity in batch mode is given below and has to be executed from Unity executable's location,

this command creates an empty project,

Unity -batchmode -quit -createProject <path/to/create a project>

After creating a project, you can save a script to build assets in the Assets/Editor folder, I have a written a script to automate the process of building assetbundle for all assets in the Assets/Models folder.

//BuildAssets.cs
using System.Collections;
using System.Collections.Generic;

public class BuildAssets : UnityEngine.MonoBehaviour
{
static void BuildAssetBundle()
{
    int i = 0;
    string log = "log.txt";
    string[] assetN;
    int N_Files;
    UnityEditor.AssetBundleBuild[] AssetMap = new UnityEditor.AssetBundleBuild[2];
    AssetMap[0].assetBundleName = "res";

    // Adding to path /Models
    string path = UnityEngine.Application.dataPath + "/Models";

    //log
    System.IO.File.AppendAllText(log, System.DateTime.Now.ToString() + "\n\n");
    System.IO.File.AppendAllText(log, path + "\n");


    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
    System.IO.FileInfo[] files = dir.GetFiles();

    // Number of files in "/Models" folder
    N_Files = files.Length;

    //log
    System.IO.File.AppendAllText(log, "Num assets: "+N_Files + " \n");


    assetN = new string[N_Files];
    foreach (System.IO.FileInfo file in files)
    {
        if (file.Exists)
        {
            if (!file.Extension.Equals(".meta"))
            {
                assetN[i] = "Assets/Resources/" + file.Name;
                System.IO.File.AppendAllText(log, assetN[i] + " \n");
                i += 1;
            }
        }
    }
    AssetMap[0].assetNames = assetN;

    UnityEditor.BuildPipeline.BuildAssetBundles("Assets/AssetBundles", AssetMap, UnityEditor.BuildAssetBundleOptions.None, UnityEditor.BuildTarget.Android);
    System.IO.File.AppendAllText(log, "\t----X----\n"); //log
   }
}

This is the command for building the asset bundle through command line.

Unity -batchmode -quit -projectPath path/to/UnityProjects/Projectname -executeMethod BuildAssets.BuildAssetBundle -logFile <Log file location>

I've tested this and it works for our project.

Upvotes: 3

Krzysztof Bociurko
Krzysztof Bociurko

Reputation: 4662

AssetBundles require unity pro to build. There is a command line batch mode that you can use to build your asset bundles automaticly and host it on virtualy any host (a simple HTTP get).

Remember that you might not need asset bundles (or unity pro) - you can easily download textures and audio from the web using the WWW class. For textures you can use png, jpeg and tiff, for audio wav, ogg (only desktop and webplayer), mp3 (only mobile). Mesh loading should be also possible but that will require additional tools.

Upvotes: 0

Related Questions