Skip to content

Instantly share code, notes, and snippets.

@seiji
Created December 4, 2013 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seiji/7783715 to your computer and use it in GitHub Desktop.
Save seiji/7783715 to your computer and use it in GitHub Desktop.
Build AssetBundle
require 'rake'
PROJECT_DIR = File.dirname __FILE__
UNITY_APP_PATH = "/Applications/Unity/Unity.app/Contents/MacOS/Unity"
UNITY_EDITOR_LOG_PATH = "~/Library/Logs/Unity/Editor.log"
Dir.glob('lib/tasks/**/*.rake').each{ |r| load r }
namespace "asset_bundle" do
desc "build asset bundle"
task :build, [:dir, :out] do |t, args|
project_dir = create_unity_project()
# Copy the project assets from the source folder
puts "Copying resources from source folder to assets folder.";
system_or_exit(%Q[cp -r #{args[:dir]}/* #{project_dir}/Assets]);
puts "Finding assets.";
assets_to_bundle=`cd #{project_dir}; find "Assets" -type f -name "*.*" | sed 's/^.\\\///g' | sed 's/^/assetPathsList.Add("/g' | sed 's/$/");/g'`
# Copy the bundler script into the project
puts "Copy the bundler script into the project."
system_or_exit(%Q[mkdir #{project_dir}/Assets/Editor]);
str = <<"EOS"
using UnityEngine;
using UnityEditor;
using System.Collections;
public class AssetsBundler
{
public static void Bundle()
{
ArrayList assetPathsList = new ArrayList();
#{assets_to_bundle};
ArrayList assetList = new ArrayList();
foreach(string assetPath in assetPathsList)
{
Debug.Log("Loading " + assetPath);
UnityEngine.Object[] assets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
foreach(UnityEngine.Object asset in assets)
{
Debug.Log("Found asset: " + asset.name);
assetList.Add(asset);
}
}
UnityEngine.Object[] allAssets = (UnityEngine.Object[]) assetList.ToArray(typeof(UnityEngine.Object));
BuildPipeline.BuildAssetBundle(allAssets[0],
allAssets,
"#{args[:out]}",
BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets |
BuildAssetBundleOptions.DeterministicAssetBundle,
BuildTarget.Android);
}
}
EOS
File.open("#{project_dir}/Assets/Editor/AssetsBundler.cs", 'w') {|f|
f.write(str)
}
build_asset_bundle(project_dir)
remove_unity_project(project_dir)
end
end
private
def system_or_exit(cmd, stdout = nil)
puts "$ #{cmd}"
cmd += " >#{stdout}" if stdout
system(cmd) or raise "command failed. "
end
def create_unity_project(project_dir = "/tmp/AssetBundle-#{Time.now.to_i}")
puts "Creating temporary project."
unity_command = []
unity_command << %Q[#{UNITY_APP_PATH} -batchmode -quit]
unity_command << %Q[-createProject #{project_dir}]
system_or_exit(unity_command.join(' '))
project_dir
end
def build_asset_bundle(project_dir)
puts "Building the bundle."
unity_command = []
unity_command << %Q[#{UNITY_APP_PATH} -batchmode -quit]
unity_command << %Q[-projectProject #{project_dir}]
unity_command << %Q[-executeMethod AssetsBundler.Bundle]
system_or_exit(unity_command.join(' '))
# system_or_exit(%Q[cat #{UNITY_EDITOR_LOG_PATH}])
end
def remove_unity_project(project_dir)
puts "Deleting temporary project."
system_or_exit(%Q[rm -Rf #{project_dir}])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment